2 If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
3 destructors, in a sane way, including e.g. on library unload. If not you're on
6 Some compilers need #pragma to handle this, which does not work with macros,
7 so the way you need to use this is (for constructors):
9 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
10 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
12 G_DEFINE_CONSTRUCTOR(my_constructor)
13 static void my_constructor(void) {
19 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
21 #define G_HAS_CONSTRUCTORS 1
23 #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
24 #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
26 #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
27 /* Visual studio 2008 and later has _Pragma */
29 #define G_HAS_CONSTRUCTORS 1
31 #define G_DEFINE_CONSTRUCTOR(_func) \
32 static void _func(void); \
33 static int _func ## _wrapper(void) { _func(); return 0; } \
34 __pragma(section(".CRT$XCU",read)) \
35 __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
37 #define G_DEFINE_DESTRUCTOR(_func) \
38 static void _func(void); \
39 static int _func ## _constructor(void) { atexit (_func); return 0; } \
40 __pragma(section(".CRT$XCU",read)) \
41 __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
43 #elif defined (_MSC_VER)
45 #define G_HAS_CONSTRUCTORS 1
47 /* Pre Visual studio 2008 must use #pragma section */
48 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
49 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
51 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
52 section(".CRT$XCU",read)
53 #define G_DEFINE_CONSTRUCTOR(_func) \
54 static void _func(void); \
55 static int _func ## _wrapper(void) { _func(); return 0; } \
56 __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
58 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
59 section(".CRT$XCU",read)
60 #define G_DEFINE_DESTRUCTOR(_func) \
61 static void _func(void); \
62 static int _func ## _constructor(void) { atexit (_func); return 0; } \
63 __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
65 #elif defined(__SUNPRO_C)
67 /* This is not tested, but i believe it should work, based on:
68 * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
71 #define G_HAS_CONSTRUCTORS 1
73 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
74 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
76 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
78 #define G_DEFINE_CONSTRUCTOR(_func) \
79 static void _func(void);
81 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
83 #define G_DEFINE_DESTRUCTOR(_func) \
84 static void _func(void);
88 /* constructors not supported for this compiler */