为什么只有一些 C++ 模板实例导出到共享库中?

Why are only some of these C++ template instantiations exported in a shared library?

我有一个 C++ 动态库(在 macOS 上),它有一个模板函数,带有一些在 public API 中导出的显式实例化。客户端代码只能看到模板声明;他们不知道里面发生了什么,并依赖这些实例化在 link 时间可用。

出于某种原因,只有部分显式实例在动态库中可见。

这是一个简单的例子:

// libtest.cpp
#define VISIBLE __attribute__((visibility("default")))

template<typename T> T foobar(T arg) {
    return arg;
}

template int  VISIBLE foobar(int);
template int* VISIBLE foobar(int*);

我希望两个实例都可见,但只有非指针实例是:

$ clang++ -dynamiclib -O2 -Wall -Wextra -std=c++1z -stdlib=libc++ -fvisibility=hidden -fPIC libtest.cpp -o libtest.dylib
$ nm -gU libtest.dylib | c++filt
0000000000000f90 T int foobar<int>(int)

此测试程序无法link,因为缺少指针一:

// client.cpp
template<typename T> T foobar(T);  // assume this was in the library header

int main() {
    foobar<int>(1);
    foobar<int*>(nullptr);
    return 0;
}
$ clang++ -O2 -Wall -Wextra -std=c++1z -stdlib=libc++ -L. -ltest client.cpp -o client
Undefined symbols for architecture x86_64:
  "int* foobar<int*>(int*)", referenced from:
      _main in client-e4fe7d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

类型和可见性之间似乎确实存在某种联系。如果我将 return 类型更改为 void,它们都是可见的(即使模板参数仍然是指针或其他)。特别奇怪的是,这同时导出:

template auto VISIBLE foobar(int)  -> int;
template auto VISIBLE foobar(int*) -> int*;

这是一个错误吗?为什么 apparent syntactic sugar 会改变行为?

如果我将模板定义更改为可见,它会起作用,但它似乎不理想,因为只应导出这些实例中的一些......我仍然想了解为什么会发生这种情况,无论哪种方式。

我正在使用 Apple LLVM 版本 8.0.0 (clang-800.0.42.1)。

您的问题在 linux 上可重现:

$ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix

$ clang++ -shared -O2 -Wall -Wextra -std=c++1z -fvisibility=hidden \
-fPIC libtest.cpp -o libtest.so

$ nm -C libtest.so | grep foobar
0000000000000620 W int foobar<int>(int)
0000000000000630 t int* foobar<int*>(int*)

非指针重载是弱全局的,但指针重载是 本地。

造成这种情况的原因被 clang 对 __attribute__ 的松弛诊断所掩盖 语法扩展,毕竟是 GCC 的发明。如果我们编译 g++ 而不是我们得到:

$ g++ -shared -O2 -Wall -Wextra -std=c++1z -fvisibility=hidden -fPIC libtest.cpp -o libtest.so
libtest.cpp:9:36: warning: ‘visibility’ attribute ignored on non-class types [-Wattributes]
 template int * VISIBLE foobar(int *);
                                    ^

注意 g++ 仅在指针重载时忽略可见性属性, 并且,就像 clang - 并且与该警告一致 - 它发出代码:

$ nm -C libtest.so | grep foobar
0000000000000610 W int foobar<int>(int)
0000000000000620 t int* foobar<int*>(int*)

很明显 clang 也在做同样的事情,但没有告诉我们原因。

用1和满足g++的重载的区别 另一个不满意的是intint *的区别。 在此基础上,我们希望 g++ 对更改感到满意:

template int  VISIBLE foobar(int);
//template int * VISIBLE foobar(int *);
template float VISIBLE foobar(float);

原来如此:

$ g++ -shared -O2 -Wall -Wextra -std=c++1z -fvisibility=hidden -fPIC libtest.cpp -o libtest.so
$ nm -C libtest.so | grep foobar
0000000000000650 W float foobar<float>(float)
0000000000000640 W int foobar<int>(int)

叮当也是:

$ clang++ -shared -O2 -Wall -Wextra -std=c++1z -fvisibility=hidden -fPIC libtest.cpp -o libtest.so
$ nm -C libtest.so | grep foobar
0000000000000660 W float foobar<float>(float)
0000000000000650 W int foobar<int>(int)

它们都会做你想用 T 非指针类型的重载,但是 不是 T 指针类型。

然而,您在这里面临的是不是禁止动态可见函数 return 指针而不是非指针。如果 visibility 就这么坏了。这只是对以下形式的类型的禁令:

D __attribute__((visibility("...")))

其中 D 是指针或引用类型,与以下形式的类型不同:

E __attribute__((visibility("..."))) *

或:

E __attribute__((visibility("..."))) &

其中 E 不是 指针或引用类型。区别在于:

  • 要键入的(具有可见性的指针或引用...) D

和:

  • 具有可见性的(指向类型E的指针或引用)...

参见:

$ cat demo.cpp 
int  xx ;
int __attribute__((visibility("default"))) * pvxx; // OK
int * __attribute__((visibility("default"))) vpxx; // Not OK
int __attribute__((visibility("default"))) & rvxx = xx; // OK,
int & __attribute__((visibility("default"))) vrxx = xx; // Not OK


$ g++ -shared -Wall -Wextra -std=c++1z -fvisibility=hidden -o libdemo.so demo.cpp 
demo.cpp:3:46: warning: ‘visibility’ attribute ignored on non-class types [-Wattributes]
 int * __attribute__((visibility("default"))) vpxx; // Not OK
                                              ^
demo.cpp:5:46: warning: ‘visibility’ attribute ignored on non-class types [-Wattributes]
 int & __attribute__((visibility("default"))) vrxx = xx; // Not OK
                                          ^

$ nm -C libdemo.so | grep xx
0000000000201030 B pvxx
0000000000000620 R rvxx
0000000000201038 b vpxx
0000000000000628 r vrxx
0000000000201028 b xx

OK 声明成为全局符号;不好的变成本地的, 只有前者是动态可见的:

nm -CD libdemo.so | grep xx
0000000000201030 B pvxx
0000000000000620 R rvxx

这种行为是合理的。我们不能指望编译器归因于 指针或引用的全局动态可见性 引用不具有全局或动态可见性的内容。

这种合理的行为只会让您的 objective 感到沮丧,因为 - 正如您现在可能看到的那样:

template int  VISIBLE foobar(int);
template int* VISIBLE foobar(int*);

并不代表您认为的那样。你认为,对于给定的类型 U

template U VISIBLE foobar(U);

声明了一个具有default的模板实例化函数 可见性,接受类型为 U 和 return 的参数相同。实际上, 它声明了一个接受参数的模板实例化函数 输入 U 和 returns 输入:

U __attribute__((visibility("default")))

允许 U = int,但不允许 U = int *

表达你的意图 template<typename T> T foobar(T arg) 应该是动态可见的函数,限定模板函数的类型 本身具有可见性属性。根据 GCC __attribute__ 的文档 syntax - 无可否认 没有说任何关于模板的具体内容——你必须创建一个属性 函数在其定义之外的声明中的限定。如此遵守 有了它,你会像这样修改你的代码:

// libtest.cpp
#define VISIBLE __attribute__((visibility("default")))

template<typename T> T foobar(T arg) VISIBLE;

template<typename T> T foobar(T arg) {
    return arg;
}

template int  foobar(int);
template int* foobar(int*);

g++ 不再有任何问题:

$ g++ -shared -O2 -Wall -Wextra -std=c++1z -fvisibility=hidden -fPIC libtest.cpp -o libtest.so
$ nm -CD libtest.so | grep foobar
0000000000000640 W int foobar<int>(int)
0000000000000650 W int* foobar<int*>(int*)

并且两个重载都是动态可见的。 clang也一样:

$ clang++ -shared -O2 -Wall -Wextra -std=c++1z -fvisibility=hidden -fPIC libtest.cpp -o libtest.so
$ nm -CD libtest.so | grep foobar
0000000000000650 W int foobar<int>(int)
0000000000000660 W int* foobar<int*>(int*)

运气好的话,你会在 Mac OS

上用 clang 得到相同的结果