Issue
My goal is to reuse some already generated c++20 modules in one project into another.
I tried the following rel="nofollow noreferrer">github repo and module usage worked perfectly.
First project generating the module files:
I decided to change the Cmake structure to this:
cmake_minimum_required(VERSION 3.26)
set(APP_NAME CppModules)
project(${APP_NAME} LANGUAGES CXX)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS Off)
add_executable(${APP_NAME} main.cpp)
add_library(my_lib2)
target_sources(my_lib2
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES FILES
src/my_lib/my_lib2.cpp
)
add_library(my_lib)
target_sources(my_lib
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES FILES
src/my_lib/my_lib.cpp
)
target_link_libraries(${APP_NAME} PRIVATE my_lib my_lib2 )
set_target_properties(${APP_NAME} PROPERTIES LINKER_LANGUAGE CXX)
Now, I have some .pcm files and some {my_lib, mylib2}.a files that have been generated.
Second project trying to include:
This is the structure of the second project trying to import the module:
import my_lib;
int main() {
foo f;
f.helloworld();
return 0;
}
But that program complains with
fatal error: module 'my_lib' not found
even if I have the following cmake file:
cmake_minimum_required(VERSION 3.26)
set(APP_NAME Importer)
project(${APP_NAME} LANGUAGES CXX)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS Off)
add_executable(${APP_NAME} main.cpp)
target_link_libraries(${APP_NAME} PRIVATE /absolute/path/to/libmy_lib.a)
set_target_properties(${APP_NAME} PROPERTIES LINKER_LANGUAGE CXX)
so I miss a way to inject the previously generated module into that project.
Question is, how may I do ? What is the missing bit ?
Solution
See /reference
compiler parameter for MSVC (https://learn.microsoft.com/en-us/cpp/build/reference/module-reference?view=msvc-170). I don't think there is something special in CMake, so you have to set up it manually, like this:
target_compile_options(${PROJECT_NAME} PUBLIC /reference "mymodule.ifc")
As far as I know, module ABI is not stable - it differs for debug and release and for different compilers, maybe even for different compiler versions / MSVC toolkit versions
Answered By - Perl99 Answer Checked By - Gilberto Lyons (WPSolving Admin)