Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Think of the following situation: I have a project with a main directory and a subdirectory with the name subDir and 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.c only one intermediate file with the name file.i will be generated.

That is the expected behaviour, as the man file of gcc tells me, that the intermediate files will always be placed in the current path, when using -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.

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

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

like image 750
Customizer Avatar asked Jan 29 '10 20:01

Customizer


People also ask

What is GCC save temps?

Instructs the compiler to generate intermediate files in various formats from the specified C or C++ file.

What does save temps do?

Instructs the compiler to generate intermediate assembly files from the specified C/C++ file. It is similar to disassembling object code that has been compiled from C/C++.


2 Answers

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 -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.*

like image 178
qbert220 Avatar answered Oct 18 '22 03:10

qbert220


There's no problem with file.cpp / file.c in different directories. GCC will create a *.ii and a *.i depending on the files' extension.

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

like image 39
tur1ng Avatar answered Oct 18 '22 03:10

tur1ng