如何将 -I 合并到 makefile 中
我按照以下方式编译特定代码没有问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 GNU Make makefile 中,约定是使用 CXXFLAGS 作为 C++ 编译器标志,并且要为特定目标添加标志,您可以使用特定于目标的变量。 例如:
请注意,您可能还想切换到使用模式规则,而不是为所有(非常相似的)目标显式声明规则。 例如,您可以将 Code1、Code2 和 Code3 规则替换为:
编辑:响应有关所见特定错误的更新帖子:看起来您可能会被烧毁,因为您包含 <使用 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: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:
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
-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
和/opt/local/include
之间的空格:It would be helpful to know what the complaint is -- but try taking out the space between the
-I
and/opt/local/include
:如果您使用 GNU make,您可能会考虑这样的结构:
您依靠内置规则来管理从
.cc
文件到.o
文件到可执行文件的编译。请注意,您可能还必须定义链接器标志
等......
If you're using GNU make you might consider a structure like:
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
and the like...
这可能有效...
This MIGHT work...