为什么在共享库中仅导出其中一些C++模板实例化?



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

出于某种原因,只有其中一些显式实例化在动态库中可见。

下面是一个简单的示例:

// 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)

此测试程序无法链接,因为缺少指针:

// 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)

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

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

这是一个错误吗?为什么明显的句法糖会改变行为?

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

我使用的是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*)

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

造成这种情况的原因被叮叮当对__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*)

显然,叮当也在做同样的事情,但没有告诉我们为什么。

满足 g++ 的重载与 1 和 与另一个不满意的是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指针类型。

但是,您在这里面临的并不是禁止动态可见的功能。 返回指针而不是非指针。它不可能逃脱注意,如果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 声明变为全局符号;Not OK 声明变为局部符号, 只有前者是动态可见的:

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

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

这种合理的行为似乎只会挫败您的目标,因为 - 正如你现在可能看到的:

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

并不意味着你认为它做了什么。你认为,对于给定的类型U

template U VISIBLE foobar(U);

声明具有default的模板实例化函数 可见性,接受类型U的参数并返回相同的参数。事实上 它声明一个模板实例化函数,该函数接受 键入U并返回类型:

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

这在U=int中是允许的,但在U=int *时是不允许的。

为了表达您的意图,即template<typename T> T foobar(T arg)的瞬间 应为动态可见函数,限定模板函数的类型 本身具有可见性属性。根据海湾合作委员会对__attribute__的文件 语法 - 诚然 没有说任何关于模板的具体内容 - 你必须创建一个属性 声明中函数的限定,而不是其定义。所以遵守 有了它,你可以像这样修改你的代码:

// 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++ -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获得相同的结果

最新更新