In a bit of serialization code for a project I\'m working on I have a type whose size is compiler dependent. In order to deal with this, I decided to use a template specializati
This is a peculiar problem. I looked into it a little, and this issue is unrelated to template specialization. I guess g++ doesn't, by default, strip unused symbols. This makes sense in case you later want to link your output to another program.
However, there are command line options that you can use to strip unused symbols. For details, see this post:
How to remove unused C/C++ symbols with GCC and ld?
but also see here
Using GCC to find unreachable functions ("dead code")
and here
Dead code detection in legacy C/C++ project
Just to try this out, I modified the code as follows:
#include
void junk_function() {
std::cout<<"test" << std::endl;
}
template
void special_function()
{
std::cout << "Called without specialization: " << size << std::endl;
}
template <>
void special_function<4>()
{
std::cout << "dword" << std::endl;
}
template <>
void special_function<8>()
{
std::cout << "qword" << std::endl;
}
int main()
{
special_function();
return 0;
}
Then stored this code to sp.cpp. First,
g++ -Os sp.cpp -o sp
nm sp
and got this (note, I removed a bunch of symbols for readability):
0804879a T _Z13junk_functionv
080487b8 T _Z16special_functionILi4EEvv
080487f5 T _Z16special_functionILi8EEvv
Seems the two unused symbols are there. I also tried -O1, -O2, -O3, and got the same.
Next:
g++ -Os -fdata-sections -ffunction-sections sp.cpp -o sp -Wl,--gc-sections
nm sp
and got this:
0804875a T _Z16special_functionILi4EEvv
That's it. So it looks like you just need to pass the right arguments in to tell g++ to strip unused symbols. On mac, I guess they have the -dead_strip option, but I don't know why it doesn't work in g++ (even though it is mentioned in the man pages. Admittedly, I didn't dig into this, so there may be a fine print that I missed).
I think Visual C++'s linker strips by default when you link, but I didn't test. Maybe someone else can chime in.