CMake构建工程的时候很多程序可以使用写好的库,这就会涉及到库的编译链接过程。这里使用的提到的 Ubuntu下libxml2的安装和使用xml库作为讲解示例,如何安转运行请点击链接。

使用kdevelop新建工程名字XmlDemo,

CMakeLists.txt文件

cmake_minimum_required(VERSION 3.7)project(hello)add_executable(hello main.cpp)install(TARGETS hello RUNTIME DESTINATION bin)

main.cpp文件

#include<stdio.h>
#include<libxml/parser.h>
#include<libxml/tree.h>int main(int argc, char **argv)
{//Define document pointerxmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0");//Define node pointerxmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root");//Set the root element of the documentxmlDocSetRootElement(doc,root_node);//Create child nodes directly in the root nodexmlNewTextChild(root_node,NULL,BAD_CAST"newnode1",BAD_CAST"newnode1 content");xmlNewTextChild(root_node,NULL,BAD_CAST"newnode2",BAD_CAST"newnode2 content");//Create a new nodexmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");//Create a new text nodexmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");//Add a new node to parentxmlAddChild(root_node,node);xmlAddChild(node,content);//Create a new property carried by a nodexmlNewProp(node,BAD_CAST"attribute",BAD_CAST"yes");//Create a son and grandson node elementnode = xmlNewNode(NULL,BAD_CAST"son");xmlAddChild(root_node,node);xmlNodePtr grandson = xmlNewNode(NULL,BAD_CAST"grandson");xmlAddChild(node,grandson);xmlAddChild(grandson,xmlNewText(BAD_CAST"THis is a grandson node"));//Dump an XML document to a fileint nRel = xmlSaveFile("CreatedXmlDemo.xml",doc);if(nRel != -1) {//Free up all the structures used by a document,tree includedxmlFreeDoc(doc);}return 0;
}

如果直接编译的话会提示错误

/home/wang/projects/XmlDemo/build> make -j2
Scanning dependencies of target xmldemo
[ 50%] Building CXX object CMakeFiles/xmldemo.dir/main.cpp.o
/home/wang/projects/XmlDemo/main.cpp:2:26: fatal error: libxml/parser.h: No such file or directory#include<libxml/parser.h>^
compilation terminated.
make[2]: *** [CMakeFiles/xmldemo.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/xmldemo.dir/all] Error 2
make: *** [all] Error 2
*** Failure: Exit code 2 ***

找不到头文件,因此需要修改CMakeLists.txt让让编译系统找到头文件。

cmake_minimum_required(VERSION 3.7)project(xmldemo)include_directories(/usr/include/libxml2)add_executable(xmldemo main.cpp)install(TARGETS xmldemo RUNTIME DESTINATION bin)

修改称上面的样子再进行编译,

/home/wang/projects/XmlDemo/build> make -j2
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wang/projects/XmlDemo/build
Scanning dependencies of target xmldemo
[ 50%] Building CXX object CMakeFiles/xmldemo.dir/main.cpp.o
[100%] Linking CXX executable xmldemo
CMakeFiles/xmldemo.dir/main.cpp.o: In function `main':
/home/wang/projects/XmlDemo/main.cpp:8: undefined reference to `xmlNewDoc'
/home/wang/projects/XmlDemo/main.cpp:11: undefined reference to `xmlNewNode'
/home/wang/projects/XmlDemo/main.cpp:14: undefined reference to `xmlDocSetRootElement'
/home/wang/projects/XmlDemo/main.cpp:17: undefined reference to `xmlNewTextChild'
/home/wang/projects/XmlDemo/main.cpp:18: undefined reference to `xmlNewTextChild'
/home/wang/projects/XmlDemo/main.cpp:21: undefined reference to `xmlNewNode'
/home/wang/projects/XmlDemo/main.cpp:23: undefined reference to `xmlNewText'
/home/wang/projects/XmlDemo/main.cpp:25: undefined reference to `xmlAddChild'
/home/wang/projects/XmlDemo/main.cpp:26: undefined reference to `xmlAddChild'
/home/wang/projects/XmlDemo/main.cpp:28: undefined reference to `xmlNewProp'
/home/wang/projects/XmlDemo/main.cpp:31: undefined reference to `xmlNewNode'
/home/wang/projects/XmlDemo/main.cpp:32: undefined reference to `xmlAddChild'
/home/wang/projects/XmlDemo/main.cpp:33: undefined reference to `xmlNewNode'
/home/wang/projects/XmlDemo/main.cpp:34: undefined reference to `xmlAddChild'
/home/wang/projects/XmlDemo/main.cpp:35: undefined reference to `xmlNewText'
/home/wang/projects/XmlDemo/main.cpp:35: undefined reference to `xmlAddChild'
/home/wang/projects/XmlDemo/main.cpp:38: undefined reference to `xmlSaveFile'
/home/wang/projects/XmlDemo/main.cpp:41: undefined reference to `xmlFreeDoc'
collect2: error: ld returned 1 exit status
make[2]: *** [xmldemo] Error 1
make[1]: *** [CMakeFiles/xmldemo.dir/all] Error 2
make: *** [all] Error 2
*** Failure: Exit code 2 ***

提示undefined reference,这个很容易理解,就是找不到libxml2的实现。

CMakeLists.txt中再增加一行

cmake_minimum_required(VERSION 3.7)project(xmldemo)include_directories(/usr/include/libxml2)add_executable(xmldemo main.cpp)target_link_libraries(xmldemo xml2)install(TARGETS xmldemo RUNTIME DESTINATION bin)

然后进行编译

/home/wang/projects/XmlDemo/build> make -j2
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wang/projects/XmlDemo/build
[ 50%] Linking CXX executable xmldemo
[100%] Built target xmldemo
*** Finished ***

可以看到成功编译完成。

运行之后可以看到生成CreateXmlDemo.xml文件。

wang@wang:~/projects/XmlDemo$ ls
build  CMakeLists.txt  main.cpp  XmlDemo.kdev4
wang@wang:~/projects/XmlDemo$ cd build/
wang@wang:~/projects/XmlDemo/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  xmldemo
wang@wang:~/projects/XmlDemo/build$ ./xmldemo
wang@wang:~/projects/XmlDemo/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CreatedXmlDemo.xml  Makefile  xmldemo

如果觉得这篇文章有用,可以扫免费红包支持。

CMake加入第三方库相关推荐

  1. 【cmake学习】cmake 引入第三方库(头文件目录、库目录、库文件)

    程序的编写需要用到头文件,程序的编译需要lib文件,程序的运行需要dll文件,因此cmake引入第三方库其实就是将include目录.lib目录.bin目录引入工程.         目录 1.fin ...

  2. cmake之 第三方库始终报错 /usr/bin/ld: cannot find -lxxx?

    为什么自己要添加始终?感觉很奇怪? 加载第三方库对于一个程序是很经常碰到的事情. 首先,自己应该很清楚第三方库的位置哈.将库添加到工程中. #注意:动态库简写,全称均可:静态库要全称. TARGET_ ...

  3. VulkanLearning - 环境搭建:使用CLion+cmake链接第三方库

    不知从何时开始,突然不想再打开VS写东西了(这人pass有病(很大一部分原因是找不到如何添加VC工程模板(说白了就是一条菜狗)))加上正要开始学习Vulkan以及一些相关的图形学知识,在工程编译方面要 ...

  4. CMake引入三方库

    做移动端的NDK开发经常需要引入三方库,本文以常见的JSON库为例进行说明 jsoncpp源码下载地址https://github.com/open-source-parsers/jsoncpp 下载 ...

  5. ISP 【一】————boost标准库使用——批量读取保存文件 /boost第三方库的使用及其cmake添加,图像gramma

    CMakeLists.txt文件中需要添加第三方库,并企鹅在CMakeLists.txt中添加 include_directories(${PROJECT_SOURCE_DIR}/../3party/ ...

  6. CMake基础 第8节 包含第三方库

    介绍# 几乎所有重要的项目都需要包含第三方库.头文件或程序.CMake支持使用find_package()函数查找这些工具的路径.这将从CMAKE_MODULE_PATH中的文件夹列表中搜索格式为Fi ...

  7. AS使用NDK Cmake方式依赖第三方库注意事项

    AS2.2以后支持Cmake了,以前的Android.mk的方式可以告别了,官方教程 CMakeLists.txt文件的书写 引入第三方库使用add_library()需要指定是SHARED-> ...

  8. C++-Cmake指令:link_directories【指定第三方库所在路径】【官方不建议使用该命令,取而代之的为find_package()、find_library()】

    LINK_DIRECTORIES 命令来指定第三方库所在路径,比如,你的动态库在/home/myproject/libs这个路径下,则通过命令:LINK_DIRECTORIES(/home/mypro ...

  9. Cmake管理统一版本的SLAM第三方库

    学习SLAM的过程中,都需要安装eigen.opencv等多种第三方库,而这些库往往对应不同版本,有时还会涉及多个版本之间的切换,但SLAM算法在终端树莓派或工控机部署时得重新安装第三方库. 本文使用 ...

最新文章

  1. 很慌!一次惊心动魄的服务器误删文件的恢复过程
  2. ustc小道消息20220104
  3. Mac系统如何配置adb路径
  4. (最优解)L1-028 判断素数 (10分)——17行代码AC
  5. mysql5.7+proxy_mysql 5.7+mysql-proxy 0.8.5 读写分离
  6. Intel OpenCL + Ubuntu安装
  7. (32)System Verilog类class中构造函数new()示例
  8. 基于JAVA+SpringMVC+Mybatis+MYSQL的校园失物招领系统
  9. python与机器学习(五)——决策树
  10. 个人总结OLinux上安装oracle11G Data Guard
  11. C语言函数一章教学,c语言案例教程:函数教学讲义.ppt
  12. 学习笔记:定积分的求解(矩形法)
  13. 思想实验及其在科学发展中的作用
  14. 华为交换机:STP测试实验
  15. Maven手动添加dependency(以Oracle JDBC为例)
  16. 广告学本科--现代管理学--[00107]
  17. 细说中国富豪的9种发家类型
  18. 非匀质化资金池——为什么资产 NFT 化是 DeFi 的必经之路
  19. Python(分治算法)问题 A: 找出伪币_给你一个装有n枚硬币的袋子。n枚硬币中有一个是伪造的,并且那个伪造的硬币比真的硬币要轻一些。你的任务是找出这枚伪造的硬币。
  20. css实现序列帧闪电动效

热门文章

  1. 魔坊APP项目-24-种植园,修复宠物喂食时出现的饱食度没有增加的bug、宠物挂了的bug问题
  2. 小型电梯尺寸_家用别墅小型电梯介绍:品牌、大概价格及尺寸等
  3. 全球最大的Spark+AI峰会发放优惠码SAIS20TRAIN,培训费优惠20%!
  4. 天梵古法健康知识普及:足阳明胃经经穴
  5. Java格式化输出(表格样式)
  6. 全国各地土特产一览表(以后好问朋友要礼物了)
  7. 程序员的五一是怎么过的?除了狗粮还是狗粮?
  8. 2021年安全员-A证复审考试及安全员-A证考试试题
  9. 前几天挂掉一个读者的滴滴二面矩阵题目
  10. 智能出价策略如何影响广告效果?