尝试获取“import std;”时出错在 CMake 项目中工作

问题描述 投票:0回答:1

通过遵循 Andreas Weis 关于模块入门的精彩演讲,我成功地让自己的简单模块正常工作,但我在真正想要实现的目标(即导入标准库)方面遇到了麻烦。

我已经尝试了这个答案(也是由Andreas Weis)中建议的解决方案,但它的最后更新是在5个月前,似乎对我不起作用,不幸的是我没有足够的声誉来发表回复到它。

除了这两篇之外,我还没有找到任何关于用CMake构建STL的相关信息。


这是我当前的设置:

# 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 "")

但是我遇到了一大堆相同的错误:

[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 
c++ cmake stl c++23 c++-modules
1个回答
0
投票

问题是 CMake 使用 clang 而不是 cl 来编译我从 Visual Studio 获得的 STL 模块。切换到 Visual Studio 的 Developer Powershell 并重建后,一切都编译没有问题。

© www.soinside.com 2019 - 2024. All rights reserved.