2

I'm looking C++20 modules, and I'm asking how to make shared libs with modules.

All examples (I've found), works in same directory (lib + main) so there is no problem on compilation time.

But if I want to make a .so file, and import it into another program in another dir.

g++ give me (I've used that code https://gcc.gnu.org/wiki/cxx-modules#Example) with that command : g++-11 -std=c++20 -fmodules-ts main.cpp -o app -Xlinker libhello.so

In module imported at main.cpp:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is 'gcm.cache/hello.gcm'
hello: note: imports must be built before being imported
hello: fatal error: returning to the gate for a mechanical issue

Should I share hello.gcm file too ? And put it in /usr/local/lib too ?

asked Mar 2, 2022 at 11:40
1
  • I think you should seperate your module interface and implementation into 2 files, import the interface file to main.cpp and use the implementation file to create a shared Library, then link together. Commented Apr 30, 2022 at 3:22

2 Answers 2

3

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.

answered Mar 2, 2022 at 13:14
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried to look for more details about what you had say but module seems not a way to distribute lib as package (stackoverflow.com/a/59674916/2558648)
Don't distribute the module implementation source. Distribute the module interface source. Or stick with a plain old .h file. In an age of precompiled header files, there's really not a lot wrong with that.
0

Looks like the module called "hello" wasn't compiled first. I mean, you should first compile it with a similar compiler command, and then compile "main". A lot of time passed, but it's just interesting how you dealt with that :)

answered Jul 28, 2024 at 18:25

1 Comment

Add to my todo list: Just do TRY !!! ^^ Actually, we have moved to ExternalContent with CMake, compilation take more time but we don't have problem with pre-compiled libs

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.