我正在使用 Visual Studio 2022,我完全困惑了。该项目基于 CMAKE,在命令行和 Visual Studio 2019 中构建良好。但在 vs2022 中,它不会使用选择作为编译器的 Visual Studio 17 2022 或 Visual Studio 16 2019 进行构建。
错误总是:
Fatal error C1083: Cannot open include file: 'Eigen/Dense': No such file or directory exists.
这是我为了测试我的理智而创建的一个最小示例。
测试.cpp:
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(TEST)
set (CMAKE_CXX_STANDARD 20)
find_package(EIGEN3 REQUIRED)
add_executable (${PROJECT_NAME}
test.cpp
)
target_link_libraries (${PROJECT_NAME} Eigen3::Eigen)
CMakePresets.json:
{
"version": 2,
"cmakeMinimumRequired": {
"major": 3,
"minor": 25,
"patch": 0
},
"configurePresets": [
{
"name": "windows-vs22",
"displayName": "Windows x64 Release",
"description": "Default Windows CMake preset",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_MODULE_PATH": "${sourceDir}/conan/install/Release",
"CMAKE_PREFIX_PATH": "${sourceDir}/conan/install/Release"
},
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
},
{
"name": "windows-vs19",
"displayName": "Windows x64 Release",
"description": "Default Windows CMake preset",
"generator": "Visual Studio 16 2019",
"binaryDir": "${sourceDir}/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_MODULE_PATH": "${sourceDir}/conan/install/Release",
"CMAKE_PREFIX_PATH": "${sourceDir}/conan/install/Release"
},
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
}
],
"buildPresets": [
{
"name": "windows-vs22",
"displayName": "Release",
"configurePreset": "windows-vs22"
},
{
"name": "windows-vs19",
"displayName": "Release",
"configurePreset": "windows-vs19"
}
]
}
带有 CMake 的 Visual Studio 2022 是否有 bug,或者我做错了什么? 我很困惑,因为项目文件似乎很好,为什么它甚至应该在命令行和 vs2019 中构建?
有人可以告诉我吗?
编辑 1 - Visual Studio 2022 内的 cmake 日志(配置 2019 时):
Working directory: C:/Users/USERNAME/Desktop/mini_eigen/build/windows-vs19
1> [CMake] -- CMake Version: 3.29.5-msvc4
1> [CMake] -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19045.
1> [CMake] -- The C compiler identification is MSVC 19.29.30154.0
1> [CMake] -- The CXX compiler identification is MSVC 19.29.30154.0
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Conan: Component target declared 'Eigen3::Eigen'
1> [CMake] -- Configuring done (4.6s)
1> [CMake] -- Generating done (0.0s)
1> [CMake] -- Build files have been written to: C:/Users/USERNAME/Desktop/mini_eigen/build/windows-vs19
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted toolchain configurations.
1> Extracted includes paths.
1> CMake generation finished.
我通过将以下内容添加到 CMakeLists.txt 文件来修复它
if (CMAKE_VERSION MATCHES "-msvc")
include_directories(${Eigen3_INCLUDE_DIR})
endif()
因此,现在当从 Visual Studio UI 中构建项目时,会额外包含
Eigen3_INCLUDE_DIR
,以便 Visual Studio 能够正常工作。为什么这在 VS2022 中是必要的,而不是在 VS2019 中对我来说仍然是个谜。