Home > Enterprise >  Use C 20 modules to make shared libs
Use C 20 modules to make shared libs

Time:03-03

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 ?

CodePudding user response:

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.

  • Related