简体   繁体   English

GCC 6.x关于Lambda可见性的警告

[英]GCC 6.x warning about lambda visibility

I am building a shared library that contains a bunch of lambdas, and some of those lambdas are created inside other lambdas. 我正在构建一个包含一堆lambda的共享库,其中一些lambda在其他lambda内部创建。 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. 但是,当我使用-fvisibility = hidden和-Wall时,会收到有关声明具有更高可见性的警告,但我真的不明白。 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. 如果我将#if 0更改为#if 1,从而将fn的创建移到“外部” lambda之外,则所有编译都可以。

This warning started appearing when I installed GCC 6 on my Arch box. 当我在Arch框中安装GCC 6时,此警告开始出现。 I get it when compiling with 6.3.1 and 7.1.1. 我在使用6.3.1和7.1.1进行编译时得到了它。

So, my questions are: 因此,我的问题是:

  1. What is this warning trying to tell me? 这个警告想告诉我什么?
  2. How do I get rid of the warning without having to violate my code too much (moving the lambdas like in my example is not really an option.) 如何摆脱警告,而不必过多地违反我的代码(像我的示例中那样移动lambda并不是一种选择。)

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. 更新:所以,我已经接受了这是GCC中的错误,我现在想摆脱副作用最小的警告。 So I added "__attribute__ ((visibility ("default")))" to the constructor of MyClass, which appears to work nicely. 因此,我在MyClass的构造函数中添加了“ __attribute__((visibility(” default“)))”“,它看起来很不错。

Looks like it's a bug in gcc. 看起来这是gcc中的错误。

There is bug report and there were same warnings earlier without lambdas. 错误报告,并且之前有同样的警告没有lambda。 You can handle this with using -fvisibility default, or manually setupping visibility to hidden/default by attribute. 您可以使用-fvisibility default或通过属性手动将可见性设置为hidden / default来处理此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM