messagpack的使用

我打算使用messagepack对通信的对象进行序列化,使用的方式参考这篇文章:

http://www.cppfans.org/1410.html

此处记录一下自己遇到的一些问题

先是用VS2010对messagepack编译,生成静态库,这个没有问题,新建一个test项目来学习,测试messagepack,编译没有问题,连接的时候出现下面的问题

错误 1 error LNK2019: 无法解析的外部符号 __imp__ntohl@4,该符号在函数 "private: static void __cdecl msgpack::packer<class msgpack::sbuffer>::_pack_raw(class msgpack::sbuffer &,unsigned int)" (?_pack_raw@?$packer@Vsbuffer@msgpack@@@msgpack@@CAXAAVsbuffer@2@I@Z) 中被引用 D:project_kukuprogramlibrarymsgpack-0.5.4 est_msgpackenter.obj test_msgpack

此处说ntohl这个函数没有函数实现,而这个函数是SOCKET函数,查一下MSDN,它需要ws2_32.lib这个静态库来支持,在cpp文件中加上对这个库的引用,或者在项目属性的库依赖项加上ws2_32.lib,这回连接就没问题了

项目中使用的VS2013 Desktop 版本,在此版本中使用messagepack时出现下面的问题

错误 1 error C2371: “int8_t”: 重定义;不同的基类型 c:program filesmicrosoft visual studio 12.0vcincludestdint.h 8 1 test_msgpack

看一下输出的显示

1>c:program filesmicrosoft visual studio 12.0vcincludestdint.h(8): error C2371: “int8_t”: 重定义;不同的基类型
1> d:project_kukuprogramlibrarymsgpack-0.5.4srcmsgpacksysdep.h(23) : 参见“int8_t”的声明

一个是在stdint.h中定义的,另一个是在msgpacksysdep.h中定义的,前面一个是系统文件,后面一个是库里面的声明文件,打开后面一个文件看一下

#ifdef _MSC_VER
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#endif

其实是_MSC_VER的版本造成的问题,这个时候只要把#ifdef改成#ifndef即可

原文地址:https://www.cnblogs.com/emyueguang/p/3613117.html