Skip to content

Commit 4ed1c7b

Browse files
committed
Make MSVC symbol export/import work
1 parent ecc994f commit 4ed1c7b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

tests/packages/sharedlib-in-package/mypkg/examplelib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
//
33
// SPDX-License-Identifier: MIT
44

5-
int sum(int a, int b);
5+
#include "mypkg_dll.h"
6+
7+
MYPKG_DLL int sum(int a, int b);

tests/packages/sharedlib-in-package/mypkg/meson.build

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,31 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
if meson.get_compiler('c').get_id() in ['msvc', 'clang-cl', 'intel-cl']
6+
export_dll_args = ['-DMYPKG_DLL_EXPORTS']
7+
import_dll_args = ['-DMYPKG_DLL_IMPORTS']
8+
else
9+
export_dll_args = []
10+
import_dll_args = []
11+
endif
12+
513
example_lib = shared_library(
614
'examplelib',
715
'examplelib.c',
16+
c_args: export_dll_args,
817
install: true,
918
install_dir: py.get_install_dir() / 'mypkg',
1019
)
1120

21+
example_lib_dep = declare_dependency(
22+
compile_args: import_dll_args,
23+
link_with: example_lib,
24+
)
25+
1226
py.extension_module(
1327
'_example',
1428
'_examplemod.c',
15-
link_with: example_lib,
29+
dependencies: example_lib_dep,
1630
install: true,
1731
subdir: 'mypkg',
1832
install_rpath: '$ORIGIN',
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
// MYPKG_DLL
4+
// inspired by https://github.com/abseil/abseil-cpp/blob/20240116.2/absl/base/config.h#L736-L753
5+
// and https://github.com/scipy/scipy/blob/9ded83b51099eee745418ccbb30196db96c81f3f/scipy/_build_utils/src/scipy_dll.h
6+
//
7+
// When building the `examplelib` DLL, this macro expands to `__declspec(dllexport)`
8+
// so we can annotate symbols appropriately as being exported. When used in
9+
// headers consuming a DLL, this macro expands to `__declspec(dllimport)` so
10+
// that consumers know the symbol is defined inside the DLL. In all other cases,
11+
// the macro expands to nothing.
12+
// Note: MYPKG_DLL_{EX,IM}PORTS are set in mypkg/meson.build
13+
#if defined(MYPKG_DLL_EXPORTS)
14+
#define MYPKG_DLL __declspec(dllexport)
15+
#elif defined(MYPKG_DLL_IMPORTS)
16+
#define MYPKG_DLL __declspec(dllimport)
17+
#else
18+
#define MYPKG_DLL
19+
#endif

0 commit comments

Comments
 (0)