Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit ad84455

Browse files
committed
init.c: Add msvc compatible global construct/destruct.
1 parent 158f4ba commit ad84455

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/init.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@
3232
#include <winsock2.h>
3333
#endif
3434

35-
#define CONSTRUCTOR_ATTRIBUTE __attribute__((constructor))
36-
#define DESTRUCTOR_ATTRIBUTE __attribute__((destructor))
35+
#ifdef _MSC_VER
36+
#define CONSTRUCTOR_ATTRIBUTE(_func) static void _func(void); \
37+
static int _func ## _wrapper(void) { _func(); return 0; } \
38+
__pragma(section(".CRT$XCU",read)) \
39+
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
40+
#define DESTRUCTOR_ATTRIBUTE(_func) static void _func(void); \
41+
static int _func ## _constructor(void) { atexit (_func); return 0; } \
42+
__pragma(section(".CRT$XCU",read)) \
43+
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
44+
#else
45+
#define CONSTRUCTOR_ATTRIBUTE(_func) void _func(void) __attribute__((constructor))
46+
#define DESTRUCTOR_ATTRIBUTE(_func) void _func(void) __attribute__((destructor))
47+
#endif
3748

3849
/* Declare static mutex */
3950
static SSH_MUTEX ssh_init_mutex = SSH_MUTEX_STATIC_INIT;
@@ -44,8 +55,8 @@ static int _ssh_initialized = 0;
4455
/* Cache the returned value */
4556
static int _ssh_init_ret = 0;
4657

47-
void libssh_constructor(void) CONSTRUCTOR_ATTRIBUTE;
48-
void libssh_destructor(void) DESTRUCTOR_ATTRIBUTE;
58+
CONSTRUCTOR_ATTRIBUTE(libssh_constructor);
59+
DESTRUCTOR_ATTRIBUTE(libssh_destructor);
4960

5061
static int _ssh_init(unsigned constructor) {
5162

0 commit comments

Comments
 (0)