【发布时间】:2020-12-24 04:38:21
【问题描述】:
我是一名 Java/Go/Python 开发人员,我试图用 C++ 工具链弄湿我的脚,并且很难强迫 CMake 找到依赖项。我使用apt-get install libxml++2.6-dev 在Debian 上安装了libxml++,并将它安装到/usr/include/libxml++2.6/libxml++。这是一个问题,因为这不是相对于/usr/include 的正确路径——如果我尝试#include <libxml++/libxml++.h> 它显然无法找到它,并且如果我包含诸如#include <libxml++2.6/libxml++/whatever.h> 然后whatever.h 将无法找到搜索libxml++ 路径的其他头文件,例如#include <libxml++/version.h>。库附带了 pkg-config 文件,但我真的不知道如何处理它们,并且使用 find_package(LibXml++) 或 include_directories(${LibXml++_INCLUDE_DIRS}) 之类的东西不起作用。
查看 CMake 文档,似乎有几种方法可以解决每个简单的问题,如果这与 apt-get 安装东西的方式有关,我觉得这一定是一个非常简单的问题,有一个简单的解决方案。有什么我可以添加的东西来强制 CMake 在 /usr/include/[something]/libxml++ 中特别查看并仍然使用相对路径正确导入它,还是我每次使用 apt 安装时都需要移动东西?
抱歉,如果这是重复的,但是“libxml++”对 Google 来说是一个非常困难的库,因为“++”标点符号以及它显然没有像常规 libxml2 那样使用任何地方。我很高兴阅读更多基本资源,但不想通过 30 年的 C++ 演变来得出答案。
供参考:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(sandbox)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost REQUIRED)
find_package(LibXml2 REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ ${LIBXML2_INCLUDE_DIR})
add_executable(sandbox main.cpp)
target_link_libraries(sandbox ${LIBXML2_LIBRARIES})
源文件:
#include <iostream>
#include <libxml++2.6/libxml++/libxml++.h>
int main() {
std::cout << "hello" << std::endl;
}
错误信息:
[main] Building folder: cpp-sandbox
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /workspaces/cpp-sandbox/build --config Debug --target all -- -j 6
[build] Scanning dependencies of target sandbox
[build] [ 50%] Building CXX object CMakeFiles/sandbox.dir/main.cpp.o
[build] In file included from /workspaces/cpp-sandbox/main.cpp:2:
[build] /usr/include/libxml++-2.6/libxml++/libxml++.h:50:10: fatal error: libxml++/exceptions/internal_error.h: No such file or directory
[build] #include <libxml++/exceptions/internal_error.h>
[build] ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] compilation terminated.
[build] make[2]: *** [CMakeFiles/sandbox.dir/build.make:63: CMakeFiles/sandbox.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/sandbox.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2
【问题讨论】:
-
不确定您使用的是哪个确切版本的 CMake,但您是否尝试过简单地链接到
LibXml2::LibXml2导入的 CMake 目标(请参阅 here)?