C++ g++ 预处理器输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12489719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:19:40  来源:igfitidea点击:

g++ preprocessor output

c++g++c-preprocessor

提问by mirt

I want to get only preprocessed version of file.cc. I did g++ -E file.cc, got:

我只想获得file.cc. 我做了g++ -E file.cc,得到:

# 1 "file.cc"
# 1 "<command-line>"
# 1 "file.cc"

What did I do wrong?

我做错了什么?

回答by Andreas Fester

Assumed that your source file contains a simple main function:

假设您的源文件包含一个简单的 main 函数:

$ cat file.cc
int main() {
    return 0;
}

Then, with the command you showed, the output looks like this:

然后,使用您显示的命令,输出如下所示:

$ g++ -E file.cc
# 1 "file.cc"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "file.cc"

int main() {
    return 0;
}

The output which is shown in the question happens when file.ccis empty. Note that "empty" means that the file can still contain comments, or #ifdefblocks with a condition which evaluates to false - since the preprocessor filters them out, they do not appear in the output either.

问题中显示的输出在file.cc为空时发生。请注意,“空”意味着文件仍然可以包含注释或#ifdef条件为 false 的块 - 由于预处理器将它们过滤掉,它们也不会出现在输出中。

回答by Christian Stieber

If you don't want the line markers, use the -P option:

如果您不想要线条标记,请使用 -P 选项:

-P
    Inhibit generation of linemarkers in the output from the preprocessor. This might
    be useful when running the preprocessor on something that is not C code, and will
    be sent to a program which might be confused by the linemarkers.