C++ 带有参数“-S -save-temps”的gcc将中间文件放在当前目录中

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

gcc with parameters "-S -save-temps" puts intermediate files in current directory

c++cgcccompiler-constructiong++

提问by Customizer

The parameters -S -save-tempswork fine, as long as i don't use them on files with the same name.

参数-S -save-temps工作正常,只要我不在同名文件上使用它们。

Think of the following situation: I have a project with a main directory and a subdirectory with the name subDirand in both of the directories are files placed with the name file.c. If I now call gcc -S -save-temps file.cpp subDir/file.conly one intermediate file with the name file.iwill be generated.

考虑以下情况:我有一个项目,其中有一个主目录和一个带有名称的子目录,subDir并且在这两个目录中都放置了名称为 的文件file.c。如果我现在gcc -S -save-temps file.cpp subDir/file.c只调用一个具有该名称的中间文件,file.i则会生成一个。

That is the expected behaviour, as the man file of gcctells me, that the intermediate files will always be placed in the current path, when using -save-temps.

这是预期的行为,正如 man 文件gcc告诉我的那样,当使用-save-temps.

My problem is, that I'm working on projects I don't know in advance. It could very well be, that someone constructed the above mentioned example in his Makefiles. In that case I would be stuck, because I need both intermediate files.

我的问题是,我正在从事我事先不知道的项目。很可能有人在他的 Makefile 中构建了上述示例。在那种情况下,我会被卡住,因为我需要两个中间文件。

A few words to the system I'm constructing (for better understanding): My tool uses make --just-printto collect the calls, a make file of a project invokes. I scan these calls for compiler calls and add the -save-tempsand -Soptions. The purpose is to get every preprocessed file, that is used in the process of compiling the project.

对我正在构建的系统说几句话(为了更好地理解):我的工具用于make --just-print收集调用,一个项目调用的 make 文件。我扫描这些调用以获取编译器调用并添加-save-temps-S选项。目的是获取在编译项目过程中使用的每个预处理文件。

Do you have any ideas, how I'm able to get every preprocessed file, even if the above mentioned example should appear?

你有什么想法,即使上面提到的例子应该出现,我如何能够获得每个预处理文件?

采纳答案by tur1ng

There's no problem with file.cpp / file.c in different directories. GCC will create a *.iiand a *.idepending on the files' extension.

file.cpp/file.c在不同目录下没有问题。GCC 将根据文件的扩展名创建 a*.ii和 a *.i

If they both have c||cpp you can use -Eand receive only one *.iwhere you can search for the pragma # 1 "<FILE_PATH>"and extract it via a script.

如果它们都有 c||cpp,您只能使用-E和接收一个*.i,您可以在其中搜索编译指示# 1 "<FILE_PATH>"并通过脚本提取它。

回答by qbert220

In gcc 4.5 you can use the option "-save-temps=obj" when using the -o option. This saves the intermediate files in the same directory as the output file, which can help prevent issues when you have the same filename using different source and output directories.

在 gcc 4.5 中,您可以在使用 -o 选项时使用选项“-save-temps=obj”。这会将中间文件保存在与输出文件相同的目录中,这有助于防止在使用不同的源目录和输出目录具有相同文件名时出现问题。

gcc -save-temps=obj -c dir1/foo.c -o dir1/foo.o
gcc -save-temps=obj -c dir2/foo.c -o dir2/foo.o

The intermediate files will be saved as dir1/foo.*and dir2/foo.*

中间文件将保存为dir1/foo.*dir2/foo.*