简单的cmakelist运用以及c++中tinyXml的使用
大致学习了一下CMakeLists,刚好想用一下c++中的tinyXml,就写了一个简单的工程
上面这个是工程目录的目录结构:
cmakeTest文件夹 相当于workSpace,也就是主目录
build文件夹 是cmake生成的中间文件的存放地
tinyXml文件夹 是tinyXml源码的地方,这个文件夹内有一个子目录的CMakeLists文件
CmakeLists 就是主目录的cmake文件
main.cpp 就是主文件
menu.xml 是测试解析xml的测试xml文件
<?xml version="1.0"?>
<breakfast_menu>
<foods>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<name>Test</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</foods>
</breakfast_menu>
主要有breakfast_menu、foods、food三个node
// main.cpp
#include <iostream>
#include "tinyXml/tinyxml.h"
using namespace std;
void readXml()
{
TiXmlDocument doc("C:\\workSpace\\xmlHander\\cmakeTest\\menu.xml"); //文件路径要全,一开始我只写了文件名导致打不开文件纠结了好久
if (!doc.LoadFile())
{
std::cout << "failed to load xml file!" << std::endl;
const char * error = doc.ErrorDesc(); //可以打印出错误信息
cout << error << endl;
}
TiXmlHandle handler(&doc); // handle的对象
//结合xml文件看:
//handler.FirstChild("breakfast_menu") 指的是获取breakfast_menu这个node的handler对象,返回值类型是TiXmlHandle
//child("food,1")是指foods这个node下第2个food的node
// TiXmlElement* desc = handler.FirstChild("breakfast_menu").FirstChild("foods").Child("food",1).FirstChildElement("description").ToElement();
TiXmlElement* desc = handler.FirstChild("breakfast_menu").FirstChild("foods").Child("food",3).Child("name",1).ToElement();
if (desc)
{
cout << "success" << endl;
}
else
{
cout << "error" << endl;
}
cout << desc->GetText() << endl;
}
int main()
{
readXml();
}
# 子目录下的cmakelists.txt文件
#
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成(静态)链接库
add_library (tinyXml ${DIR_LIB_SRCS})
# 主目录的CMakeLists.txt文件
#
# 指定cmake最低版本
cmake_minimum_required(VERSION 3.0)
# 项目名称(只是一个名字而已)
project(XmlParser)
# set( CMAKE_CXX_FLAGS "-std=c++11" )
# 如果不加下面两行,那么编译cmake时就要加参数-G “MinGW Makefiles” 用来指定编译器
# 通常需要加上,这样就可以直接在build目录下直接cmake ..
# 描述有误,待验证
# 实际上应该是指定c语言文件和c++文件的默认编译器
# 如果win下环境变量里面没有设置mingw的路径的话,这里应该是可以帮助查找g++的路径
set(CMAKE_C_COMPILER "C:\\mingw64\\bin\\gcc")
set(CMAKE_CXX_COMPILER "C:\\mingw64\\bin\\g++")
# 添加了之后,就相当于在编译的时候加上了 -Wall -g选项
# -Wall : 编译时将错误信息打印出来
# -g : 启用gdb
add_definitions("-Wall -g")
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 添加 tinyXml 子目录
add_subdirectory(tinyXml)
# 指定生成目标
# xmlParser : 目标文件的名字
add_executable(xmlParser ${DIR_SRCS})
# 添加链接库
target_link_libraries(xmlParser tinyXml)
以上代码均运行正常,注释中的描述可能有不对的地方,发现的话烦请各位指出,感激!