我在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”选项。我该如何解决?
答案 0 :(得分:1)
只要我使用find_package(LibXml2)
而不是find_package(LIBXML2 CONFIG)
,对我来说就很好。根据{{3}},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