我有一个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*)
非指针重载是弱全局的,但指针重载是局部的。
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*)
显然,lang正在做同样的事情,但没有告诉我们原因。
一个满足g ++而另一个不满足g ++的重载之间的区别是int
和之间的区别int *
。在此基础上,我们希望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)
c也是如此:
$ 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声明成为全局符号;不正常的变为本地,只有前者是动态可见的:
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
并返回该参数。实际上,它声明了一个模板实例化函数,该函数接受type参数U
并返回type:
U __attribute__((visibility("default")))
允许用于U
= int
,但不允许用于U
= int *
。
为了表达您的意图,即template<typename T> T foobar(T arg)
实例化应为动态可见的函数,请使用visible属性来限定模板函数本身的类型。根据GCC的语法文档__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可获得相同的结果
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句