Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gcc hide visibility of symbols coming from included static library

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

like image 978
Mauro Avatar asked Jul 28 '15 08:07

Mauro


2 Answers

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

like image 199
m.s. Avatar answered Sep 17 '22 21:09

m.s.


-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.

like image 21
Matthijs Avatar answered Sep 21 '22 21:09

Matthijs