I'm compiling and archiving a library (call it libbar.a
). The key translation units in that library use a function void foo()
defined (non-statically) in a foo.cpp
which also gets compiled and put in the library.
I want to avoid this void foo()
clashing with other symbols elsewhere in my codebase (which uses that library). Now, you could say - just don't include a header which declares void foo()
; but - what if I actually do use the same void foo()
elsewhere? Even though it's more efficient to just have it once in my entire codebase, I actually want whoever uses the library to be oblivious to the implementation detail of void foo()
being used internally. So - I want for nobody to be able to look up that symbol in libbar.a
, but for the code within libbar.a
to still be able to use it.
How can I achieve this?
Notes:
- The code (both for
foo
and my library) is C and/or C++. Answers assuming it's C-only or C++-only are also relevant. - I realize that if I change the name (e.g. to
void bar_foo()
), or putvoid foo()
into a namespace, that could have the desired effect. But I need to not do that, i.e. I need to keep using the same code forvoid foo()
with no changes. I'm only willing to change things in library code which usesvoid foo()
and in the build mechanism. - I'm working with gcc on Linux; I'm hoping something which works with clang or is compiler-agnostic, though. I also automate my build with CMake, but don't bother writing CMake code - just say what you want the build system to do in general terms.