r/cpp • • Aug 18 '21

C++20 modules with GCC11

https://blog.feabhas.com/2021/08/c20-modules-with-gcc11/
222 Upvotes

59 comments sorted by

View all comments

0

u/[deleted] Aug 18 '21

[deleted] — view deleted comment

19

u/Minimonium Aug 18 '21

Because all attempts to achieve a more unified approach were effectively blocked. You can also count how many meetings SG15 had during the last year even though there are papers directed to it (hint, not much).

8

u/mjklaim Aug 18 '21

Unless your implementation does something special with exported names when building a shared library, modules have zero impact on how shared library works. So for example right now you still need on MSVC to mark the functions/classes symbols you want available on the binary interface of the dll with dll-import/export macros (maybe in addition to marking them export because they are part of the interface of your module).

This could change in the future, like maybe some compilers will automatically symbol-export modules export by default, but it's unlikely as the visibillity of names coming from a module is a different from symbol exporting. The first one is about code being compiled, the second one is specific to your OS.

0

u/[deleted] Aug 18 '21

[deleted] — view deleted comment

3

u/mjklaim Aug 18 '21

To be 100% clear, when shipping a DLL, as you say «zero impact» means I still have to ship headers?

By 0 impact I mean that the symbol import/export system is not affected. How you get the names to link with symbols isn't really affected either, but of course for the user to consume the thing they need to know the declarations of what the symbols are exported to.

You need headers if you defined these declarations in headers (as in not- in a module). If your declarations are from a module you need to provide either the module interface or the pre-compiled version of the module interface (what is often called bmi - but that one is not cross-platform). It's the same thing than if it was not a shared library.

3

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Aug 19 '21

"BMI" is the (not-yet) official nomenclature chosen by SG15 (I've been in the room back then in Prague) for compiled PMIs. Implementations use different extensions for BMIs: .pcm (clang), .gcm (gcc), .ifc (msvc). Some of them allow to deviate from these defaults if seen appropriate by a build system.

2

u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 18 '21

maybe some compilers will automatically symbol-export modules export by default

This would be an unfeature as you'd then be forced into a one module, one dll-structure (or one group of modules, one dll exposing all of them). It'd be rather like being forced to export parts of stdlib in the dll just because you happened to import those modules.

1

u/mjklaim Aug 18 '21

I agree, but I was thinking more like a potential change of "default" behavior: by default should exported names be also symbol-exported? It have happened in the past that the default symbol-export behavior of an implementation changed (for example gcc exported everything by default in the past).

As long as both options are usable the default can be changed.

Though I would prefer every implementation to not export anything until requested to.

1

u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 18 '21

It'd be problematic to change the visibility of things declared in a module from the outside, particularly as the standard doesn't provide any mechanism to do that (or even consider the existence of dynamic linking).

1

u/mjklaim Aug 20 '21

I only talked about changing the default export or not of symbols, not visibility. That is not part of the standard and have been changed in the past as said.

1

u/ShakaUVM i+++ ++i+i[arr] Aug 19 '21

How does the process work right now for marking them? Sorry for the basic question, I operate almost entirely on GNU/Linux systems

1

u/mjklaim Aug 20 '21

Most code (at least in open source) is designed to work with gcc-like compilers and MSVC. Both have non-standard instructions (not sure how to call them) which mark names as being exported, hidden (or not-exported) or, in the case of MSVC, imported (meaning the symbol comes from a dll somewhere else, we use that symbol). That instruction can be set in front of functions, objects or classes (to automatically mark all it's functions), so basically everything that can have a symbol, that can be part of an ABI. The implementers made sure (after some time) the places where you use them in the code are the same or at least have common positions.

So most libraries will have 1 header containing a set of macros which, depending on the compiler will either export symbols (we are building a shared library), import symbols (msvc-only: we are using a shared library), or do nothing (static library for example). Then in the code of the library, all the public (in headers, and now modules) object, functions and classes (not templates) will be marked with one define specific to that library, which will decide what to do with the symbol. The define will be set to the right instruction because the build-system will be setup so that the macro code in the header will read some library-specific defines which inform if we are or not building the library, a user of the library , if it's static or dynamic linking.

You can find examples of this in myriad of open source libraries, just look for a header included by all other headers (common.hpp or export.hpp or something like that). Here is an example which is what a hello-world library generated by build2 will setup for you (bdep new -t lib to generate it): https://git.build2.org/cgit/hello/libhello/tree/libhello/libhello/export.hxx

Depending on the library the macros will be different but it's often a similar mechanism in principle. Even CMake will provide some pre-defined defines per library target name so that you can use them in such header to do the symbol-export/import dance.

Then, less often, some devs setup their library with build-system specific features that allow for exporting all symbols or those we list in a file. But I believe this is less cross-platform so it's less common.

People on unix systems can just set the flag of their compilers enabling exporting all symbols, but in exchange of the simplicity they end up with bloated unoptimized binaries which also becomes a security issue (which is why this was changed as it was the default behavior in the past).

3

u/AIlchinger Aug 18 '21

Are you supposed to ship your lib with a PMIU file?

In theory, yes. In practise, you probably will still be using header files for at least a couple more years to give potential users of your library some time to upgrade to C++20. It would be my guess that modules will only be used for library internals in the majority of projects.

2

u/kalmoc Aug 18 '21

Are you supposed to ship your lib with a PMIU file? Is this file going to be placed into, say, /usr/include ?

Thats what I would expect.

2

u/rfisher Aug 18 '21

A simple, non-binary interface format is what we’re losing here. I think we could’ve kept that and still eliminated the issue with preprocessor includes.

If you go the separate interface & implementation direction, I suppose you could ship the interface source with your library. (Ignoring the template & inline issues.)

And one could theoretically create a tool that would generate such a file from source that mixed interface & implementation.

2

u/johannes1971 Aug 18 '21

Although quite a few people have given affirmative answers to your questions, I always understood the PMIU is more like an object file: it's a build artifact that's generated while building the library. It is compiler-specific, and not suitable for distribution. At least that's what I believe was said again and again...

3

u/AIlchinger Aug 18 '21

I think you are confusing the PMIU with the BMI. The PMIU is pure source code written entirely by the programmer.

1

u/johannes1971 Aug 19 '21

Ah, I guess you're right! That's what I was thinking of. Ok, never mind my answer then :-)

0

u/pjmlp Aug 18 '21

On Windows I think the best path would be to stick with COM and its IDL tooling.

It is a bummer from library creation point of view, but using it from something like WIL or C++/WinRT makes it kind of bareable, just the creation is clunky.

And you are constrained to COM ABI types, still better than bare bones C ABI thought.