Vcpkg: How to modify `find_package(a_pkg)` to get a_pkg_DIR?

Created on 29 Oct 2020  Â·  2Comments  Â·  Source: microsoft/vcpkg

I have found servel pkg can not obtain the pkg_DIR, for example,

GTest_DIR
mathgl_DIR

SET(CMAKE_TOOLCHAIN_FILE "E:/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
...
find_package(GTest CONFIG REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
message(gtest_include_dir=${GTEST_INCLUDE_DIR})
message(gtest_library=${GTEST_LIBRARY})

throw

1> [CMake] gtest_include_dir=
1> [CMake] gtest_library=

and
find_package(mathgl CONFIG REQUIRED)
throw

1> [CMake] Could not find a package configuration file provided by "mathgl" with any
1> [CMake] of the following names:
1> [CMake]
1> [CMake] mathglConfig.cmake
1> [CMake] mathgl-config.cmake
1> [CMake]
1> [CMake] Add the installation prefix of "mathgl" to CMAKE_PREFIX_PATH or set
1> [CMake] "mathgl_DIR" to a directory containing one of the above files.

question

Most helpful comment

There are 2 different issues here:

Linking GTest

When you vcpkg install gtest it will print out the following instructions for linking, so you should use target_link_libraries() and that should add the correct include directories.

    find_package(GTest CONFIG REQUIRED)
    target_link_libraries(main PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main)

Linking mathgl

There is a bug in the port, the CMake integration files are not produced correctly.

So the printed instructions won't work:

The package mathgl:x64-windows provides CMake targets:

    find_package(mathgl CONFIG REQUIRED)
    target_link_libraries(main PRIVATE mgl mgl-static)

You can workaround this by setting the include directories and library path manually.

Below is an example CMakeLists.txt that links both libraries:

cmake_minimum_required(VERSION 3.14)
project(example CXX)

add_executable(example main.cpp)

# Link GTest
find_package(GTest CONFIG REQUIRED)

# Link MathGL (work around broken config files, that make `find_package(mgl CONFIG REQUIRED)` fail)
find_path(MATHGL_INCLUDE_DIR mgl2/mgl.h)
find_library(MATHGL_LIBRARY mgl)

target_include_directories(example PRIVATE ${MATHGL_INCLUDE_DIR})
target_link_libraries(example 
    PRIVATE GTest::gtest GTest::gmock GTest::gtest_main GTest::gmock_main ${MATHGL_LIBRARY})

All 2 comments

There are 2 different issues here:

Linking GTest

When you vcpkg install gtest it will print out the following instructions for linking, so you should use target_link_libraries() and that should add the correct include directories.

    find_package(GTest CONFIG REQUIRED)
    target_link_libraries(main PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main)

Linking mathgl

There is a bug in the port, the CMake integration files are not produced correctly.

So the printed instructions won't work:

The package mathgl:x64-windows provides CMake targets:

    find_package(mathgl CONFIG REQUIRED)
    target_link_libraries(main PRIVATE mgl mgl-static)

You can workaround this by setting the include directories and library path manually.

Below is an example CMakeLists.txt that links both libraries:

cmake_minimum_required(VERSION 3.14)
project(example CXX)

add_executable(example main.cpp)

# Link GTest
find_package(GTest CONFIG REQUIRED)

# Link MathGL (work around broken config files, that make `find_package(mgl CONFIG REQUIRED)` fail)
find_path(MATHGL_INCLUDE_DIR mgl2/mgl.h)
find_library(MATHGL_LIBRARY mgl)

target_include_directories(example PRIVATE ${MATHGL_INCLUDE_DIR})
target_link_libraries(example 
    PRIVATE GTest::gtest GTest::gmock GTest::gtest_main GTest::gmock_main ${MATHGL_LIBRARY})

@vicroms thanks your help and your patience, solved my problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

spindensity picture spindensity  Â·  3Comments

LilyWangL picture LilyWangL  Â·  3Comments

madkoala picture madkoala  Â·  3Comments

ThinkalVB picture ThinkalVB  Â·  3Comments

angelmixu picture angelmixu  Â·  3Comments