Sorry newbie here but this is not obvious for me.
I use linux mint, latest clang version (21.0.0) and switching my project to C++ modules. The problem I experience is that i need to interact in shared library with classes from main executable. The structure of project is like that:
converter/converter_base.cppm
converter/C++/*.cpp, *.cppm
src/*.cpp, *.cppm
the converter is place for dynamic library (perform some convertions) and src/* are files of main executable. When i try to build shared library it does not see modules from main executable, and if I add modules from main executable it says module x is compiled in several transition units. What the proper way for:
- Use C++20 modules in both the main executable and the shared library
- Allow shared library to be able import modules from main executable and ideally vise versa
- Properly link it all together
As more context here's cmake build script I use:
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "TRUE") # required for modules
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP "TRUE") # enables BMI dependencies
add_executable(ispa)
#target_compile_options(ispa PRIVATE -fmodules)
get_file_range(ISPA_SOURCES
${PARSER_DIR}/*.cpp
${SRC_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/external/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/external/*.cc
)
get_file_range(ISPA_MODULE_INTERFACES
${SRC_DIR}/*.cppm
)
target_sources(ispa PRIVATE ${ISPA_SOURCES})
target_sources(ispa
PRIVATE
FILE_SET cxx_modules TYPE CXX_MODULES FILES
${ISPA_MODULE_INTERFACES}
)
target_include_directories(ispa PRIVATE ${INCLUDE_DIRS})
set_target_properties(ispa PROPERTIES
OUTPUT_NAME ispa
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
HEADER_FILE_ONLY FALSE
SKIP_PRECOMPILE_HEADERS TRUE
)
add_library(ispa-converter INTERFACE)
target_include_directories(ispa-converter INTERFACE ${INCLUDE_DIRS})
#
#if(WIN32)
# file(GLOB_RECURSE CONVERTER_SRC ${SRC_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/external/cpuf/*.cpp)
#endif()
file(GLOB_RECURSE CPP_CONVERTER_SOURCES converters/C++/*.cpp)
add_library(ispa-converter-cpp SHARED
${CPP_CONVERTER_SOURCES}
# ${CONVERTER_SRC}
)
get_file_range(CONVERTER_MODULE_INTERFACES
converters/C++/*.cppm
)
target_sources(ispa-converter-cpp
PRIVATE
FILE_SET cxx_modules TYPE CXX_MODULES FILES
${CONVERTER_MODULE_INTERFACES}
${ISPA_MODULE_INTERFACES}
)
target_link_libraries(ispa-converter-cpp PRIVATE
ispa-converter
)
target_include_directories(ispa-converter-cpp PRIVATE converters/C++)