C++下有这一个Yaml的解析包,PHP也有一个Yaml的解析包,这样两者就能进行对文件的解析是一个规则,达到统一的目的,双方就可以通讯,这样语言之间对配置的解析一样,上层通过Socket来通信统一,进而实现大系统的搭建。
首先要有CMake环境,需要编译一个CMake出来,然后才是在下载下来的文件目录下运行这个Cmake:
tar -zxvf cmake-2.8.4.tar.gz
首先,需要下载安装Cmake;
wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz
安装cmake;
tar xvzf cmake-2.8.4.tar.gz
./configure
make
make install
其次,下载YAML的C++解析器 yaml-cpp ,URL为:http://www.oschina.NET/p/yaml-cpp https://github.com/jbeder/yaml-cpp/releases/
编译:
cmake -DBUILD_SHARED_LIBS=ON
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp# cmake -DBUILD_SHARED_LIBS=ON
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Performing Test FLAG_WEXTRA
-- Performing Test FLAG_WEXTRA - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /home/admin/c++/yaml_4_C/yaml-cpp
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp# make
Scanning dependencies of target yaml-cpp
[ 3%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitter.cpp.o
[ 6%] Building CXX object CMakeFiles/yaml-cpp.dir/src/emitterstate.cpp.o
[ 9%] Building CXX object CMakeFiles/yaml-cpp.dir/src/simplekey.cpp.o
[ 12%] Building CXX object CMakeFiles/yaml-cpp.dir/src/parser.cpp.o
......
[ 96%] Building CXX object test/CMakeFiles/run-tests.dir/spectests.cpp.o
Linking CXX executable run-tests
[ 96%] Built target run-tests
Scanning dependencies of target parse
[100%] Building CXX object util/CMakeFiles/parse.dir/parse.cpp.o
Linking CXX executable parse
[100%] Built target parse
然后呢: make ,后生成:
/home/admin/c++/yaml_4_C/yaml-cpp/test3
libyaml-cpp.so.0.2.6
libyaml-cpp.so.0.2 -> libyaml-cpp.so.0.2.6
libyaml-cpp.so -> libyaml-cpp.so.0.2
查看H文件位置:
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp# find . -name "yaml.h"
./include/yaml-cpp/yaml.h
最后,进行Yaml的代码解析测试:
1)建立目录:
mkdir test3
目录位置:root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp/test3
2)写入C++测试代码
1.vi test.cpp
加入:
#include
#include "yaml.h"
int main()
{
std::ifstream fin("component.yaml");
YAML::Parser parser(fin);
YAML::Node doc;
while(parser.GetNextDocument(doc))
{
}
}
3).把yaml.h拷贝到test3的目录下来:
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp/test3# cp ../include/yaml-cpp/yaml.h
4)后进行编译编译:
g++ -o test test.cpp -I../include/ ../libyaml-cpp.so
5)运行测试
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp/test3# ./test
./test: error while loading shared libraries: libyaml-cpp.so.0.2: cannot open shared object file: No such file or directory
6)解决问题:
/home/admin/c++/yaml_4_C/yaml-cpp/test3
libyaml-cpp.so.0.2.6
libyaml-cpp.so.0.2 -> libyaml-cpp.so.0.2.6
libyaml-cpp.so -> libyaml-cpp.so.0.2
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp# cp libyaml-cpp.so /usr/lib/
root@116.255.139.240:/usr/lib# ln -s libyaml-cpp.so libyaml-cpp.so.0.2
7)再次执行,得到解决:
再执行就不报错了,原来是没有把这个so给放到/usr/lib下,再就是没有给重新软链接一个,Ok了。
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp/test3# ./test
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp/test3#
8)自由发挥,放到实际的开发中去。
Yaml实际解析测试:
#include
#include "yaml.h"
#include
#include
using namespace std;
int main()
{
std::ifstream fin("component.yaml");
YAML::Parser parser(fin);
YAML::Node doc;
while(parser.GetNextDocument(doc))
{
std::string output;
doc["site"] >> output;
cout << output <
}
}
component.yaml这个Yaml文件的内容:
site: jackxiang.com
编译:
g++ -g -o test test.cpp -I../include/ ../libyaml-cpp.so
测试Yaml解析是否成功:
root@116.255.139.240:/home/admin/c++/yaml_4_C/yaml-cpp/test3# ./test
jackxiang.com
解析成功!
最后,当然还有纯C语言的,来解析 YAML 1.1 数据,其编译估计差不多,下载URl:
http://www.oschina.net/p/libyaml
建议用eclipse来开发C++,还有PHP,因为eclipse 有PHP的插件,也有C++的插件,我是用的Zend for eclipse,现在eclipse还有一个Yaml的编辑插件,相关介绍情况如下:
安装YEdit
YEdit是一个在Eclipse上编辑YAML文件的插件。当我们我们使用Python进行App Engine的开发,会涉及编辑一些YAML文件,这个插件提供了较好的支持。
这个还是通过Eclipse的更新管理器来完成安装,在对话框中的在Work with中输入http://dadacoalition.org/yedit,然后勾选该插件,然后不断下一步,稍等片刻,安装成功。