I'm compiling a C++ static library using g++
via Cmake. I want to remove symbols relating to the internal implementation so they don't show up in nm
. (See here and here for the same with shared libraries.)
This answer tells you how to do it on iOS, and I'm trying to understand what happens under the hood so I can replicate on Linux. They invoke ld
with:
-r
/--relocatable
to Generate relocatable output---i.e., generate an output file that can in turn serve as input to ld.-x
/--discard-all
: Delete all local symbols.
AFAICS the -r
glues all the modules into one module, and then the -x
removes symbols only used inside that module. Is that right?
It's not clear how the linker 'knows' which symbols will be exported externally? Does it rely on __attribute__((visibility("hidden/default")))
as in the .so
case?
Edit: clearly I'm confused... I thought cmake invoked ld
to link the .o
s into .a
. Googled + clarified above.
Question still stands: how do I modify the build process to exclude most symbols?