【问题标题】:How to feed CMake with vcpkg information?如何为 CMake 提供 vcpkg 信息?
【发布时间】:2019-12-09 14:09:59
【问题描述】:

假设我用vcpkg 安装了一些库,并且它有很多依赖(比如cgal)。现在我想用CMake针对这些库编译一些程序。

我应该如何告诉CMake 我下载的所有库的所有位置?包括我安装的主库?我在CMake 中只有一个设置,称为“源目录”,它将指向我的代码。库的设置在哪里?


D:\Users\ThirdPartyDesign\CGAL-5.0-examples\CGAL-5.0\examples\Triangulation_2
λ env | grep CMAKE
CMAKE_TOOLCHAIN_FILE=D:\Users\ThirdPartyDesign\vcpkg\scripts\buildsystems\vcpkg.cmake



D:\Users\ThirdPartyDesign\CGAL-5.0-examples\CGAL-5.0\examples\Triangulation_2
λ cmake .
CMake Warning at CMakeLists.txt:18 (find_package):
  By not providing "FindCGAL.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "CGAL", but
  CMake did not find one.

  Could not find a package configuration file provided by "CGAL" with any of
  the following names:

    CGALConfig.cmake
    cgal-config.cmake

  Add the installation prefix of "CGAL" to CMAKE_PREFIX_PATH or set
  "CGAL_DIR" to a directory containing one of the above files.  If "CGAL"
  provides a separate development package or SDK, be sure it has been
  installed.


-- This program requires the CGAL library, and will not be compiled.
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Users/ThirdPartyDesign/CGAL-5.0-examples/CGAL-5.0/examples/Triangulation_2

【问题讨论】:

    标签: c++ cmake vcpkg


    【解决方案1】:

    通常你需要设置; CMAKE_TOOLCHAIN_FILE 和 VCPKG_TARGET_TRIPLET。

    将 VCPKG_TARGET_TRIPLET 设置为您正在使用的 vcpkg 三元组。默认为x86-windows

    设置CMAKE_TOOLCHAIN_FILE指向path_to_vcpkg\scripts\buildsystems\vcpkg.cmake

    然后就可以使用find_package等cmake函数找到需要的包了。

    请参阅https://github.com/microsoft/vcpkg/blob/master/docs/examples/installing-and-using-packages.md#cmake-toolchain-file 了解更多信息。

    【讨论】:

    • “设置 CMAKE_TOOLCHAIN_FILE”是什么意思?这是否意味着在shell中使用set?这是否意味着在 CMakeLists.txt 中添加一行?这是否意味着在 cmake 命令行上使用 -DCMAKE...?
    • @sigfpe 将其用作 -DCMAKE_TOOLCHAIN_FILE=path_to_vcpkg\scripts\buildsystems\vcpkg.cmake 我不确定其他方式,但这个可行 确保先执行“vcpkg 集成安装”。
    【解决方案2】:

    我通过vcpck 添加了jsoncpp 包。我使用以下命令将 vcpkg 存储库克隆到外部文件夹,我什至将它放在 install_dependencies.sh 文件中:

    DIR="external"
    git clone https://github.com/Microsoft/vcpkg.git "$DIR/vcpkg"
        cd "$DIR/vcpkg"
        ./bootstrap-vcpkg.sh
        ./vcpkg integrate install
        ./vcpkg install jsoncpp
    

    然后,在 CMakeLists.txt 文件中,我在 add_executable 函数之后添加了以下命令:

    # ...
    add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADERS})
    
    #-- JSONCPP ------------------------------
    
    set(JSON_INC_PATH external/vcpkg/packages/jsoncpp_x64-osx/include)
    target_include_directories(${PROJECT_NAME} PUBLIC ${JSON_INC_PATH})
    
    set(JSON_LIB_PATH external/vcpkg/packages/jsoncpp_x64-osx/lib)
    target_link_directories(${PROJECT_NAME} PUBLIC ${JSON_LIB_PATH})
    
    #----------------------------------------
    
    target_link_libraries(${PROJECT_NAME}
            #...
            jsoncpp
            )
    

    现在重新加载后,我可以使用以下方法包含 json 库:

    #include <json/json.h>
    # Rest of code here...
    

    【讨论】:

      猜你喜欢
      • 2016-09-09
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      相关资源
      最近更新 更多