8

I have:

  1. a shared library, say libShared.so, which contains a class Bar, with a method int Bar::do(int d) const
  2. a static library, say libStatic.a, which contains a class Foo, with a method int Foo::act(int a) const.

The code of Bar is something like this:

//Bar.h
class __attribute__ ((visibility ("default"))) Bar
{
  private: 
    __attribute__ ((visibility ("hidden"))) int privateMethod(int x) const;
  public:
    Bar() {}
    int do(int d) const;
}

//Bar.cpp
#include "Bar.h"
#include "Foo.h"

int Bar::do(int d) const {
   Foo foo;
   int result = foo.act(d) + this->privateMethod(d);
   return result;
}

libShared.so is compiled with flag -fvisibility=hidden.

The problem is the following: I execute Linux command nm -g -D -C --defined-only libShared.so, and it results that class Foo, along with its method, is visible outside libShared.so, despite having told to the compiler to hide everything except what is marked as "public" (infact, they are marked as "T" by nm).

How can I avoid this? I want libShared.so not to expose symbols coming from its dependencies.

Thanks

Mauro
  • 113
  • 1
  • 5

2 Answers2

9

You need to compile libStatic.a also with flag -fvisibility=hidden.

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • sorry, but doing so won't prevent _libShared.so_ to use _Foo_? – Mauro Jul 28 '15 at 09:34
  • 4
    I answer myself. It works! I suppose this is because a static linkage doesn't support symbols hiding, so the _hidden_ flag will take effect only when the objects in static library are used inside a shared one. Thanks a lot! – Mauro Jul 28 '15 at 09:39
  • Please modify your answer with the cimments insight about hiding symbols in static and dynamic libs. Had discovered it just now – Aviv Jan 14 '20 at 07:49
3

-fvisibility=hidden only affects the default visibility of symbols generated by the compiler, it does not modify the visibility of existing symbols such as those obtained from static libraries.

Fortunately the linker does have a flag to do exactly that: Use -Wl,--exclude-libs=ALL to change the visibility of global symbols from static libraries to "hidden", thus preventing them from being exported by your shared library.

Matthijs
  • 704
  • 7
  • 8