I am building a shared library that contains a bunch of lambdas, and some of those lambdas are created inside other lambdas. But, when I use -fvisibility=hidden and -Wall I get a warning about declarations of something with greater visibility, that I honestly do not understand. I have a minimal example:
#include <memory>
template<class T>
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<int> r; }
If I compile this I get the following warning:
% 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<T>::MyClass()::<lambda()> [with T = int]::<lambda()>’:
visibility_test.cpp:13:22: required from ‘MyClass<T>::MyClass()::<lambda()> [with T = int]’
visibility_test.cpp:11:23: required from ‘struct MyClass<T>::MyClass() [with T = int]::<lambda()>’
visibility_test.cpp:11:14: required from ‘MyClass<T>::MyClass() [with T = int]’
visibility_test.cpp:22:27: required from here
visibility_test.cpp:13:32: warning: ‘MyClass<T>::MyClass()::<lambda()> [with T = int]::<lambda()>’ declared with greater visibility than the type of its field ‘MyClass<T>::MyClass()::<lambda()> [with T = int]::<lambda()>::<this capture>’ [-Wattributes]
auto fn = [this] { /*Do something useful here */ };
If I change the #if 0 to #if 1, thereby moving the creation of fn to outside the "outer" lambda it all compiles fine.
This warning started appearing when I installed GCC 6 on my Arch box. I get it when compiling with 6.3.1 and 7.1.1.
So, my questions are:
Update: So, I have accepted that this is a bug in GCC, and I now wanted to get rid of the warning with minimal side effects. So I added "__attribute__ ((visibility ("default")))" to the constructor of MyClass, which appears to work nicely.
Looks like it's a bug in gcc.
There is bug report and there were same warnings earlier without lambdas. You can handle this with using -fvisibility
default, or manually setupping visibility to hidden/default by attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With