英文:
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?"
英文:
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?
答案1
得分: 0
CMAKE_TOOLCHAIN_FILE
可能不会在 CMakeLists.txt 中指定:
> 在使用 CMake 进行交叉编译时,应在命令行上指定此变量。
因此,您必须在 PowerShell 中运行它
cmake -DCMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"`
或在开发者命令提示符中运行
cmake -DCMAKE_TOOLCHAIN_FILE "%VCPKG_ROOT%/scripts/buildsystems/vcpkg.cmake"`
确保正确定义了 VCPKG_ROOT
环境变量,或者使用实际的完整路径到 vcpkg.cmake
,请参阅指南 Get started with vcpkg。
注意,cmake_minimum_required()
应该在 CMakeLists.txt 的顶部行,set(CMAKE_CXX_STANDARD 17)
应该位于 project()
指令之前。
vcpkg 的 find_package()
可能区分大小写,最好使用官方名称 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)
例如,我运行了
git clone git@github.com:microsoft/vcpkg.git C:/Work/3rd/vcpkg
而这对我来说运行良好
cmake -S . -B build -D CMAKE_TOOLCHAIN_FILE=C:/Work/3rd/vcpkg/scripts/buildsystems/vcpkg.cmake
英文:
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 git@github.com: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
答案2
得分: 0
好的,我修复了一个问题,尽管我不确定是什么特别的修复。
首先,我发现我缺少了CMake路径,没有在环境变量中添加(C:\Program Files\CMake\3_24\bin)。
其次,我删除了link_directories命令,并用target_include_directories命令替换了include_directories命令。
第三,我改变了使用target_link_libraries命令的方式。对于我的情况,正确的方式如下:target_link_libraries(esmToolkit PRIVATE LibXml2::LibXml2)
现在,CMake可以从命令行和CMake-GUI生成正确的解决方案。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论