如何将 -I 合并到 makefile 中

发布于 2024-07-17 05:12:28 字数 1609 浏览 4 评论 0原文

我按照以下方式编译特定代码没有问题:

g++ -I /opt/local/include Code1.cc -o Code1

但是,当我尝试在 makefile 中执行此操作时:

CXX = g++ -Wall -Werror -gstabs -pedantic -O2 -g

all: Code3 Code2 Code1


Code3: Code3.cc Tools.cc
    $(CXX)   $^ -o $@

Code2: Code2.cc Tools.cc
    $(CXX)   $^ -o $@

Code1: Code1.cc Tools.cc
    $(CXX) -I /opt/local/include $^ -o $@

它会抱怨。 正确的做法是什么? 请注意,只有 Code1.cc 需要包含外部库。

Code1.cc 的标头如下所示:

#include <iostream>      
#include <vector>        
#include <fstream>       
#include <sstream>       
#include <iomanip>       
#include <boost/math/distributions/chi_squared.hpp>
using namespace std;     
using boost::math::chi_squared; 
using boost::math::quantile;

我得到的错误如下:

g++ -Wall -Werror -gstabs -pedantic -O2 -g -I/opt/local/include Code1.cc Tools.cc -o EstimateErrorMean
In file included from /opt/local/include/boost/detail/lcast_precision.hpp:16,
                 from /opt/local/include/boost/lexical_cast.hpp:31,
                 from /opt/local/include/boost/math/special_functions/gamma.hpp:23,
                 from /opt/local/include/boost/math/distributions/chi_squared.hpp:13,
                 from EstimateErrorMean.cc:19:
/opt/local/include/boost/integer_traits.hpp:164:66: error: use of C99 long long integer constant
/opt/local/include/boost/integer_traits.hpp:164:77: error: use of C99 long long integer constant
/opt/local/include/boost/integer_traits.hpp:170:70: error: use of C99 long long integer constant

I have no problem compiling specific code the following way:

g++ -I /opt/local/include Code1.cc -o Code1

However when I tried to do that in the makefile:

CXX = g++ -Wall -Werror -gstabs -pedantic -O2 -g

all: Code3 Code2 Code1


Code3: Code3.cc Tools.cc
    $(CXX)   $^ -o $@

Code2: Code2.cc Tools.cc
    $(CXX)   $^ -o $@

Code1: Code1.cc Tools.cc
    $(CXX) -I /opt/local/include $^ -o $@

It complains. What's the correct way to do it?
Note that only Code1.cc require the external library as include.

The header of Code1.cc looks like this:

#include <iostream>      
#include <vector>        
#include <fstream>       
#include <sstream>       
#include <iomanip>       
#include <boost/math/distributions/chi_squared.hpp>
using namespace std;     
using boost::math::chi_squared; 
using boost::math::quantile;

The error I get is as follows:

g++ -Wall -Werror -gstabs -pedantic -O2 -g -I/opt/local/include Code1.cc Tools.cc -o EstimateErrorMean
In file included from /opt/local/include/boost/detail/lcast_precision.hpp:16,
                 from /opt/local/include/boost/lexical_cast.hpp:31,
                 from /opt/local/include/boost/math/special_functions/gamma.hpp:23,
                 from /opt/local/include/boost/math/distributions/chi_squared.hpp:13,
                 from EstimateErrorMean.cc:19:
/opt/local/include/boost/integer_traits.hpp:164:66: error: use of C99 long long integer constant
/opt/local/include/boost/integer_traits.hpp:164:77: error: use of C99 long long integer constant
/opt/local/include/boost/integer_traits.hpp:170:70: error: use of C99 long long integer constant

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木格 2024-07-24 05:12:28

在 GNU Make makefile 中,约定是使用 CXXFLAGS 作为 C++ 编译器标志,并且要为特定目标添加标志,您可以使用特定于目标的变量。 例如:

CXX=g++

# Set CXXFLAGS to include the base set of flags

CXXFLAGS=-Wall -Werror -gstabs -pedantic -O2 -g

all: Code3 Code2 Code1

Code3: Code3.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

Code2: Code2.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

# Add a target-specific addition to CXXFLAGS for Code1:

Code1: CXXFLAGS += -I/opt/local/include

Code1: Code1.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

请注意,您可能还想切换到使用模式规则,而不是为所有(非常相似的)目标显式声明规则。 例如,您可以将 Code1、Code2 和 Code3 规则替换为:

%: %.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

编辑:响应有关所见特定错误的更新帖子:看起来您可能会被烧毁,因为您包含 <使用 Makefile 时,代码>-Wall -Werror 位于标志中,但不在命令行中。 -Wall 导致 g++ 对语法更加挑剔; -Werror 导致 g++ 将通常只是警告的事情提升为全面的错误。 由于错误是针对第三方库中的代码报告的,因此也许您可以使用不太严格的警告选项(即删除 -Wall); 或者也许您需要切换到可以处理构造而不会抱怨的较新版本的编译器; 或者您可能只需要显式指定 -std=c99 来提醒编译器您需要 C99 支持。

希望有帮助,

埃里克·梅尔斯基

In a GNU Make makefile, the convention is to use CXXFLAGS for C++ compiler flags, and to make an addition to the flags for a specific target, you can use target-specific variables. For example:

CXX=g++

# Set CXXFLAGS to include the base set of flags

CXXFLAGS=-Wall -Werror -gstabs -pedantic -O2 -g

all: Code3 Code2 Code1

Code3: Code3.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

Code2: Code2.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

# Add a target-specific addition to CXXFLAGS for Code1:

Code1: CXXFLAGS += -I/opt/local/include

Code1: Code1.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

Note that you may also want to switch to using pattern rules, rather than explicitly declaring rules for all your (very similar) targets. For example, you could replace the Code1, Code2 and Code3 rules with just this:

%: %.cc Tools.cc
        $(CXX) $(CXXFLAGS) $^ -o $@

EDIT: In response to the updated post regarding the specific error seen: it looks like you are probably getting burned because you include -Wall -Werror in the flags when you are using the Makefile, but not on the command line. -Wall causes g++ to be a lot more picky about syntax; -Werror causes g++ to promote things that are normally just warnings into full-blown errors. Since the errors are being reported against code in a third-party library, perhaps you can get by with less strict warning options (ie, remove -Wall); or perhaps you need to switch to a newer version of the compiler that can handle the constructs without complaining; or perhaps you just need to explicitly specify -std=c99 to alert the compiler that you want C99 support.

Hope that helps,

Eric Melski

墨离汐 2024-07-24 05:12:28

-pedantic 导致报告所有必需的警告,而 -Werror 导致将警告报告为错误。 由于 C++ 没有定义“ULL”长整型常量语法(C99 定义了),因此 g++ 可能会报告此情况,然后将其提升为完全错误状态。

尝试删除 -pedantic。

或者尝试#include。< /a>

-pedantic causes all required warnings to be reported, and -Werror causes warnings to be reported as errors. As C++ does not define the "ULL" long long integer constant syntax (C99 does), this is probably being reported and then promoted to full-on error status by g++.

Try removing -pedantic.

Or try #including <boost/cstdint.hpp>.

愿与i 2024-07-24 05:12:28

了解投诉内容会很有帮助,但请尝试去掉 -I/opt/local/include 之间的空格:

 -I/opt/local/include $^ -o $@

It would be helpful to know what the complaint is -- but try taking out the space between the -I and /opt/local/include :

 -I/opt/local/include $^ -o $@
快乐很简单 2024-07-24 05:12:28

如果您使用 GNU make,您可能会考虑这样的结构:

CXXFLAGS += -Wall -Werror -gstabs -pedantic -O2 -g
CXXFLGAS += -I/opt/local/include

SRCS:=Code1.cc Code2.cc Code3.cc
OBJS:=$(subst .cc,.o,$(SRCS))
EXES:=$(subst .o,,$(OBJS))

all: $(EXES)

您依靠内置规则来管理从 .cc 文件到 .o 文件到可执行文件的编译。

请注意,您可能还必须定义链接器标志

LDFLAGS:=-g

等......

If you're using GNU make you might consider a structure like:

CXXFLAGS += -Wall -Werror -gstabs -pedantic -O2 -g
CXXFLGAS += -I/opt/local/include

SRCS:=Code1.cc Code2.cc Code3.cc
OBJS:=$(subst .cc,.o,$(SRCS))
EXES:=$(subst .o,,$(OBJS))

all: $(EXES)

where you count on the built in rule to manage compiling from .cc files to .o files to the executables.

Note that you may also have to define the linker flags

LDFLAGS:=-g

and the like...

゛时过境迁 2024-07-24 05:12:28
INCLUDE_PATH = -I/opt/local/include
LINK_PATH = -L <Library paths>
LIBS = -l<libraryname>

OBJECTS = Code1.o\
          Code2.o\
          Code3.o\

CXXFLAGS += $(INCLUDE_PATH)

Code1 : $(OBJECTS)
          $(CXX) $(LINK_PATH) -o$@ $(OBJECTS) $(LIBS)

这可能有效...

INCLUDE_PATH = -I/opt/local/include
LINK_PATH = -L <Library paths>
LIBS = -l<libraryname>

OBJECTS = Code1.o\
          Code2.o\
          Code3.o\

CXXFLAGS += $(INCLUDE_PATH)

Code1 : $(OBJECTS)
          $(CXX) $(LINK_PATH) -o$@ $(OBJECTS) $(LIBS)

This MIGHT work...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文