Question

I have the following enum:

__atttribute__((visibility ("default") )) enum MSG
{
    OK,
    FAIL,
};

When I compile, it gives me the warning:

warning: attribute ignored in declaration of ‘enum MSG’
warning: attribute for ‘enum MSG’ must follow the ‘enum’ keyword

When I put the attribute after the enum, I get the following errors:

warning: type attributes are honored only at type definition
error: use of enum ‘MSG’ without previous declaration
error: expected unqualified-id before ‘{’ token

Can anyone tell me how to fix this?

Was it helpful?

Solution

The visibility attribute applies to symbols like functions and variables. A definition of an enumeration type that doesn't contain a variable name doesn't create any symbols.

Enumeration type without a variable:

enum msg { OK, FAIL };

An enumeration variable:

enum msg message;

Enumeration type together with a variable:

enum msg { OK, FAIL } message;

In the first case, there's no symbol the visibility attribute could affect at all.

OTHER TIPS

You can fix this by declaring the type of your enum like this:

enum class MSG : std::uint32_t __atttribute__((visibility ("default") ))
{
    OK,
    FAIL,
};

Though it seems like this is a bug in GCC that was fixed in versions 6+. Related SO post

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top