问题
When I build a shared library "mylib.so" from a source file "secret.cc", the resulting shared object contains the source filename:
... do_global_ctors_aux^@secret.cc^@__DTOR_END ...
But I don't want to divulge the name of that file ("secret.cc") to the users of my library. Is there a way to strip the filename information from the shared object, or to prevent it from being inserted in the first place?
回答1:
It's quite simple: Don't let the compiler know the source's filename from the very beginning. Instead of
g++ -std=c++11 -O3 -Wall -c my_source.cc -o my_source.o
do this:
cat my_source.cc | g++ -std=c++11 -O3 -Wall -c -x c++ - -o my_source.o
Note that you need to provide -x c++ explicitly, the error messages obviously won't contain the filename anymore and there is one additional caveat: When your sources contain relative includes, i.e., includes in quotes (#include "foo.hpp") instead of angle brackets (#include <foo.hpp>), those will no longer work as the compiler can't refer to the file's directory, it just sees a byte-stream from a pipe.
来源:https://stackoverflow.com/questions/15688434/remove-embedded-source-filenames-from-shared-libraries