Remove embedded source filenames from shared libraries

江枫思渺然 提交于 2019-12-11 11:46:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!