我正在 gcc 下使用 -fvisibility=hidden 编译以下代码:
template<class T> struct /*__attribute__ ((visibility("default")))*/ A {};
template<class T> struct B
{
B() __attribute__ ((visibility("default")));
};
template<class T> B<T>::B() {}
template class B<int>;
template class B<A<int> >;
如果我通过 nm | 运行生成的对象文件grep B,我得到
000000000002b97c t B<A<int> >::B()
000000000002b972 t B<A<int> >::B()
000000000002b968 T B<int>::B()
000000000002b95e T B<int>::B()
即, B
可见,但 B; >
是不可见的。 B;如果我取消注释将 A
标记为可见的代码段,>
就会变得可见。但是,我不想将所有 A 标记为可见,因为在实际代码中 A
包含大量应保持私有的方法。
为什么A
的可见性会影响B的可见性? >
?我可以制作B吗? >
可见而不使所有 A
可见?
I'm compiling the following code under gcc with -fvisibility=hidden:
template<class T> struct /*__attribute__ ((visibility("default")))*/ A {};
template<class T> struct B
{
B() __attribute__ ((visibility("default")));
};
template<class T> B<T>::B() {}
template class B<int>;
template class B<A<int> >;
If I run the resulting object file through nm | grep B, I get
000000000002b97c t B<A<int> >::B()
000000000002b972 t B<A<int> >::B()
000000000002b968 T B<int>::B()
000000000002b95e T B<int>::B()
I.e., B<int>
is visible but B<A<int> >
is invisible. B<A<int> >
becomes visible if I uncomment the snippet marking A<T>
as visible. However, I do not want to mark all of A visible, since in the real code A<T>
contains a vast number of methods which should remain private.
Why does the visibility of A<T>
affect the visibility of B<A<T> >
? Can I make B<A<T> >
visible without making all of A<T>
visible?
发布评论
评论(2)
假设我正确理解了 ODR(我可能没有:)),隐藏您的
B >
实例化看起来像是 ODR 相关的需求。如果B >
未隐藏,A<>
成员的多个实例可能存在并被引用,从而导致 ODR 违规。 GCC 的 符号可见性 Wiki 简要描述了使用隐藏符号可见性时的此类违规行为具有模糊链接的“实体”,包括模板(请参阅有关例外的部分)。您想通过隐藏
A<>
模板中的符号来实现什么目的?Assuming I understand the ODR correctly (I likely do not :)), hiding your
B<A<int> >
instantatiation looks like an ODR related requirement. IfB<A<int> >
was not hidden it would be possible for multiple instantations ofA<>
's members to exist and be referenced, resulting in an ODR violation. GCC's symbol visibility Wiki briefly describes such violations when using hidden symbol visibility on "entities" with vague linkage, including templates (see the section on exceptions).What are you trying to achieve by hiding the symbols in your
A<>
template?如果您特别希望隐藏 A<> 的内联方法尝试使用
-fvisibility-inlines-hidden
If you specifically wish to hide the inline methods of A<> try using
-fvisibility-inlines-hidden