Cannot build a cmake project in Visual Studio and vcpkg (fatal error C1083)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 22:50:16

问题


I installed vcpkg today, enabled integration with Visual Studio, ie .\vcpkg integrate install, and started installing libraries.

I basically installed cpprestsdk and that trigger installation of boost libraries. Then I opened the project in the Visual Studio (CMake).

When I installed cpprestsdk I received this message:

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)

So my 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} )

However when I build the project I am taking some errors regarding 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

and

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

In my powershell I see those libraries installed

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


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

Any ideas, what is going on. I have already lost hours with this.

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                                                                                           

回答1:


From what I can tell it looks like you just forgot to configure your CMake toolchain file. When you enabled the vcpkg integration, you should have gotten a message that said something like:

CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=(your vcpkg directory here)/scripts/buildsystems/vcpkg.cmake"

If you're using Visual Studio 2017, you just have to go to the CMake menu at the top, go down to "Change CMake Settings", and click on your project's name. This will open up a CMakeSettings.json file. It will look something like this:

{
  // 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": ""
    },

To set up your buildchain, you're going to have to let CMake know where the vcpkg tool chain file is. To do this, add the following line under ctestCommandArgs for all four configurations.

"variables": [
  {
    "name": "CMAKE_TOOLCHAIN_FILE",
    "value": "<LOCATION OF THE FILE> (reintegrate vcpkg and it'll tell you again)"
  }
]

I also recommend you do the following:

  • Change the generator from Ninja to Visual Studio
  • Specify a native 64bit buildchain for the x64 configurations
  • Remove the -v thing under build command args

This is how my CMakeSettings.json file looks:

{
  // 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" ]
    }
  ]
}

Notice that the x64 configurations pass use the -T host=x64 command line argument, as well as specifically declaring the generator as Visual Studio 15 2017 Win64, rather than just Visual Studio 15. If you don't specify a native x64 host, Visual Studio will default to x86.




The problem with the way you set up your file as a work around (if it worked at all), is that since you were specifically specifying directories and whatnot, you lost a lot of the convenience of using a build system in the first place.

When you get a chance, I really recommend you watch this video on how to use CMake effectively. He talks about things like not using the INCLUDE_DIRECTORIES command, because that command works at the directory level, rather than on a per-target basis.

I'm sure you only did all that out of desperation trying to get CMake to work (we've all been there), but it's something to keep in mind; it definitely blew my mind the first time I saw using those super convenient and helpful commands was actually doing me more harm than good.

For example, rather than explicitly setting the C++ standard flag the way you did in your CMakeLists.txt, try this next time:

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
)

You get the idea. Basically you want to only configure your project, not actually set the flags yourself, otherwise you're only making it harder for yourself to keep up with the maintenance and configuration of your project over time.

Hope this helps, good luck!



来源:https://stackoverflow.com/questions/50291077/cannot-build-a-cmake-project-in-visual-studio-and-vcpkg-fatal-error-c1083

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!