7

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

4

2 回答 2

9

您还需要使用 flag编译libStatic.a-fvisibility=hidden

于 2015-07-28T08:53:56.200 回答
2

-fvisibility=hidden仅影响编译器生成的符号的默认可见性,它不会修改现有符号的可见性,例如从静态库获得的符号。

幸运的是,链接器确实有一个标志可以做到这一点:用于-Wl,--exclude-libs=ALL将全局符号的可见性从静态库更改为“隐藏”,从而防止它们被共享库导出。

于 2021-03-04T01:10:55.987 回答