As we know, C++23 support Standard Library Modules. Until May 2023, MSVC support it but we need add Standard Library Modules manually as Microsoft blog mentioned.
But how to use import std
in CMake project? The MS blog doesn't mentioned it. And these files can't work.(The std.ifc
file is obtained from microsoft blog tutorial:cl /std:c++latest /EHsc /nologo /W4 /MTd /c "%VCToolsInstallDir%\modules\std.ixx"
(use in msvc x64 native console))
CMakeList.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.26)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a")
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP ON)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${CMAKE_PROJECT_NAME})
set(CMAKE_CXX_STANDARD 23)
project(1-1)
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME}
PUBLIC
FILE_SET all_my_modules TYPE CXX_MODULES FILES
main.cpp
std.ifc
)
main.cpp
import std;
using namespace std;
int main(){
cout<<"Hello\n";
}
And MSVC shows:
[build] main.cpp(1,11): error C2230: Could not find module "std"
[build] main.cpp(5,5): error C2065: "cout" : Undeclared identifier
I can use copy %VCToolsInstallDir\modules\std.ixx
to project folder and change std.ifc
to std.ixx
, but is there a more elegant way to achieve it to avoid building std module every time? I think it's because .ifc
is not a source file,how to deal with it in CMake?
CMakeLists.txt
andmain.cpp
files you show? What happens? What errors do you get? – Tweezestd.io
is not a C++23 module. There arestd
andstd.compat
; that's it. – Ellordstd
. – Thesaurus