Last active
September 20, 2024 19:19
-
-
Save bstaletic/9c38fd2869707c4666b9361810817243 to your computer and use it in GitHub Desktop.
c++23 modules example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.30) | |
# Set experimental flag to enable `import std` support from CMake. | |
# This must be enabled before C++ language support. | |
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD | |
# This specific value changes as experimental support evolves. See | |
# `Help/dev/experimental.rst` in the CMake source corresponding to | |
# your CMake build for the exact value to use. | |
"0e5b6991-d74f-4b3d-a41c-cf096e0b2508") | |
project(std_module_example CXX) | |
# Tell CMake that we explicitly want `import std`. This will initialize the | |
# property on all targets declared after this to 1 | |
set(CMAKE_CXX_MODULE_STD 1) | |
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | |
# Turning off extensions avoids an issue with the clang 16 compiler | |
# clang 17 and greater can avoid this setting | |
set(CMAKE_CXX_EXTENSIONS ON) | |
# Set the version of C++ for the project | |
set(CMAKE_CXX_STANDARD 23) | |
# Create a library | |
add_library(foo STATIC) | |
# Add the module file to the library | |
target_sources(foo PUBLIC FILE_SET CXX_MODULES FILES foo_interface.cpp) | |
target_sources(foo PUBLIC foo_impl.cpp) | |
target_compile_features(foo INTERFACE cxx_std_23) | |
# Create an executable | |
add_executable(hello) | |
target_sources(hello PRIVATE main.cpp) | |
# Link to the library foo | |
target_link_libraries(hello PRIVATE foo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//module; | |
//#include <whatever> | |
module foo; | |
import std; | |
void f() { std::cout << "f()\n"; } | |
foo::foo() = default; | |
foo::~foo() = default; | |
void foo::helloworld() { std::cout << "hello world\n"; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export module foo; | |
export class foo { | |
public: | |
foo(); | |
~foo(); | |
void helloworld(); | |
void other(); | |
}; | |
export void f(); | |
export void g(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import foo; | |
int main() | |
{ | |
foo fo; | |
fo.helloworld(); | |
f(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment