Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using __attribute__ with typedef

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!

like image 560
sskanitk Avatar asked Oct 24 '12 20:10

sskanitk


1 Answers

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.

like image 97
AnT Avatar answered Oct 17 '22 21:10

AnT