9

Suppose I have installed some library with vcpkg and it's numerous dependencied (say cgal). Now I want to compile some program against these libraries with CMake.

How should I tell CMake about all locations of all libraries I have downloaded? Including main library I have installed? I have only one setting in CMake, called "source directory" which will point to my code. Where are settings for libraries?


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
Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

11

Normally you need to set; CMAKE_TOOLCHAIN_FILE and VCPKG_TARGET_TRIPLET.

Set VCPKG_TARGET_TRIPLET to the vcpkg triplet that you are using. The default is x86-windows

Set CMAKE_TOOLCHAIN_FILE to point to path_to_vcpkg\scripts\buildsystems\vcpkg.cmake

Then you can use cmake functions such as find_package to find the required package.

see https://github.com/microsoft/vcpkg/blob/master/docs/examples/installing-and-using-packages.md#cmake-toolchain-file for more information.

bertubezz
  • 361
  • 1
  • 8
  • What does "Set CMAKE_TOOLCHAIN_FILE" mean? Does it mean use set in the shell? Does it mean add a set line to the CMakeLists.txt? Does it mean use -DCMAKE... on the cmake command line? – sigfpe Apr 27 '21 at 17:04
  • 2
    @sigfpe Use it as -DCMAKE_TOOLCHAIN_FILE=path_to_vcpkg\scripts\buildsystems\vcpkg.cmake I'm not sure about other ways, but this one works Make sure to do "vcpkg integrate install" first. – shubhamgoel27 May 07 '21 at 11:10
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

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
        )

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

#include <json/json.h>
# Rest of code here...
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Maykon Meneghel
  • 335
  • 6
  • 8