r/cpp • • Oct 05 '26

Notes on dllexport and dllimport

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

7 comments sorted by

9

u/fsfod Oct 06 '26

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

3

u/hansw2000 Oct 07 '26

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

4

u/ack_error Oct 07 '26

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

12

u/jedwardsol const & Oct 05 '26

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

1

u/OrphisFlo I like build tools Oct 06 '26

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

2

u/hansw2000 Oct 06 '26

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

4

u/pjmlp Oct 06 '26

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.