GCC ignores function attributes
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Jun 12 09:57:00 GMT 2014
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. Your function does not have a void return type. I don't know
why that's different, but both the C and C++ compilers treat the
example the same.
More information about the Gcc-help
mailing list