作者:多米音乐_54101533 | 来源:互联网 | 2023-02-01 23:40
我正在构建一个包含一堆lambdas的共享库,其中一些lambdas是在其他lambdas中创建的.但是,当我使用-fvisibility = hidden和-Wall时,我会收到有关具有更高可见性的声明的警告,我真的不明白.我有一个最小的例子:
#include
template
class MyClass {
public:
MyClass() {
#if 0
auto fn = [this] { /*Do something useful here*/ };
auto outer = [this,fn]() { /*use fn for something here*/ };
#else
auto outer = [this]()
{
auto fn = [this] { /*Do something useful here */ };
//use fn for something here
};
#endif
/* use outer for something */
}
};
int main() { MyClass r; }
如果我编译这个,我得到以下警告:
% g++ -Wall -fvisibility=hidden -Wno-unused-but-set-variable -o visibility_test.cpp.o -c visibility_test.cpp
visibility_test.cpp: In instantiation of ‘struct MyClass::MyClass():: [with T = int]::’:
visibility_test.cpp:13:22: required from ‘MyClass::MyClass():: [with T = int]’
visibility_test.cpp:11:23: required from ‘struct MyClass::MyClass() [with T = int]::’
visibility_test.cpp:11:14: required from ‘MyClass::MyClass() [with T = int]’
visibility_test.cpp:22:27: required from here
visibility_test.cpp:13:32: warning: ‘MyClass::MyClass():: [with T = int]::’ declared with greater visibility than the type of its field ‘MyClass::MyClass():: [with T = int]::::’ [-Wattributes]
auto fn = [this] { /*Do something useful here */ };
如果我将#if 0更改为#if 1,从而将fn的创建移到"外部"lambda之外,那么所有编译都很好.
当我在Arch框上安装GCC 6时,此警告开始出现.我在使用6.3.1和7.1.1进行编译时得到它.
所以,我的问题是:
这个警告试图告诉我什么?
如何摆脱警告而不必过多地违反我的代码(像我的例子中那样移动lambda不是一个真正的选择.)
更新:所以,我已经接受这是GCC中的一个错误,我现在想要摆脱警告,副作用最小.所以我将"__attribute __((visibility("default")))"添加到MyClass的构造函数中,它看起来效果很好.
1> ForEveR..:
看起来这是gcc中的一个错误.
有错误报告,没有lambdas,早期有相同的警告.您可以使用-fvisibility
默认值或手动设置隐藏/默认属性的可见性来处理此问题.