I’ve just started to learn programming in C++ and the books I use for that focus on the new module system to replace the #include method.
For make I’ve found Makefiles that allow to first convert the #includes (like iostream for example) into a module file (g++ gives these a .gcm extension) and then use those to compile the binary.
Now since I’m using CLion and it’s use of CMake it would be great to be able to do this with CMake but I can only find information on how to do this by making a personal module file that #includes the header and exports the requested functionality by using a function.
Do anyone know if this is (already) possible or do we have to wait for GCC or CLANG to include standard library modules (like print for C++20, or std for C++23) ?
The majority of the information I found solve it like this:
main.cpp
import foo;
int main() {
print("Hello, World!n");
}
foo.cppm
module;
#include <iostream>
#include <string>
export module foo;
export void print(std::string s) { std::cout << s; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
project(cmake_module_example CXX)
set(CMAKE_CXX_STANDARD 23)
# Create a library
add_library(my_modules)
# Add the module file to the library
target_sources(my_modules
PUBLIC
FILE_SET CXX_MODULES FILES
foo.cppm
)
# Create an executable
add_executable(hello main.cpp
main.cpp)
# Link to the library foo
target_link_libraries(hello my_modules)
I’m using Debian 12 and tried with gcc-14 and clang-17
The build tool that CLion uses is ninja, and unless they ship with a newer version the current version is 1.11.1
Patrick Kox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.