From 511b71f19af2822c1f7e210a5ed4978fd0bd0d35 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Sat, 4 Apr 2026 00:04:55 +0800 Subject: [PATCH] dev: add module example --- .../samples/cmake-workspace/CMakeLists.txt | 30 ++++++++++++++++++- .../vscode/samples/cmake-workspace/README.md | 17 +++++++++++ .../cmake-workspace/greeting_module.cppm | 14 +++++++++ .../samples/cmake-workspace/main_module.cc | 8 +++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 editors/vscode/samples/cmake-workspace/greeting_module.cppm create mode 100644 editors/vscode/samples/cmake-workspace/main_module.cc diff --git a/editors/vscode/samples/cmake-workspace/CMakeLists.txt b/editors/vscode/samples/cmake-workspace/CMakeLists.txt index 24ad3849..4673b8b3 100644 --- a/editors/vscode/samples/cmake-workspace/CMakeLists.txt +++ b/editors/vscode/samples/cmake-workspace/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.28) project(clice_vscode_cmake_sample LANGUAGES CXX) @@ -12,3 +12,31 @@ target_include_directories(sample_greeting PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") add_executable(sample_app main.cc) target_link_libraries(sample_app PRIVATE sample_greeting) + +set(SAMPLE_MODULES_SUPPORTED OFF) +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND + CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16) + set(SAMPLE_MODULES_SUPPORTED ON) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND + CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 14) + set(SAMPLE_MODULES_SUPPORTED ON) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND + CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.34) + set(SAMPLE_MODULES_SUPPORTED ON) +endif() + +if(SAMPLE_MODULES_SUPPORTED) + add_executable(sample_module_app) + target_sources(sample_module_app PRIVATE main_module.cc) + target_sources(sample_module_app + PRIVATE + FILE_SET CXX_MODULES + FILES greeting_module.cppm + ) +else() + message(STATUS + "Skipping sample_module_app because the active compiler lacks " + "CMake C++20 module scanning support. Use Clang >= 16, GCC >= 14, " + "or MSVC 19.34+ to enable it." + ) +endif() diff --git a/editors/vscode/samples/cmake-workspace/README.md b/editors/vscode/samples/cmake-workspace/README.md index 68821c60..15c2c920 100644 --- a/editors/vscode/samples/cmake-workspace/README.md +++ b/editors/vscode/samples/cmake-workspace/README.md @@ -4,6 +4,11 @@ This workspace is a standalone CMake project for attaching the VS Code extension `clice` already auto-detects `build/compile_commands.json`, so this sample does not need any helper scripts or extra CMake glue. +The workspace contains two entry points: + +- `main.cc`: a traditional include-based example. +- `main_module.cc`: a C++20 modules example that imports `greeting_module.cppm`. + ## Prepare The Workspace From this directory: @@ -19,3 +24,15 @@ If you also want the sample binary: ```sh cmake --build build ``` + +## C++20 Modules + +The module example is enabled automatically when CMake is using a compiler it can scan for C++20 modules: + +- Clang 16+ +- GCC 14+ +- MSVC 19.34+ + +If the active compiler is older than that, CMake still configures the workspace and builds `sample_app`, but it skips `sample_module_app`. + +When you do have a supported compiler, `main_module.cc` and `greeting_module.cppm` will also appear in `build/compile_commands.json`, which makes this workspace useful for testing clice's module handling in an editor. diff --git a/editors/vscode/samples/cmake-workspace/greeting_module.cppm b/editors/vscode/samples/cmake-workspace/greeting_module.cppm new file mode 100644 index 00000000..8043c488 --- /dev/null +++ b/editors/vscode/samples/cmake-workspace/greeting_module.cppm @@ -0,0 +1,14 @@ +module; + +#include +#include + +export module sample.greeting; + +export namespace sample { + +std::string build_module_greeting(std::string_view name) { + return "Hello, " + std::string(name) + " from the C++20 module sample."; +} + +} diff --git a/editors/vscode/samples/cmake-workspace/main_module.cc b/editors/vscode/samples/cmake-workspace/main_module.cc new file mode 100644 index 00000000..1dea4816 --- /dev/null +++ b/editors/vscode/samples/cmake-workspace/main_module.cc @@ -0,0 +1,8 @@ +#include + +import sample.greeting; + +int main() { + std::cout << sample::build_module_greeting("clice") << '\n'; + return 0; +}