Save yourself days of pain! This guide provides the exact configuration needed to make import std;
work with GCC 15.1 and CMake 4.1.
If you just want it to work, here's the exact setup:
cmake_minimum_required(VERSION 4.1)
# THIS IS CRITICAL - Enable experimental import std support
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
project(YourProject)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
set_target_properties(YourTarget PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
CXX_MODULE_STD 1 # β THIS IS THE KEY PROPERTY THAT EVERYONE MISSES!
)
target_compile_options(YourTarget PRIVATE -fmodules-ts)
- GCC 15.1+ (with libstdc++ module support)
- CMake 4.1+ (for experimental std import)
- C++23 standard
- Ninja (recommended build system)
project/
βββ CMakeLists.txt # Root configuration
βββ Library1/
β βββ CMakeLists.txt # Library with CXX_MODULE_STD 1
β βββ math_lib.cppm # Module using import std
βββ App1/
β βββ CMakeLists.txt # App with CXX_MODULE_STD 1
β βββ main.cpp # Uses import std + custom module
βββ App2/
βββ CMakeLists.txt # Another app
βββ main.cpp
cmake_minimum_required(VERSION 4.1)
# Enable experimental import std support for GCC 15.1
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
project(ModulesExample)
# Set C++23 standard and enable modules
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
# Set build type to Debug for debugging
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
# Add subdirectories
add_subdirectory(Library1)
add_subdirectory(App1)
add_subdirectory(App2)
add_library(Library1)
# Add module interface
target_sources(Library1
PUBLIC
FILE_SET CXX_MODULES FILES
math_lib.cppm
)
# CRITICAL: Set C++23 standard and CXX_MODULE_STD property
set_target_properties(Library1 PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
CXX_MODULE_STD 1 # β Without this, import std; will fail!
)
# GCC 15.1 specific module flags
target_compile_options(Library1 PUBLIC -fmodules-ts)
// Module interface using import std
export module math_lib;
import std; // This works with proper CMake configuration!
export namespace MathLib {
int add(int a, int b) {
return a + b;
}
void print_result(std::string_view operation, int result) {
std::cout << std::format("{} result: {}\n", operation, result);
}
}
add_executable(App1 main.cpp)
# Link with the library
target_link_libraries(App1 PRIVATE Library1)
# CRITICAL: Must set CXX_MODULE_STD 1 for any target using import std
set_target_properties(App1 PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
CXX_MODULE_STD 1 # β This is required for import std; to work!
)
# GCC 15.1 specific module flags
target_compile_options(App1 PRIVATE -fmodules-ts)
// Using both import std and custom modules
import std; // Full standard library access
import math_lib; // Custom module
int main() {
std::println("Hello from modern C++23!");
int result = MathLib::add(10, 5);
MathLib::print_result("Addition", result);
// Use modern C++23 features
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::println("Vector size: {}", numbers.size());
return 0;
}
# Configure (GCC 15.1 must be default or specified)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -G Ninja
# Build (std module built automatically by CMake)
cmake --build build
# Run
./build/App1/App1
./build/App2/App2
import std;
^~~~~~
Solution: Add CXX_MODULE_STD 1
to target properties. This is the most common mistake!
Solution: You need CMake 4.1+ for experimental import std support.
Solution: Don't use Clang flags with GCC. Use only -fmodules-ts
.
Solution: Ensure all targets that use import std;
have CXX_MODULE_STD 1
property.
CXX_MODULE_STD 1
- This property is not well documented but absolutely required- CMake 4.1+ - Earlier versions don't support experimental import std
- Exact UUID - The experimental feature UUID must be exact
- Every target - Every target using
import std;
needs the property, not just the root - C++23 - C++20 is not sufficient for full std module support
Create this minimal test to verify everything works:
import std;
int main() {
std::println("import std; is working!");
std::vector<int> v = {1, 2, 3};
std::println("Vector has {} elements", v.size());
auto result = std::format("Formatted: {}", 42);
std::println(result);
return 0;
}
add_executable(test_import_std test_import_std.cpp)
set_target_properties(test_import_std PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
CXX_MODULE_STD 1 # Critical!
)
target_compile_options(test_import_std PRIVATE -fmodules-ts)
If this builds and runs, your setup is correct!
g++ --version # Should be 15.1+
g++ -fmodules-ts -xc++ -std=c++23 -c /dev/null # Should not error
cmake --version # Should be 4.1+
cmake --build build --verbose
# Look for -fmodules-ts flags and std module compilation
{
"version": "2.0.0",
"tasks": [
{
"label": "CMake Build",
"type": "shell",
"command": "cmake",
"args": ["--build", "build"],
"group": {"kind": "build", "isDefault": true},
"presentation": {"echo": true, "reveal": "always"}
}
]
}
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug App",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/App1/App1",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "CMake Build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
With this setup working, you can use:
import std;
- Complete standard library as modulestd::println()
- Modern C++23 print functionstd::format()
- Advanced string formatting- Custom modules with clean interfaces
- Fast incremental compilation
- Full debugging support in VS Code
Please share this with other C++ developers! The combination of:
- Missing
CXX_MODULE_STD 1
property - Experimental CMake feature with specific UUID
- Need for CMake 4.1+ (not just 3.28)
...has caused countless hours of frustration. Let's save the community time!
This configuration guide is provided under CC0 - use it freely to help other developers!
Keywords for searchability: GCC 15.1, CMake 4.1, import std, C++23, modules, CXX_MODULE_STD, CMAKE_EXPERIMENTAL_CXX_IMPORT_STD, std module, unknown compiled module interface