How to Use C++20 Modules with Cmake

Use C++ 20 modules to make shared libs

Well, the error message tells you where the compiler is looking for that file, and it certainly isn't /usr/local/lib, so that's not going to work. You could distribute the gcm file and instruct the user to put it in the gcm.cache directory for their project I suppose, but, quoting from the link you posted (emphasis theirs):

The CMI is not a distributable artifact. Think of it as a cache, that can be recreated as needed.

(Where CMI stands for Compiled Module Interface, and corresponds to your gcm file.) So I guess you shouldn't do that. For one thing, your link states that it might contain absolute paths (to include files referenced by the module interface via your include path, typically), and that sounds like a recipe for trouble. Also, these files are not portable across different compilers (and possibly even compiler versions).

So, the solution seems to be to supply your module interface file(s) along with your library and tell your users to recompile them before they try to do anything else. You can tell them how in your readme file.

How to use Clang with C++ Modules in Windows 10

CMake does not support C++ modules. Once it is officially supported, you can expect it to work. Until than, the compiler or msbuild might do some magic, but you cannot rely on it.

Link clang++ modules: Function exported using C++20 modules is not visible (clang++): Cannot compile executable file that uses module

You want to compile a module twice. Once to emit a module interface file (which should not have the .o suffix BTW; the customary suffix is .pcm, for "precompiled module") and once to emit an object file (with the customary .o suffix).

clang++ -std=c++20 -c f1_module.cpp -o target/f1_module.o # plain old object
clang++ -std=c++20 -Xclang -emit-module-interface \
-c f1_module.cpp -o target/f1_module.pcm # module interface

Now your module (consisting of two files) is ready, you can use it.

You need to compile the main file against the .pcm file and link the resulting objects against the .o file to produce an executable.

clang++ -std=c++20 -fprebuilt-module-path=./target \
-c f1_demo.cpp -o target/f1_demo.o
clang++ -std=c++20 target/f1_demo.o target/f1_module.o -o target/f1.exe

This is not strictly necessary. With clang, the precompiled module can be used as an object file. The linker however won't recognize it, so clang will need to convert .pcm to .o and feed the temporary .o to the linker, each time you link. This is somewhat of a waste so you may want to eliminate this conversion step by building a separate .o file as above. If you choose not to, you still need to mention the module file on the link line. The simplified process is like this:

clang++ -std=c++20 -Xclang -emit-module-interface \
-c f1_module.cpp -o target/f1_module.pcm # compile module once
clang++ -std=c++20 -fprebuilt-module-path=./target \
-c f1_demo.cpp -o target/f1_demo.o # compile main just as before
clang++ -std=c++20 target/f1_demo.o target/f1_module.pcm \
-o target/f1.exe # mention .pcm explicitly

As far as I know (and I don't know very much), there is currently no way to have clang find out automatically which modules to link against.



Related Topics



Leave a reply



Submit