I am using Visual Studio 2022 and I am completely confused. The project is CMAKE based and builds fine on the command line and in Visual Studio 2019. But in vs2022 it will not build with either Visual Studio 17 2022 or Visual Studio 16 2019 selected as a compiler.
The error is always:
Fatal error C1083: Cannot open include file: 'Eigen/Dense': No such file or directory exists.
This is a minimal example I created to test my sanity.
test.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"
}
]
}
Is Visual Studio 2022 with CMake just bugged, or what does I do wrong? I'm confused since the project files seem to be fine because why should it even build on command line and in vs2019?
Does anyone have any clue for me?
EDIT 1 - The cmake log inside visual studio 2022 (when configuring 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.
I fixed it by adding the following to the CMakeLists.txt file
if (CMAKE_VERSION MATCHES "-msvc")
include_directories(${Eigen3_INCLUDE_DIR})
endif()
So now when the project is built from within the Visual Studio UI, the Eigen3_INCLUDE_DIR
is extra included so that Visual Studio will work. Why this is necessary in VS2022 and not in VS2019 is still a mystery to me.