I thought that private methods, which are not used inside its class are removed by the compiler/linker and would not be part of the final binary.
I have created an example class, with a private method which is implemented but not used.
class XXX
{
public:
XXX();
private:
void MyUnusedMethod();
};
And in the implementation file:
void XXX::MyUnusedMethod()
{
const char* hugo = "ABCCHARLYABC";
printf( hugo );
}
After compilation the string still exist in the final binary. Why? And how can I prevent this?
Best regards,
Charly
One portable way is to have a .o file for each function. Then build an archive .a from those .o files. When linking against that archive the linker links in only those .o files that resolve symbols, i.e. the .o files with a function that nobody calls are not linked in.
Another way is to use latest versions of gcc with link-time code generation.