CMake加入第三方库
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加入第三方库相关推荐
- 【cmake学习】cmake 引入第三方库(头文件目录、库目录、库文件)
程序的编写需要用到头文件,程序的编译需要lib文件,程序的运行需要dll文件,因此cmake引入第三方库其实就是将include目录.lib目录.bin目录引入工程. 目录 1.fin ...
- cmake之 第三方库始终报错 /usr/bin/ld: cannot find -lxxx?
为什么自己要添加始终?感觉很奇怪? 加载第三方库对于一个程序是很经常碰到的事情. 首先,自己应该很清楚第三方库的位置哈.将库添加到工程中. #注意:动态库简写,全称均可:静态库要全称. TARGET_ ...
- VulkanLearning - 环境搭建:使用CLion+cmake链接第三方库
不知从何时开始,突然不想再打开VS写东西了(这人pass有病(很大一部分原因是找不到如何添加VC工程模板(说白了就是一条菜狗)))加上正要开始学习Vulkan以及一些相关的图形学知识,在工程编译方面要 ...
- CMake引入三方库
做移动端的NDK开发经常需要引入三方库,本文以常见的JSON库为例进行说明 jsoncpp源码下载地址https://github.com/open-source-parsers/jsoncpp 下载 ...
- ISP 【一】————boost标准库使用——批量读取保存文件 /boost第三方库的使用及其cmake添加,图像gramma
CMakeLists.txt文件中需要添加第三方库,并企鹅在CMakeLists.txt中添加 include_directories(${PROJECT_SOURCE_DIR}/../3party/ ...
- CMake基础 第8节 包含第三方库
介绍# 几乎所有重要的项目都需要包含第三方库.头文件或程序.CMake支持使用find_package()函数查找这些工具的路径.这将从CMAKE_MODULE_PATH中的文件夹列表中搜索格式为Fi ...
- AS使用NDK Cmake方式依赖第三方库注意事项
AS2.2以后支持Cmake了,以前的Android.mk的方式可以告别了,官方教程 CMakeLists.txt文件的书写 引入第三方库使用add_library()需要指定是SHARED-> ...
- C++-Cmake指令:link_directories【指定第三方库所在路径】【官方不建议使用该命令,取而代之的为find_package()、find_library()】
LINK_DIRECTORIES 命令来指定第三方库所在路径,比如,你的动态库在/home/myproject/libs这个路径下,则通过命令:LINK_DIRECTORIES(/home/mypro ...
- Cmake管理统一版本的SLAM第三方库
学习SLAM的过程中,都需要安装eigen.opencv等多种第三方库,而这些库往往对应不同版本,有时还会涉及多个版本之间的切换,但SLAM算法在终端树莓派或工控机部署时得重新安装第三方库. 本文使用 ...
最新文章
- 很慌!一次惊心动魄的服务器误删文件的恢复过程
- ustc小道消息20220104
- Mac系统如何配置adb路径
- (最优解)L1-028 判断素数 (10分)——17行代码AC
- mysql5.7+proxy_mysql 5.7+mysql-proxy 0.8.5 读写分离
- Intel OpenCL + Ubuntu安装
- (32)System Verilog类class中构造函数new()示例
- 基于JAVA+SpringMVC+Mybatis+MYSQL的校园失物招领系统
- python与机器学习(五)——决策树
- 个人总结OLinux上安装oracle11G Data Guard
- C语言函数一章教学,c语言案例教程:函数教学讲义.ppt
- 学习笔记:定积分的求解(矩形法)
- 思想实验及其在科学发展中的作用
- 华为交换机:STP测试实验
- Maven手动添加dependency(以Oracle JDBC为例)
- 广告学本科--现代管理学--[00107]
- 细说中国富豪的9种发家类型
- 非匀质化资金池——为什么资产 NFT 化是 DeFi 的必经之路
- Python(分治算法)问题 A: 找出伪币_给你一个装有n枚硬币的袋子。n枚硬币中有一个是伪造的,并且那个伪造的硬币比真的硬币要轻一些。你的任务是找出这枚伪造的硬币。
- css实现序列帧闪电动效