GCC ignores function attributes

Nikos Chantziaras realnc@gmail.com
Thu Jun 12 21:35:00 GMT 2014


On 12/06/14 12:30, Jonathan Wakely wrote:
> On 12 June 2014 01:42, Nikos Chantziaras <realnc@gmail.com> wrote:
>> On 12/06/14 03:31, Jonathan Wakely wrote:
>>>
>>> On 11 June 2014 23:36, Nikos Chantziaras wrote:
>>>>
>>>> According to the documentation:
>>>>
>>>> https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Function-Attributes.html#Function-Attributes
>>>>
>>>> and more specific, the example about the visibility attribute:
>>>>
>>>>     void __attribute__ ((visibility ("protected")))
>>>>     f () { /* Do something. */; }
>>>>
>>>> doesn't work for me. I have this C++ function prototype:
>>>>
>>>>     const SDL_AudioSpec& __attribute__((visibility("default")))
>>>>     spec();
>>>>
>>>> Compiling this with g++ 4.8.2 gives me:
>>>>
>>>>     warning: 'visibility' attribute ignored on non-class types
>>>
>>> I think the attribute binds to the return type here, which is a
>>> reference (not a class type) so can't be given visibility.
>>>
>>> This applies the attribute to the function, not the return type:
>>>
>>> const SDL_AudioSpec& spec() __attribute__((visibility("hidden")));
>>
>>
>> I've found that this also works:
>>
>> __attribute__((visibility("hidden"))) const SDL_AudioSpec& spec();
>>
>> The only one that doesn't, is the documented one :-/  Documentation bug?
>
> No, the example in the documentation is a function with a void return,
> and in that case the attribute binds to the function not the return
> type.

That can't be. This works:

   int __attribute__((visibility("hidden"))) spec();

spec() is indeed hidden, so the attribute is not applied to the return 
type at all (if that was even possible; how can you hide a return type.)



More information about the Gcc-help mailing list