Common pattern for exporting symbols from shared library for msvc and gcc #10822
-
I'm interested if there is a cleaner looking recipe to support the following pattern of shared library symbol exports across Windows and *nix. Example function (for illustration purposes only):
Example MSVC export definition file Example.def (for illustration purposes only):
Example gcc linker script (for illustration purposes only):
My, quite possibly hacky, meson.build aiming at a common pattern:
It does work as I'd hope/expect. As it happens in my actual scenario I am code generating the .def or .exp files depending on compiler, but if I don't specify both files then I get an error because the logic for vs_module_defs expects that you give it a legit file as an argument. My hack above just passes the .exp file. It's not difficult to work around the problem but I'd be interested if other folks have cleaner ways to accomplish this. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
There are two ways to do this in MSVC, and two ways to do this in GCC. GCC has version scripts and MSVC has def files. They use different formats, sadly, and involve passing external files to the linker. GCC also has symbol visibility and MSVC has the dllexport declspec. You can stick this inside a header as a macro and choose the correct one per platform, with the caveat that MSVC is dumb and actually requires you to use different values depending on whether you are building a static or shared library -- and have some extra pain if you want to build both, you have to compile all source files twice in that case. Your example shows using the first method. The problem is that you always need to define the vs_module_defs, even if the file isn't actually created yet. A dummy file works. It also works if you do |
Beta Was this translation helpful? Give feedback.
-
What I would recommend is using a macro that you set for all your public symbols, like To use it:
|
Beta Was this translation helpful? Give feedback.
-
In fact, I've just remembered another idea I looked at a few years ago which I might try later. Some linkers allow you to specify exported symbol names on the command line at link time. MSVC using link /EXPORT and I'm sure I've seen something similar on *nix somewhere. |
Beta Was this translation helpful? Give feedback.
There are two ways to do this in MSVC, and two ways to do this in GCC.
GCC has version scripts and MSVC has def files. They use different formats, sadly, and involve passing external files to the linker.
GCC also has symbol visibility and MSVC has the dllexport declspec. You can stick this inside a header as a macro and choose the correct one per platform, with the caveat that MSVC is dumb and actually requires you to use different values depending on whether you are building a static or shared library -- and have some extra pain if you want to build both, you have to compile all source files twice in that case.
Your example shows using the first method. The problem is that you always nee…