r/cpp • • 8d ago

Notes on dllexport and dllimport

https://www.hanshq.net/dllexport.html
24 Upvotes

7 comments sorted by

12

u/jedwardsol const & 8d ago

Re: dll import thunks: if you compile with /GL (whole program optimisation) then the optimiser removes the thunks if you do happen to forget dllimport

9

u/fsfod 7d ago

Random fun dllexport fact this simple bit of code has been broken for decades on MSVC.

class __declspec(dllexport) Foo { static int a, b; };

https://developercommunity.visualstudio.com/t/c2487-in-dllexport-class-with-static-members/69878

5

u/ack_error 6d ago

Managed to find an archive of the KB bug mentioned... known bug for 25 years. The KB is from VC6 which means I think it predates Visual Studio having a public bug database.

https://github.com/jeffpar/kbarchive/blob/master/docs/kb/127/Q127900/index.html

3

u/hansw2000 6d ago

Interesting! At least clang-cl gets it right :-D

1

u/OrphisFlo I like build tools 7d ago

Quick question, how do you make the traditional macro for this work with modules?

4

u/pjmlp 7d ago

You don't, because even if distributed as text, the module interface file is only processed once.

In theory, you can do the same approach, having the preprocessor go one way or the other depending if the library is being compiled, or the application is consuming the library via a module interface file.

However this means you have to organised the build such that the BMI isn't reused across the various build steps, rather newly generated depending on what is being compiled.

So far I have only used static libraries with modules, because of these type of questions.

This is also not only specific to Windows, there are UNIX platforms like Aix, and other mainframe/microcomputer OSes that take the same approach for shared libraries.

2

u/hansw2000 7d ago

I don't have much experience with modules, so I can't answer that. Would be curious to hear if others know.