且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从共享库中删除嵌入的源文件名

更新时间:2023-12-04 20:58:10

很简单:不要让编译器从一开始就知道源文件名。取代

  g ++ -std = c ++ 11 -O3 -Wall -c my_source.cc -o my_source.o 

执行此操作:



cat my_source.cc | g ++ -std = c ++ 11 -O3 -Wall -c -x c ++ - -o my_source.o

请注意,你需要明确提供 -x c ++ ,错误消息显然不会包含文件名,还有一个额外的警告:当你的源包含相对包含,即,包括在引号( #includefoo.hpp)中,而不是尖括号( #include< foo.hpp> ),这些将不再工作,因为编译器不能引用该文件的目录,它只是看到一个字节流从管道。


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?

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.