gpt4 book ai didi

c++ - 无法在 Visual Studio 和 vcpkg 中构建 cmake 项目( fatal error C1083)

转载 作者:搜寻专家 更新时间:2023-10-31 00:52:57 26 4
gpt4 key购买 nike

我今天安装了 vcpkg,启用了与 Visual Studio 的集成,即 .\vcpkg integrate install,并开始安装库。

我基本上安装了 cpprestsdk 并触发了 boost 库的安装。然后我在 Visual Studio (CMake) 中打开项目。

当我安装 cpprestsdk 时,我收到了这条消息:

The package cpprestsdk:x86-windows provides CMake targets:

find_package(cpprestsdk REQUIRED)
# Note: 1 targets were omitted
target_link_libraries(main PRIVATE cpprestsdk::cpprest cpprestsdk::cpprestsdk_boost_internal cpprestsdk::cpprestsdk_zlib_internal cpprestsdk::cp
prestsdk_openssl_internal)

The package cpprestsdk:x64-windows provides CMake targets:

find_package(cpprestsdk REQUIRED)
# Note: 1 targets were omitted
target_link_libraries(main PRIVATE cpprestsdk::cpprest cpprestsdk::cpprestsdk_boost_internal cpprestsdk::cpprestsdk_zlib_internal cpprestsdk::cp
prestsdk_openssl_internal)

所以我的 CMakeLists.txt

cmake_minimum_required (VERSION 2.8)

project(CppRestSwaggerClient)

#find_package(Boost REQUIRED)

# THE LOCATION OF OUTPUT BINARIES
set(CMAKE_LIBRARY_DIR ${PROJECT_SOURCE_DIR}/lib)
set(LIBRARY_OUTPUT_PATH ${CMAKE_LIBRARY_DIR})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()


# Update require components as necessary
#find_package(Boost 1.45.0 REQUIRED COMPONENTS ${Boost_THREAD_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_DATE_TIME_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY})

find_package(cpprestsdk REQUIRED)

message("this is ${Boost_INCLUDE_DIRS}")

# build and set path to cpp rest sdk
set(CPPREST_ROOT ${PROJECT_SOURCE_DIR}/../../../vcpkg/packages/cpprestsdk_x86-windows)
set(CPPREST_INCLUDE_DIR ${CPPREST_ROOT}/include)
#set(CPPREST_LIBRARY_DIR ${CPPREST_ROOT}/lib)

include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR} ${Boost_INCLUDE_DIRS})
#include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR})


#SUPPORTING FILES
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData" "Object")
#SOURCE FILES
file(GLOB SOURCE_FILES "api/*" "model/*")

add_library(${PROJECT_NAME} ${SUPPORTING_FILES} ${SOURCE_FILES} )

然而,当我构建项目时,我遇到了一些关于 boost 的错误

c:\code\cpprest-client\multipartformdata.cpp(16): fatal error C1083: Cannot open include file: 'boost/uuid/random_generator.hpp': No such file or directory

c:\code\cpprest-client\api\userapi.h(36): fatal error C1083: Cannot open include file: 'boost/optional.hpp': No such file or directory

在我的 powershell 中,我看到安装了这些库

PS C:\vcpkg\packages> ls .\boost-uuid_x86-windows


Directory: C:\vcpkg\packages\boost-uuid_x86-windows

任何想法,发生了什么。我已经为此浪费了几个小时。

Mode                LastWriteTime         Length Name                                                                                              
---- ------------- ------ ----
d----- 5/11/2018 11:47 AM include
d----- 5/11/2018 11:47 AM share
-a---- 5/11/2018 11:47 AM 46 BUILD_INFO
-a---- 5/11/2018 11:47 AM 405 CONTROL



PS C:\vcpkg\packages> ls .\boost-optional_x64-windows


Directory: C:\vcpkg\packages\boost-optional_x64-windows


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/11/2018 1:25 PM include
d----- 5/11/2018 1:25 PM share
-a---- 5/11/2018 1:25 PM 46 BUILD_INFO
-a---- 5/11/2018 1:25 PM 302 CONTROL

最佳答案

据我所知,您似乎只是忘记配置 CMake 工具链文件。当您启用 vcpkg 集成时,您应该会收到一条消息,内容如下:

CMake 项目应该使用:
"-DCMAKE_TOOLCHAIN_FILE=(这里是你的 vcpkg 目录)/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。如果不指定 native x64 主机,Visual Studio 将 default to x86 .




将文件设置为变通方法(如果它确实有效的话)的问题在于,由于您专门指定了目录和诸如此类的东西,因此您失去了在第一名。

有机会的话真心推荐你watch this video关于如何有效地使用 CMake。他谈到了诸如不使用 INCLUDE_DIRECTORIES 命令之类的事情,因为该命令在目录级别工作,而不是在每个目标的基础上工作。

我敢肯定,您这样做只是出于绝望,试图让 CMake 正常工作(我们都经历过),但请记住这一点;当我第一次看到使用这些 super 方便和有用的命令实际上对我弊大于利时,这绝对让我大吃一惊。

例如,与其像在 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
)

你明白了。基本上,您只想配置您的项目,而不是实际自己设置标志,否则随着时间的推移,您只会让自己更难跟上项目的维护和配置。

希望这对您有所帮助,祝您好运!

关于c++ - 无法在 Visual Studio 和 vcpkg 中构建 cmake 项目( fatal error C1083),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50291077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com