简体   繁体   English

如何在Linux中编译后以这样的方式编译c / cpp代码以提供经过预处理的汇编文件和目标文件?

[英]how to compile c/cpp code in such a way that it gives preprocessed assembled and object file after compilation in linux?

if source file is source.cpp then compiler output should have source.i source.s source.o in my directory not only .o file. 如果源文件是source.cpp,则编译器输出在我的目录中不仅应包含.o文件,还应具有source.i source.s source.o。

where 哪里

  1. preprocessed = source.i 预处理= source.i
  2. assembly = source.s 汇编= source.s
  3. object = source.o 对象= source.o

i know first two files are being created but later on they got deleted only .o file is shown so that linker can link object file but i want to see those two files also. 我知道正在创建前两个文件,但后来它们仅被删除。o文件显示出来,以便链接器可以链接目标文件,但我也想看到这两个文件。

for linux any flag or something?. 对于Linux的任何标志或东西?

According to the gcc man pages 根据gcc手册页

-save-temps -save-temps=cwd Store the usual "temporary" intermediate files permanently; -save-temps -save-temps = cwd永久存储通常的“临时”中间文件; place them in the current directory and name them based on the source file. 将它们放在当前目录中,并根据源文件命名。 Thus, compiling foo.c with -c -save-temps would produce files foo.i and foo.s, as well as foo.o. 因此,用-c -save-temps编译foo.c将产生文件foo.i和foo.s以及foo.o。 This creates a preprocessed foo.i output file even though the compiler now normally uses an integrated preprocessor. 即使编译器现在通常使用集成的预处理器,这也会创建预处理的foo.i输出文件。

so you should compile your code like this 所以你应该像这样编译你的代码

g++ -save-temps source.cpp

You can create individual file for each stage of compiler. 您可以为编译器的每个阶段创建单个文件。

Preprocessor : g++ -E file.cpp -o file.i 预处理程序: g++ -E file.cpp -o file.i

Translator : g++ -S file.i -o file.s 译者: g++ -S file.i -o file.s

Assembler : g++ -c file.s -o file.o 汇编器: g++ -c file.s -o file.o

linker : g++ file.o -o file 链接器: g++ file.o -o file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM