Skip to content

Complete guide for C++23 import std; with GCC 15.1 and CMake 4.1. Includes the critical CXX_MODULE_STD property that most guides miss.

Notifications You must be signed in to change notification settings

JRASoftware/cpp23-import-std-guide

Repository files navigation

C++23 import std; with GCC 15.1 and CMake 4.1 - Complete Working Guide

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.

🚨 TL;DR - The Magic Configuration

If you just want it to work, here's the exact setup:

Root CMakeLists.txt:

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)

For EVERY target using import std;:

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)

🎯 Requirements

  • GCC 15.1+ (with libstdc++ module support)
  • CMake 4.1+ (for experimental std import)
  • C++23 standard
  • Ninja (recommended build system)

πŸ“ Complete Working Example

Project Structure:

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

Root CMakeLists.txt:

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)

Library1/CMakeLists.txt:

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)

Library1/math_lib.cppm:

// 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);
    }
}

App1/CMakeLists.txt:

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)

App1/main.cpp:

// 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;
}

πŸ”§ Build Commands

# 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

🚨 Common Errors and Solutions

Error: "unknown compiled module interface: no such module"

import std;
^~~~~~

Solution: Add CXX_MODULE_STD 1 to target properties. This is the most common mistake!

Error: "CMake 3.28 required"

Solution: You need CMake 4.1+ for experimental import std support.

Error: "unrecognized command-line option '-stdlib=libstdc++'"

Solution: Don't use Clang flags with GCC. Use only -fmodules-ts.

Error: Module compilation but linking fails

Solution: Ensure all targets that use import std; have CXX_MODULE_STD 1 property.

🎯 Key Points That Save Hours

  1. CXX_MODULE_STD 1 - This property is not well documented but absolutely required
  2. CMake 4.1+ - Earlier versions don't support experimental import std
  3. Exact UUID - The experimental feature UUID must be exact
  4. Every target - Every target using import std; needs the property, not just the root
  5. C++23 - C++20 is not sufficient for full std module support

πŸ§ͺ Testing Your Setup

Create this minimal test to verify everything works:

test_import_std.cpp:

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;
}

test/CMakeLists.txt:

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!

πŸ” Troubleshooting

Check GCC Version and Modules Support:

g++ --version  # Should be 15.1+
g++ -fmodules-ts -xc++ -std=c++23 -c /dev/null  # Should not error

Check CMake Version:

cmake --version  # Should be 4.1+

Verbose Build for Debugging:

cmake --build build --verbose
# Look for -fmodules-ts flags and std module compilation

πŸ“š VS Code Integration

.vscode/tasks.json:

{
    "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"}
        }
    ]
}

.vscode/launch.json:

{
    "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"
        }
    ]
}

πŸŽ‰ What You Get

With this setup working, you can use:

  • import std; - Complete standard library as module
  • std::println() - Modern C++23 print function
  • std::format() - Advanced string formatting
  • Custom modules with clean interfaces
  • Fast incremental compilation
  • Full debugging support in VS Code

🀝 Share This Guide

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!

πŸ“ License

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

About

Complete guide for C++23 import std; with GCC 15.1 and CMake 4.1. Includes the critical CXX_MODULE_STD property that most guides miss.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages