c++cmakestlc++23c++-modules

Errors when trying to get "import std;" to work in a CMake project


I have managed to get my own simple module to work by following Andreas Weis' great talk on getting started with modules, but I am having trouble with what I actually want to achieve, which is importing the standard library.

I have tried the solution suggested in this answer (also by Andreas Weis), but it was last updated 5 months ago and didn't seem to work for me, and unfortunately I don't have enough reputation to post a reply to it.

Other than these two pieces, I have not found any relevant information on building the STL with CMake.


Here's my current setup:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.29)
project(my_project)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(${PROJECT_NAME})

set(stl_path "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.40.33807/modules")

# copy the MSVC-provided STL module to build/std/std.ixx
file(COPY ${stl_path}/std.ixx
    DESTINATION ${PROJECT_BINARY_DIR}/std
)

target_sources(${PROJECT_NAME}
    PUBLIC
        ${PROJECT_SOURCE_DIR}/src/main.cpp
    PRIVATE FILE_SET CXX_MODULES
    FILES
# add my own module (this works fine)
        ${PROJECT_SOURCE_DIR}/mod/my_module.ixx
# attempt to add the STL to my project (this causes errors)
        ${PROJECT_BINARY_DIR}/std/std.ixx
)

# emit compile_commands.json for clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")

But I am getting a whole bunch of the same errors:

[3/5] Building CXX object CMakeFiles/my_project.dir/std/std.ixx.obj
FAILED: CMakeFiles/my_project.dir/std/std.ixx.obj CMakeFiles/my_project.dir/std.pcm
"C:\PROGRA~1\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\x64\bin\clang++.exe"   -O0 -std=gnu++23 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -g -Xclang -gcodeview -MD -MT CMakeFiles/my_project.dir/std/std.ixx.obj -MF CMakeFiles\my_project.dir\std\std.ixx.obj.d @CMakeFiles\my_project.dir\std\std.ixx.obj.modmap -o CMakeFiles/my_project.dir/std/std.ixx.obj -c C:/dev/c/modules-test/build/std/std.ixx
C:/dev/c/modules-test/build/std/std.ixx:34:15: warning: 'std' is a reserved name for a module [-Wreserved-module-identifier]
   34 | export module std;
      |               ^

In file included from C:/dev/c/modules-test/build/std/std.ixx:118:
In file included from C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\algorithm:10:
In file included from C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\__msvc_minmax.hpp:10:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\cstdint:21:1: error: export declaration can only be used within a module purview
   21 | _EXPORT_STD using _CSTD int8_t;
      | ^
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\yvals_core.h:946:21: note: expanded from macro '_EXPORT_STD'
  946 | #define _EXPORT_STD export
      |                     ^
   /* ^^this last one repeats for pretty much every symbol in the library.. */
fatal error: too many errors emitted, stopping now 

Solution

  • The problem was that CMake used clang instead of cl to compile the STL module that I got from Visual Studio. After switching to Visual Studio's Developer Powershell and rebuilding, everything compiled without issues.