简体   繁体   中英

Will c++1z modules replace the need for dllimport dllexport on windows

I'm learning about C++1z's proposal for modules. My biggest hope is that it will replace the usage of dllimport , dllexport on windows. With c++1z modules, will I be able to build .dll on windows and .so on linux avoiding the use of dllimport/dllexport ? Will the module export be all that's needed on all platforms and compilers?

Unfortunately, no.

The proposals for modules in C++ attempt to deal with shortcomings in headers, which become particularly problematic with code that involves headers.

Templates are typically implemented entirely in a header--but this means the contents of a template become subject to any preprocessor definitions that happen before that header is included.

For example, if your template uses i as an identifier, and a header with something like #define i 2 happens to be included before your template's header, your code could start out like this:

for (int i=0; i<10 ; i++)

...but after the preprocessor is done, it would look like this:

for (int 2=0; 2<20; 2++)

...and that obviously won't compile at all.

Modules fix this. A module is compiled independently instead of being in a header. Since it's compiled independently, a module isn't affected by other headers unless its source code includes those headers.

Likewise, any preprocessor definitions made in the header can't affect any of the code that imports the module. The only names in the module that become visible in the file that imports that module are the names explicitly exported from the module.

The dllexport will still be needed but a dllimport will be probably automatic. At least in C++ Modules in VS 2015 Update 1 they say it in one comment:

Andrew Pardoe [MSFT]

@Matthias: The programmer now only needs to say __declspec(dllexport) for symbols that are to be exported at DLL boundaries. the __declspec(dllimport) is taken care of by the compiler when consuming a module.

Unfortunately I didn't find any more reliable information about it.

Possibly.

I read up on the c++20 proposals submitted to the standards committee- they're now on GitHub- and there IS one that would cover this. It proposes a new [[shared]] c++ attribute that would do this, and makes it mandatory for compilers like GCC & MSVC to implement it. Unfortunately it may have missed the window to get into the c++20 spec, although there is still time for them to approve it at the last minute.

Personally I hope they say "sure" and add it in time. The whole point of this attribute syntax- and half these other language changes- is to supposedly enable us to stop needing macros & vendor specific boilerplate in normal code. Say, like building a DLL. So if they don't adopt this proposal, they'll eventually have to adopt something similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM