Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: warning: '...' declared with greater visibility than the type of its field '...::<anonymous>'

I'm getting these two warnings (with GCC 4.2 on MacOSX):

/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154:0 /Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154: warning: 'startMainLockDetector()::MainLockDetector' declared with greater visibility than the type of its field 'startMainLockDetector()::MainLockDetector::<anonymous>'

/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154:0 /Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154: warning: 'startMainLockDetector()::MainLockDetector' declared with greater visibility than its base 'Action'

In this code:

struct Action {
    virtual ~Action() {}
    virtual int handle() = 0;
};


static void startMainLockDetector() {
    /* ... */

    struct MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };

    /* ... */
}

I'm not exactly sure what these warnings mean (what visibility?) and how to fix them. (I really want the class MainLockDetector to be local for that function only.)

I have already compiled the same code with a lot of other compilers (clang, GCC 3.*, GCC 4.0, GCC 4.4, etc) and never got any warning for this code.

like image 740
Albert Avatar asked May 13 '10 17:05

Albert


2 Answers

To fix this problem, try one of below.

  1. Use #pragma GCC visibility push() statement like this.

    #pragma GCC visibility push(hidden)
    struct MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };
    #pragma GCC visibility pop
    
  2. Use __attribute__ ((visibility("hidden"))) like this.

    struct __attribute__ ((visibility("hidden"))) MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };
    
  3. Add the command line option -fvisibility=default.

For more details, refer http://gcc.gnu.org/wiki/Visibility.

like image 76
Satachito Avatar answered Nov 15 '22 10:11

Satachito


There seems to be a problem with shared libraries. I assume you are writing a shared library. Look at this explanation. Try to add the command line option -fvisibility-inlines-hidden. The problem seems to be, that gcc tries to export some symbols of MainLockDetector (visible to linking executables and libraries), whereas Action is not exported (invisible to linking executables and libraries). So the visibility of MainLockDetector really is higher than the visibility of Action.

like image 25
ablaeul Avatar answered Nov 15 '22 12:11

ablaeul