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
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 Moduleswhich needs to be toYesand will then automatically build the standard library modules on/std:c++latestas part of your project build.
See also: How to use c++20 modules with CMake?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With