如何为 CMake 提供 vcpkg 信息?

Dim*_*ims 6 c++ cmake vcpkg

假设我已经安装了一些库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
Run Code Online (Sandbox Code Playgroud)

小智 6

通常你需要设置;CMAKE_TOOLCHAIN_FILEVCPKG_TARGET_TRIPLET

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

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

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

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

  • @sigfpe 将其用作 -DCMAKE_TOOLCHAIN_FILE=path_to_vcpkg\scripts\buildsystems\vcpkg.cmake 我不确定其他方法,但这个有效 确保首先执行“vcpkg 集成安装”。 (2认同)

小智 6

I added the jsoncpp package through vcpkg. I used the following command to clone the vcpkg repository to the external folder, I even put it in an install_dependencies.sh file:

DIR="external"
git clone https://github.com/Microsoft/vcpkg.git "$DIR/vcpkg"
    cd "$DIR/vcpkg"
    ./bootstrap-vcpkg.sh
    ./vcpkg integrate install
    ./vcpkg install jsoncpp
Run Code Online (Sandbox Code Playgroud)

Then, in the CMakeLists.txt file I added the following commands after the add_executable function:

# ...
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
        )
Run Code Online (Sandbox Code Playgroud)

Now after reloading, I can include the json library using:

#include <json/json.h>
# Rest of code here...
Run Code Online (Sandbox Code Playgroud)