g ++标志仅检查语法? [英] g++ flag to only check syntax?

查看:85
本文介绍了g ++标志仅检查语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以让g ++在编译时检查C ++ 98语法,但同时又像没有给出 -std = 一样进行编译?我的目标是确保我的源代码保持C ++ 98,但我不想阻止g ++使用任何更新的优化或技巧。目前,我编译了两次projet,一次是 CXXFLAGS = -std = c ++ 98 ,最后一次是空的 CXXFLAGS 发布。

Is there a way to have g++ check for C++98 syntax when compiling but at the same time compile as if no -std= has been given ? My goal is to make sure that my source code stays C++98 but I don't want to prevent g++ from using any newer optimisation or trick. For the moment, I compile my projet twice, once with CXXFLAGS=-std=c++98 and one with a final empty CXXFLAGS for release.

它看起来像 gcc 5 将具有 -Wc90-c99-compat -Wc99-c11-compat

It looks like gcc 5 will have -Wc90-c99-compat and -Wc99-c11-compat, that something in that direction.

推荐答案

必须运行两次,但是您可以在 -std = c ++ 98 传递中节省编译时
,并且通过仅指定语法检查来避免生成不需要的对象文件。您可以通过
传递选项 -fsyntax-only 来做到这一点。

You will have to run the compiler twice, but you can save compiletime on the -std=c++98 pass and avoid generating unwanted object files by specifying syntax-checking only. You do that by passing the option -fsyntax-only.

您还需要修改您的 make 以跳过C ++ 98的链接,因为没有任何链接。

You'd also need to modify your make to skip linkage for C++98, as there'll be nothing to link.

可能最有效的方法是在
上的 make 以下行:

Probably the most efficient way you could do this is with a make on the following lines:

.phony: all clean

SRCS = foo.cpp
FLAT_SRCS = $(patsubst %.cpp,%.ii,$(SRCS))
OBJS = $(patsubst %.ii,%.o,$(FLAT_SRCS))

%.ii: %.cpp
    g++ -std=c++98 $(CPPFLAGS) -E $< > $@ && g++ -std=c++98 -fsyntax-only $@

%.o: %.cpp
%.o: %.ii
    g++ -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<


all: foo

foo: $(OBJS) 
    g++ -o $@ $(CXXFLAGS) $(LDFLAGS) $(OBJS) $(LDLIBS)

clean:
    rm -f $(OBJS) $(FLAT_SRCS) foo

在这里,%。ii:%.cpp 规则将首先对 .cpp .ii
然后将已预处理的源传递给C ++ 98语法检查传递
,它不会产生新的

Here, the %.ii: %.cpp rule will first just preprocess the .cpp to the .ii, then pass the already preprocessed source to a C++98 syntax-checking pass, which produces no new file.

空的%。o:%.cpp 规则将覆盖隐含规则,否则该隐含规则将
使 .cpp 编译为 .o ,然后将其替换为 %.o:%.ii
规则来编译 .ii 。 g ++识别 .ii 表示已经预处理过的
C ++源代码,因此它不会第二次对源进行预处理。

The empty %.o: %.cpp rule overrides the implicit rule that would otherwise cause the .cpp to be compiled to .o, and it is replaced with the %.o: %.ii rule to compile the .ii. g++ recognises .ii as denoting already-preprocessed C++ source code, so it will not preprocess the source a second time.

代码仅预处理一次,而目标代码仅生成一次。

Code is only preprocessed once, and object code is only generated once.

链接照常。提供了C ++ 98语法检查通过后, make 看起来将像:

The linkage is as usual. Provided the C++98 syntax check passes, a make will look like:

$ make
g++ -std=c++98  -E foo.cpp > foo.ii && g++ -std=c++98 -fsyntax-only foo.ii
g++ -c -o foo.o   foo.ii
g++ -o foo   foo.o 
rm foo.ii

您会注意到, make 会自动删除预处理 .ii ,这很好:
只是 .cpp .o

You'll note that make automatically deletes the preprocessed .ii, which is fine: it's just a conduit between the .cpp and the .o.

话虽如此,我支持@Matt McNabb的观察,即您对
一无所有得益于此!鉴于您的代码 C ++ 98,当编译器指示它必须为C ++ 98时,编译器将不会对其进行任何优化(
)。当GCC进入其业务的
优化阶段时,它不再关心
最初使用哪种源代码。

All that being said, I side with @Matt McNabb's observation that you have nothing to gain by this! Given your code is C++98, the compiler won't optimize it any better when instructed that it must be C++98 than when not. By time GCC gets to the optimization stage of its business, it no longer cares what kind of source code it started off with.

您可能会假设 -std = c ++ 98 告诉g ++ 4.x,导致整个
编译器的行为类似于g ++2.x。不是这样仍然是g ++ 4.x,带有
g ++ 4.x优化技术等,仅通过C ++ 98的语言定义进行操作。

You may be supposing that -std=c++98, when given say to g++ 4.x, causes the whole compiler to behave as if was g++ 2.x. Not so. It's still g++ 4.x, with the g++ 4.x optimization tech, etc., just operating by the language definition of C++98.

如果您的代码由于某种原因不得不在某些 old 编译器(而不是release编译器)上通过
作为C ++ 98,则肯定会有一点,在这种情况下,您
将需要区分makefile中的编译器。但这显然不是
案。您也可以按常规使用 -std = C ++ 98

There would certainly be a point in this if your code for some reason had to pass, as C++98, on some older compiler than your release compiler, and in that case you would need to distinguish the compilers in the makefile. But apparently this is not the case. You might as well just compile conventionally with -std=C++98

这篇关于g ++标志仅检查语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆