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
#include
#include
int main(int argc, char **argv)
{
//Define document pointer
xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0");
//Define node pointer
xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root");
//Set the root element of the document
xmlDocSetRootElement(doc,root_node);
//Create child nodes directly in the root node
xmlNewTextChild(root_node,NULL,BAD_CAST"newnode1",BAD_CAST"newnode1 content");
xmlNewTextChild(root_node,NULL,BAD_CAST"newnode2",BAD_CAST"newnode2 content");
//Create a new node
xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");
//Create a new text node
xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");
//Add a new node to parent
xmlAddChild(root_node,node);
xmlAddChild(node,content);
//Create a new property carried by a node
xmlNewProp(node,BAD_CAST"attribute",BAD_CAST"yes");
//Create a son and grandson node element
node = 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 file
int nRel = xmlSaveFile("CreatedXmlDemo.xml",doc);
if(nRel != -1) {
//Free up all the structures used by a document,tree included
xmlFreeDoc(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
^
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
如果觉得这篇文章有用,可以扫免费红包支持。