【问题标题】:cmake find_package for libxml2 doesn't return valid header directorieslibxml2 的 cmake find_package 不返回有效的头目录
【发布时间】:2019-02-22 02:47:16
【问题描述】:

我在 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”。 OK,然后make 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”选项。我该如何解决?

【问题讨论】:

    标签: c++ cmake compilation include libxml2


    【解决方案1】:

    只要我使用find_package(LibXml2) 而不是find_package(LIBXML2 CONFIG),就可以正常工作。根据cmake-documentation,config-mode 会尝试查找包提供的配置文件:

    配置模式搜索尝试查找由 要找到的包。名为 _DIR 的缓存条目是 创建来保存包含该文件的目录。默认情况下 命令搜索名称为 的包。如果 NAMES 选项被赋予它后面的名称,而不是 .该命令搜索一个名为 Config.cmake 或 -config.cmake 对于每个指定的名称。一组可能的配置替换 文件名可以使用 CONFIGS 选项给出。

    所以在我看来,好像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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      相关资源
      最近更新 更多