I'm interested in trying to see the assembly code for my header file. Playing around with a simple .cpp file with a dummy main method and compiling it is easy: g++ -S -o prog.exe main.cpp
However, I'm going into header files now. I have a dummy header file func.hpp
that just contains a dummy method and now I want to be able to compile it and see its assembly in a .S file. (The main reason for this is so that if I have a more complicated function, maybe I can do some manual optimization in assembly and just build the final executable through g++ -o prog.exe func.S main.cpp
.
However, I am not able to find sources that explain this. If I try g++ -S -o func.S func.hpp
I get an error message output filename specified twice
. Can anyone help me with this?
For reference, here is func.hpp:
int number()
{
return 0;
}
I'm also trying it out with another hpp file named struct.hpp:
struct Coord
{
int x;
int y;
};
Same error for both, but I want to be able to see the assembly for both headers without converting them to .cpp files (because that breaks the meaning of a header file.)
You could force g++ to treat the .hpp file as a C++ source file.
g++ -x c++ -S -o func.S func.hpp
The reason you get that "output filename specified twice" message is because, when passing a header file to gcc, it will assume you want to compile it into a pre-compiled header. Just as named, this will "compile" the headers, so a *.s file will be generated in the middle. If you pass the -v
flag with the original command, you will see that cc1plus is called like:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.1/cc1plus
-quiet
-v
-D_GNU_SOURCE
func.hpp
-quiet
-dumpbase func.hpp
-mtune=generic
-march=x86-64
-auxbase-strip func.S
-version
-o func.S # <---
-o /tmp/ccxEg3J7.s # <---
--output-pch= func.S
So indeed -o
is specified twice, once from your -o
and once from the compiler.
(Of course the compiler could avoid adding the -o /tmp/ccxEg3J7.s
, but the added complexity for this edge case isn't quite worth it.)