Using __attribute__ with typedef

c++, g++, macros, typedef, visual-c++

Solution

AFAIK in GCC `visibility` attribute for function type cannot be "wrapped" into a typedef-ed type. The compiler assumes that this `visibility` attribute applies to the typedef-name itself. And GCC does not support `visibility` for typedef names (and it is not what you need anyway).

I'd say that instead of trying to wrap the `declspec`/`attribute` into the typedef, it should be specified explicitly at the point of function declaration. As in

#ifdef WIN32
#define MY_PREFIX __declspec(dllexport)
#else
#define MY_PREFIX __attribute__((visibility("default")))
#endif

typedef bool some_func(void);

MY_PREFIX some_func foo; // <- actual declaration

This will, of course, make is less clean, since instead of specifying `MY_PREFIX` once inside the typedef it should now be specified in every function declaration. But that's probably the only way to make it work, unless I'm missing something.

Problem

Apologies if the question seems too obvious or simple. Unfortunately, after going through a bunch of threads and googling about typedef coupled with attribute prefix, I am still not able to it figure out. I have a following snippet of code in a (supposedly) portable app - ``` #ifdef WIN32 #define MY_PREFIX __declspec(dllexport) #else #define MY_PREFIX __attribute__((visibility("default"))) #endif typedef MY_PREFIX bool some_func(void); ``` So my question is this - 1) What is that typedef exactly doing? 2) The code compiles fine on VS2008, but on G++ (gcc-4.1), I get a warning "‘visibility’ attribute ignored" Is there any way I can remove that warning? (Omitting -Wattributes is not an option) Thanks!

Original source