据我所知,您似乎只是忘记配置 CMake 工具链文件。当您启用 vcpkg 集成时,您应该收到一条消息,内容如下:
CMake projects should use:
"-DCMAKE_TOOLCHAIN_FILE=(your vcpkg directory here)/scripts/buildsystems/vcpkg.cmake"
如果您使用的是 Visual Studio 2017,则只需转到顶部的 CMake 菜单,转到“更改 CMake 设置”,然后单击项目名称。这将打开一个 CMakeSettings.json 文件。它看起来像这样:
{
// See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
"configurations": [
{
"name": "x86-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x86" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x86-Release",
"generator": "Ninja",
"configurationType": "Release",
"inheritEnvironments": [ "msvc_x86" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "Release",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "-v",
"ctestCommandArgs": ""
},
要设置构建链,您必须让 CMake 知道 vcpkg 工具链文件的位置。为此,请在下面添加以下行ctestCommandArgs
对于所有四种配置。
"variables": [
{
"name": "CMAKE_TOOLCHAIN_FILE",
"value": "<LOCATION OF THE FILE> (reintegrate vcpkg and it'll tell you again)"
}
]
我还建议您执行以下操作:
- 将生成器从 Ninja 更改为 Visual Studio
- 为 x64 配置指定本机 64 位构建链
- 去除
-v
构建命令参数下的东西
这就是我的CMakeSettings.json
文件看起来:
{
// See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
"configurations": [
{
"name": "x86-Debug",
"generator": "Visual Studio 15 2017",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x86" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": [
{
"name": "CMAKE_TOOLCHAIN_FILE",
"value": "C:/Source-Code/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
]
},
{
"name": "x86-Release",
"generator": "Visual Studio 15 2017",
"configurationType": "Release",
"inheritEnvironments": [ "msvc_x86" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": [
{
"name": "CMAKE_TOOLCHAIN_FILE",
"value": "C:/Source-Code/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
]
},
{
"name": "x64-Debug",
"generator": "Visual Studio 15 2017 Win64",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "-T host=x64",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": [
{
"name": "CMAKE_TOOLCHAIN_FILE",
"value": "C:/Source-Code/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
]
},
{
"name": "x64-Release",
"generator": "Visual Studio 15 2017 Win64",
"configurationType": "Release",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"cmakeCommandArgs": "-T host=x64",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": [
{
"name": "CMAKE_TOOLCHAIN_FILE",
"value": "C:/Source-Code/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
]
},
{
"name": "Linux-Debug",
"generator": "Unix Makefiles",
"remoteMachineName": "${defaultRemoteMachineName}",
"configurationType": "Debug",
"remoteCMakeListsRoot": "/var/tmp/src/${workspaceHash}/${name}",
"cmakeExecutable": "/usr/local/bin/cmake",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"remoteBuildRoot": "/var/tmp/build/${workspaceHash}/build/${name}",
"remoteInstallRoot": "/var/tmp/build/${workspaceHash}/install/${name}",
"remoteCopySources": true,
"remoteCopySourcesOutputVerbosity": "Normal",
"remoteCopySourcesConcurrentCopies": "10",
"remoteCopySourcesMethod": "rsync",
"remoteCopyBuildOutput": false,
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ]
},
{
"name": "Linux-Release",
"generator": "Unix Makefiles",
"remoteMachineName": "${defaultRemoteMachineName}",
"configurationType": "Release",
"remoteCMakeListsRoot": "/var/tmp/src/${workspaceHash}/${name}",
"cmakeExecutable": "/usr/local/bin/cmake",
"buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
"installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
"remoteBuildRoot": "/var/tmp/build/${workspaceHash}/build/${name}",
"remoteInstallRoot": "/var/tmp/build/${workspaceHash}/install/${name}",
"remoteCopySources": true,
"remoteCopySourcesOutputVerbosity": "Normal",
"remoteCopySourcesConcurrentCopies": "10",
"remoteCopySourcesMethod": "rsync",
"remoteCopyBuildOutput": false,
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ]
}
]
}
请注意,x64 配置传递使用-T host=x64
命令行参数,以及具体声明生成器为Visual Studio 15 2017 Win64
,而不仅仅是Visual Studio 15
。如果您不指定本机 x64 主机,Visual Studio 将默认为 x86 https://cmake.org/cmake/help/latest/variable/CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE.html.
您将文件设置为解决方法的方式的问题(如果它确实有效的话),是因为您专门指定了目录之类的东西,所以您首先失去了使用构建系统的很多便利。
当你有机会时,我真的推荐你看这个视频 https://www.youtube.com/watch?v=bsXLMQ6WgIk关于如何有效地使用 CMake。他谈到了诸如不使用INCLUDE_DIRECTORIES
命令,因为该命令在目录级别工作,而不是在每个目标的基础上工作。
我确信您所做的一切只是出于绝望,试图让 CMake 工作(我们都经历过),但需要记住这一点;当我第一次看到使用这些超级方便和有用的命令实际上弊大于利时,这确实让我大吃一惊。
例如,下次不要像在 CMakeLists.txt 中那样显式设置 C++ 标准标志,而是尝试这样做:
CMAKE_MINIMUM_REQUIRED (VERSION 3.8)
PROJECT (CppRestSwaggerClient VERSION 1.0.0 LANGUAGES CXX)
OPTION (TREAT_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
SET_PROPERTY (CppRestSwaggerClient CXX_STANDARD 17)
SET_PROPERTY (CppRestSwaggerClient CXX_STANDARD_REQUIRED ON)
TARGET_COMPILE_FEATURES (CppRestSwaggerClient
PRIVATE
cxx_constexpr
)
你明白了。基本上,您只想配置您的项目,而不是自己实际设置标志,否则您只会让自己更难跟上项目的维护和配置。
希望这有帮助,祝你好运!