c++ - libxml2 的 cmake find_package 不返回有效的头目录

标签 c++ cmake compilation include libxml2

我在 ubuntu 上安装了 gcc、cmake、libxml2-dev 并且我有这个 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 2.8)
find_package(LIBXML2 CONFIG)

include_directories(${LIBXML2_INCLUDE_DIRS})
link_libraries(${LIBXML2_LIBRARY})

add_executable(testusage testusage.cpp)

我运行“cmake”。好的,然后让 VERBOSE=1 它给出编译错误:

Scanning dependencies of target testusage
[ 50%] Building CXX object CMakeFiles/testusage.dir/testusage.cpp.o
/home/a/proj/mynet/mytest/cpp3p/useXml2/testusage.cpp:1:9: fatal error: libxml/parser.h: no such file or directory
 #include<libxml/parser.h>
         ^~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/testusage.dir/build.make:62: recipe for target 'CMakeFiles/testusage.dir/testusage.cpp.o' failed
make[2]: *** [CMakeFiles/testusage.dir/testusage.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testusage.dir/all' failed
make[1]: *** [CMakeFiles/testusage.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

似乎“include_directories”没有在 Makefile 中添加任何“-I”选项。我该如何解决?

最佳答案

只要我使用 find_package(LibXml2) 而不是 find_package(LIBXML2 CONFIG),对我来说工作正常。根据cmake-documentation ,配置模式试图找到一个配置文件,由包提供:

Config mode search attempts to locate a configuration file provided by the package to be found. A cache entry called _DIR is created to hold the directory containing the file. By default the command searches for a package with the name . If the NAMES option is given the names following it are used instead of . The command searches for a file called Config.cmake or -config.cmake for each name specified. A replacement set of possible configuration file names may be given using the CONFIGS option.

所以在我看来,好像 libxml2 没有提供这样的配置文件。

testusage.cpp

#include <iostream>
#include <libxml/parser.h>

int main(void)
{
  xmlParserNodeInfo info;

  std::cout << std::hex << &info << std::endl;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
find_package(LibXml2)

include_directories(${LIBXML2_INCLUDE_DIRS})
link_libraries(${LIBXML2_LIBRARY})

add_executable(testusage testusage.cpp)

调用和构建

$ cmake .

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.4") 
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/test/build

$ make --trace

Makefile:176: target 'cmake_check_build_system' does not exist
/usr/bin/cmake -H/tmp/test -B/tmp/test/build --check-build-system CMakeFiles/Makefile.cmake 0
Makefile:83: update target 'all' due to: cmake_check_build_system
/usr/bin/cmake -E cmake_progress_start /tmp/test/build/CMakeFiles /tmp/test/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
CMakeFiles/Makefile2:67: target 'CMakeFiles/testusage.dir/all' does not exist
make -f CMakeFiles/testusage.dir/build.make CMakeFiles/testusage.dir/depend
CMakeFiles/testusage.dir/build.make:112: target 'CMakeFiles/testusage.dir/depend' does not exist
cd /tmp/test/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /tmp/test /tmp/test /tmp/test/build /tmp/test/build /tmp/test/build/CMakeFiles/testusage.dir/DependInfo.cmake --color=
Scanning dependencies of target testusage
make -f CMakeFiles/testusage.dir/build.make CMakeFiles/testusage.dir/build
CMakeFiles/testusage.dir/build.make:62: update target 'CMakeFiles/testusage.dir/testusage.cpp.o' due to: ../testusage.cpp /usr/include/libxml2/libxml/SAX.h /usr/include/libxml2/libxml/SAX2.h /usr/include/libxml2/libxml/dict.h /usr/include/libxml2/libxml/encoding.h /usr/include/libxml2/libxml/entities.h /usr/include/libxml2/libxml/globals.h /usr/include/libxml2/libxml/hash.h /usr/include/libxml2/libxml/list.h /usr/include/libxml2/libxml/parser.h /usr/include/libxml2/libxml/threads.h /usr/include/libxml2/libxml/tree.h /usr/include/libxml2/libxml/valid.h /usr/include/libxml2/libxml/xlink.h /usr/include/libxml2/libxml/xmlIO.h /usr/include/libxml2/libxml/xmlautomata.h /usr/include/libxml2/libxml/xmlerror.h /usr/include/libxml2/libxml/xmlexports.h /usr/include/libxml2/libxml/xmlmemory.h /usr/include/libxml2/libxml/xmlregexp.h /usr/include/libxml2/libxml/xmlstring.h /usr/include/libxml2/libxml/xmlversion.h CMakeFiles/testusage.dir/flags.make
/usr/bin/cmake -E cmake_echo_color --switch= --green --progress-dir=/tmp/test/build/CMakeFiles --progress-num=1 "Building CXX object CMakeFiles/testusage.dir/testusage.cpp.o"
[ 50%] Building CXX object CMakeFiles/testusage.dir/testusage.cpp.o
/usr/bin/c++   -I/usr/include/libxml2   -o CMakeFiles/testusage.dir/testusage.cpp.o -c /tmp/test/testusage.cpp
CMakeFiles/testusage.dir/build.make:95: update target 'testusage' due to: CMakeFiles/testusage.dir/link.txt CMakeFiles/testusage.dir/testusage.cpp.o CMakeFiles/testusage.dir/build.make /usr/lib/x86_64-linux-gnu/libxml2.so
/usr/bin/cmake -E cmake_echo_color --switch= --green --bold --progress-dir=/tmp/test/build/CMakeFiles --progress-num=2 "Linking CXX executable testusage"
[100%] Linking CXX executable testusage
/usr/bin/cmake -E cmake_link_script CMakeFiles/testusage.dir/link.txt --verbose=
/usr/bin/cmake -E cmake_echo_color --switch= --progress-dir=/tmp/test/build/CMakeFiles --progress-num=1,2 "Built target testusage"
[100%] Built target testusage
/usr/bin/cmake -E cmake_progress_start /tmp/test/build/CMakeFiles 0

$./testusage

0x7ffe0c7a5360

关于c++ - libxml2 的 cmake find_package 不返回有效的头目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54819344/

相关文章:

python - Aptana Studio (Windows) 删除了使用 py_compile.compile 编译的 PYC 文件

c++ - 如何在 C++ 中声明/定义相互依赖的模板?

C++ 相当于 perror?

android-studio - Android Studio 可以使用系统 CMake 吗?

c++ - 为什么mapped_file::data 返回 char* 而不是 void*

linux - CMake 设置环境变量

linux -/usr/bin/ld : cannot open output file assimp2json: Is a directory

compilation - 无法为代码::Blocks编译wxWidgets

c++ - 使用 double 时 ARMv6 上的“总线错误”

C++调试: Terminated with SIGABRT