C语言 gcc预处理后可以输出C代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4900870/
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-09-02 07:43:32  来源:igfitidea点击:

Can gcc output C code after preprocessing?

cc-preprocessorpreprocessorpreprocessor-directive

提问by LGTrader

I'm using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I'd like to see the C code that I'm compiling after preprocessing, more like what I'd write.

我正在使用一个开源库,它似乎有很多预处理指令来支持除 C 之外的许多语言。这样我就可以研究库正在做什么我想看看我在预处理后编译的 C 代码,更像是我写的。

Can gcc (or any other tool commonly available in Linux) read this library but output C code that has the preprocessing converted to whatever and is also readable by a human?

gcc(或 Linux 中常用的任何其他工具)可以读取这个库,但输出 C 代码,该代码将预处理转换为任何内容并且也可以被人类读取吗?

回答by mipadi

Yes. Pass gcc the -Eoption. This will output preprocessed source code.

是的。通过 gcc-E选项。这将输出预处理的源代码。

回答by tpdi

cppis the preprocessor.

cpp是预处理器。

Run cpp filename.cto output the preprocessed code, or better, redirect it to a file with cpp filename.c > filename.preprocessed.

运行cpp filename.c以输出预处理的代码,或者更好地将其重定向到带有 cpp filename.c > filename.preprocessed.

回答by Hyman Ritter

I'm using gcc as a preprocessor (for html files.) It does just what you want. It expands "#--" directives, then outputs a readable file. (NONE of the other C/HTML preprocessors I've tried do this- they concatenate lines, choke on special characters, etc.) Asuming you have gcc installed, the command line is:

我使用 gcc 作为预处理器(用于 html 文件)。它可以满足您的需求。它扩展“#--”指令,然后输出一个可读文件。(我尝试过的其他 C/HTML 预处理器都没有这样做 - 它们连接行,阻塞特殊字符等。)假设您安装了 gcc,命令行是:

gcc -E -x c -P -C -traditional-cpp code_before.cpp > code_after.cpp

gcc -E -xc -P -C -traditional-cpp code_before.cpp > code_after.cpp

(Doesn't have to be 'cpp'.) There's an excellent description of this usage at http://www.cs.tut.fi/~jkorpela/html/cpre.html.

(不必是'cpp'。)http://www.cs.tut.fi/~jkorpela/html/cpre.html 上有关于这种用法的精彩描述。

The "-traditional-cpp" preserves whitespace & tabs.

“-traditional-cpp”保留空格和制表符。

回答by Andrii Pyvovar

Run:

跑:

gcc -E <file>.c

or

或者

g++ -E <file>.cpp

回答by Pranav Kumar

Suppose we have a file as Message.cpp or a .c file

假设我们有一个 Message.cpp 或 .c 文件

Steps 1:Preprocessing (Argument -E )

步骤 1:预处理(参数 -E)

g++ -E .\Message.cpp > P1

g++ -E .\Message.cpp > P1

P1 file generated has expanded macros and header file contents and comments are stripped off.

生成的 P1 文件扩展了宏,头文件内容和注释被剥离。

Step 2:Translate Preprocessed file to assembly (Argument -S). This task is done by compiler

步骤 2:将预处理文件转换为程序集(参数 -S)。此任务由编译器完成

g++ -S .\Message.cpp

g++ -S .\Message.cpp

An assembler (ASM) is generated (Message.s). It has all the assembly code.

生成汇编程序 (ASM) (Message.s)。它有所有的汇编代码。

Step 3:Translate assembly code to Object code. Note: Message.s was generated in Step2. g++ -c .\Message.s

第 3 步:将汇编代码转换为目标代码。注意:Message.s 是在 Step2 中生成的。 g++ -c .\Message.s

An Object file with the name Message.o is generated. It is the binary form.

生成名为 Message.o 的对象文件。它是二进制形式。

Step 4:Linking the object file. This task is done by linker

第 4 步:链接目标文件。此任务由链接器完成

g++ .\Message.o -o MessageApp

g++ .\Message.o -o MessageApp

An exe file MessageApp.exe is generated here.

这里生成了一个exe文件MessageApp.exe。

#include <iostream>
using namespace std;

 //This a sample program
  int main()
{
cout << "Hello" << endl;
 cout << PQR(P,K) ;
getchar();
return 0;
}