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.
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
-vflag with the original command, you will see that cc1plus is called like:So indeed
-ois specified twice, once from your-oand 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.)