Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do `import std` with CMake

Tags:

c++

cmake

c++23

According to this and CMake 3.28, we should be able to import std without any extra effort. But I'm getting the error Module 'std' not found with following simple demo.

import std;

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
cmake_minimum_required(VERSION 3.28)
project(module_tst)

set(CMAKE_CXX_STANDARD 23)

add_executable(demo)
target_sources(demo
    PRIVATE
    main.cpp
)

import std works well in Visual Studio, as long as I checked C/C++ -> General -> Scan Sources for Module Dependencies. I found something similar in cmake document that is CXX_SCAN_FOR_MODULES but set it on makes no difference. Is there anything missing?

I'm using latest cmake 3.28 rc1 and VS 17.8.0 Preview 4.0

like image 578
isudfv Avatar asked Feb 28 '26 22:02

isudfv


1 Answers

TLDR: CMake 3.29 does not support header units or import std;.

The CMake Documentation lists this as a limitation as of CMake 3.29:

Limitations

There are a number of known limitations of the current C++ module support in CMake. This does not document known limitations or bugs in compilers as these can change over time.

For all generators:

  • Header units are not supported.
  • No builtin support for import std; or other compiler-provided modules.

There is an issue regarding this topic on kitware's gitlab instance, but it's not linked to a milestone so I don't think we can really tell when this will be implemented.: cxxmodules: Header unit support plan.

As for why the post you mentioned seems to work isn't clear to me, though I suspect it's specific to Visual Studio:

Visual Studio 17.6 now also provides a property C/C++->Language->Build ISO C++23 Standard Library Modules which needs to be to Yes and will then automatically build the standard library modules on /std:c++latest as part of your project build.

See also: How to use c++20 modules with CMake?.

like image 153
Julian Avatar answered Mar 03 '26 12:03

Julian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!