C的序列化库tpl的使用
coolshell推荐了一个好用的C的序列化库(http://coolshell.cn/articles/878.html),于是下载来试试,下载地址:http://troydhanson.github.com/tpl/index.html,解压tpl-master.zip,到doc目录看看使用文档,在Windows有3种方式使用这个库。
On Windows
DLL
On the tpl homepage, a Visual Studio 2008 solution package is available for download. This zipfile contains pre-built 32- and 64-bit versions of tpl as a DLL. If you like,you can build the DLL yourself using VS2008 or VS2010 (the free Express Editionis sufficient) by opening the solution file and choosing Build Solution.
Non-DLL usage
Alternatively, tplcan be used directly (instead of as a DLL) by compiling the tpl sources rightinto your program. To do this, add tpl.c, tpl.h, win/mman.h and win/mmap.c to yourprogram’s source and header files and add the preprocessor definition TPL_NOLIB.
MinGW/Cygwin
Priorto tpl release 1.5, using tpl on Windows required building it with MinGW orCygwin. This is no longer necessary. If you want to build it that way anyway,use the non-Windows (i.e. tar.bz2) tpl download and follow the "configure;make; make install" approach.最简单的使用方式就是Non-DLLusage了,就是和调用这个库的源代码一起编译。下面开始吧~
1、VS2010新建一个Windows Console工程HelloTPL,将tpl.c, tpl.h, win/mman.h and win/mmap.c拷贝到工程目录下,即HelloTPL文件夹下,不要在HelloTPL文件夹外新建一个libs文件,然后把文件加进来,这样VS2010是找不到头文件的:-(
2、给工程添加预处理宏(preprocessor definition),如下
如果搞忘记给编译器添加这个宏了,就会出现“definition of dllimport function not allowed”错误,如下
3、别以为按照官方的文档做就行了,开源的代码总是会出现点问题(好头疼!),链接tpl.obj文件时出错如下
去tpl.c文件里去找找这个tpl_dump函数,结果发现S_ISREG没有定义(原来是个宏),网上一查S_ISREG是Unix/Linux下的东东,Windows没有!还好有网上有高人指点(自己定义一下S_ISREG宏),要不然我也写不成这篇文章了。
4、在tpl.h文件中添加下面几行
#ifndef S_ISREG #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) #endif
这下OK了,可以跑跑官方提供了两个小例子了O(∩_∩)O~
参考资料:
1、http://coolshell.cn/articles/878.html
2、http://troydhanson.github.com/tpl/userguide.html
3、http://stackoverflow.com/questions/11238918/s-isreg-macro-undefined
4、http://www.linuxquestions.org/questions/programming-9/porting-to-win32-429334/