Dmitrii Motorygin
Dmitrii Motorygin

Reputation: 516

How to link a library installed by vcpkg using CMake?

I installed a third party library using vcpkg. If I write full path to library in include_directories and etc. commands it works but this is silly. I'm trying to include this library using proper approach but CMake returns an error.

I tried advices from similar questions but thus far I was unable to solve the problem

This is my CMakeLists.txt

SET(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")

project(ESMTOOLKIT)

cmake_minimum_required(VERSION 3.15.5)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../bin)

find_package(LibXml2 REQUIRED)

#include_directories(C:/Users/user/source/vcpkg/vcpkg/installed/x64-windows/include include)
include_directories(${LIBXML2_INCLUDE_DIRS} include)
#link_directories(C:/Users/user/source/vcpkg/vcpkg/installed/x64-windows/lib)

set(SOURCES xmlUtils.cpp include/xmlUtils.h pch.cpp include/pch.h esmReader.cpp)

add_executable(esmToolkit ${SOURCES})

#target_link_libraries(esmToolkit libxml2.lib)
target_link_libraries(esmToolkit PRIVATE ${LIBXML2_LIBRARIES})

add_subdirectory(subrecords)
add_subdirectory(records)

This is the error message that I get:

Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
CMake Error at C:/Program Files/CMake/3_24/share/cmake-3.24/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find LibXml2 (missing: LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
Call Stack (most recent call first):
  C:/Program Files/CMake/3_24/share/cmake-3.24/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/CMake/3_24/share/cmake-3.24/Modules/FindLibXml2.cmake:108 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:9 (find_package)


Configuring incomplete, errors occurred!
See also "C:/Users/user/source/repos/esmToolkit/build/CMakeFiles/CMakeOutput.log".

I have the following environmental variables set:

VCPKG_DEFAULT_TRIPLET x64-windows
VCPKG_ROOT C:\Users\user\source\vcpkg\vcpkg

What am I doing wrong?

Upvotes: 0

Views: 2713

Answers (2)

Dmitrii Motorygin
Dmitrii Motorygin

Reputation: 516

Okay, I fixed an issue though I'm not sure what in particular fixed it.

First, I found that I have path to CMake missing from Path environmental variable (C:\Program Files\CMake\3_24\bin).

Second, I removed link_directories command and replaced include_directories command with a target_include_directories.

Third, I changed how I use target_link_libraries command. The correct way for my case is the following: target_link_libraries(esmToolkit PRIVATE LibXml2::LibXml2)

Now CMake generates correct solution from both command line and CMake-GUI

Upvotes: 0

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38834

CMAKE_TOOLCHAIN_FILE may not be specfied in CMakeLists.txt:

This variable is specified on the command line when cross-compiling with CMake.

Therefore you must run it in PowerShell

cmake -DCMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"`

or in the developer command prompt

cmake -DCMAKE_TOOLCHAIN_FILE "%VCPKG_ROOT%/scripts/buildsystems/vcpkg.cmake"`

Ensure you define VCPKG_ROOT environment variable properly, or use the actual full path to vcpkg.cmake, see the guide Get started with vcpkg.

Note, cmake_minimum_required() should be on the top line of CMakeLists.txt, set(CMAKE_CXX_STANDARD 17) should be located prior the project() directive.

vcpkg's find_package() can be case-sensive, it's better to use the official name libxml2.

cmake_minimum_required(VERSION 3.15.5)
set(CMAKE_CXX_STANDARD 17)

project(ESMTOOLKIT)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../bin)

find_package(libxml2 REQUIRED)

include_directories(${LIBXML2_INCLUDE_DIRS} include)

set(SOURCES xmlUtils.cpp include/xmlUtils.h pch.cpp include/pch.h esmReader.cpp)

add_executable(esmToolkit ${SOURCES})

target_link_libraries(esmToolkit PRIVATE LibXml2::LibXml2)

For example I run

git clone [email protected]:microsoft/vcpkg.git C:/Work/3rd/vcpkg

and this works well for me

cmake -S . -B build -D CMAKE_TOOLCHAIN_FILE=C:/Work/3rd/vcpkg/scripts/buildsystems/vcpkg.cmake

Upvotes: 0

Related Questions