CMake无法链接CLion

时间:2018-05-15 14:46:20

标签: c++ cmake libxml2 clion

tl; dr将set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )添加到CMakeLists.txt(以下更多信息)。

我想在CLion中运行一个非常简单的应用程序。此应用程序使用libxml2。

我在Ubuntu 18.04 LTS上使用了CLion 2018.1.2,libxml2-dev已正确安装。

我的项目编译为:

g++ -I /usr/include/libxml2 xmlTest.cpp -o xmlTest.exe -lxml2

虽然使用此CMakeLists.txt

在CLion中无法编译
cmake_minimum_required(VERSION 3.10)
project(xmlTest)

set(CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2 -lxml2 -lz -lm")

include_directories( /usr/local/include/libxml2 )

add_executable (xmlTest.exe xmlTest.cpp)

编译器抛出我:

/home/xxxx/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/181.4668.70/bin/cmake/bin/cmake --build /home/xxxx/src/cmake-build-debug --target xmlTest.exe -- -I /usr/include/libxml2 -lxml2
Scanning dependencies of target xmlTest.exe
[ 50%] Building CXX object CMakeFiles/xmlTest.exe.dir/xmlTest.cpp.o
[100%] Linking CXX executable xmlTest.exe
CMakeFiles/xmlTest.exe.dir/xmlTest.cpp.o: In function `main':
/home/xxxx/src/xmlTest.cpp:10: undefined reference to `xmlReadFile'
/home/xxxx/src/xmlTest.cpp:17: undefined reference to `xmlDocGetRootElement'
/home/xxxx/src/xmlTest.cpp:21: undefined reference to `xmlFreeDoc'
/home/xxxx/src/xmlTest.cpp:25: undefined reference to `xmlStrcmp'
/home/xxxx/src/xmlTest.cpp:27: undefined reference to `xmlFreeDoc'

我的申请(xmlTest.cpp):

#include <stdlib.h>
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

int main(int argc, char const *argv[]) {
  xmlDocPtr doc;
  xmlNodePtr cur;

  doc = xmlReadFile(argv[1], NULL, 0);

  if (doc == NULL ) {
    fprintf(stderr,"Document not parsed successfully. \n");
    return 1;
  }

  cur = xmlDocGetRootElement(doc);

  if (cur == NULL) {
    fprintf(stderr,"empty document\n");
    xmlFreeDoc(doc);
    return 1;
  }

  if (xmlStrcmp(cur->name, (const xmlChar *) "users")) {
    fprintf(stderr,"document of the wrong type, root node != story");
    xmlFreeDoc(doc);
    return 1;
  }

  return 0;
}

在摆弄了libxml2和CMake几个小时之后没有成功,我很乐意知道如何告诉CMake libxml2确实存在......

更新#1:

感谢@ vre的建议我更新了我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.10)
project(xmlTest)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2 -lxml2 -lz -lm")
set(CMAKE_CXX "/usr/local/include/libxml2")
find_library(libxml2 libxml2)
link_directories(/usr/local/include/libxml2)
add_compile_options("-I/usr/include/libxml2 -lxml2 -lz -lm")
find_package(LibXml2 REQUIRED)
add_executable (xmlTest.exe xmlTest.cpp)
target_compile_definitions(xmlTest.exe PUBLIC ${LibXml2_DEFINITIONS})
target_include_directories(xmlTest.exe PUBLIC ${LibXml2_INCLUDE_DIRS})
target_link_libraries(xmlTest.exe ${LibXml2_LIBRARIES})

但这并没有改变或解决错误。

更新#2(解决方案):

这是如何在CLion中使用libxml2构建项目:

cmake_minimum_required(VERSION 3.10)
project(xmlTest)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2")
set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )

add_executable (xmlTest.exe xmlTest.cpp)

我的问题是我的CMAKE_CXX_FLAGS中的参数顺序错误。 This article指出了这一点。使用set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )是关键(see this

我希望这有助于未来的时间旅行者:

1 个答案:

答案 0 :(得分:2)

您提出的解决方案有效,但CMake的目的是提供表达平台,编译器和构建工具独立描述项目方式的方法。所有的魔法都发生在FindLibXml2.cmake模块中。并且随着CMake安装,可以使用大量模块。请参阅/usr/share/cmake-3.10/ModulesUbuntu 18.04下的模块目录。

如果您按以下方式重新制定CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(xmlTest)

set(CMAKE_CXX_STANDARD 11)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
elseif(MSVC)
  # add options for Visual C/C++ Compiler here
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()    

find_package(LibXml2 REQUIRED)

add_executable(xmlTest.exe xmlTest.cpp)

target_compile_definitions(xmlTest.exe PUBLIC ${LIBXML2_DEFINITIONS})
target_include_directories(xmlTest.exe PUBLIC ${LIBXML2_INCLUDE_DIRS})
target_link_libraries(xmlTest.exe ${LIBXML2_LIBRARIES})

您可以使用各种平台以及编译器和IDE的组合。这已在Ubuntu 18.04GCC 7.3Windows 10 MSVC 15进行了测试。您需要在Windows上构建更多步骤(即安装和设置libxml2及其所有依赖项)。这个 也可以与CLion一起开箱即用。