安装C和C++的MessagePack实现时发生链接器错误

Linker error when installing MessagePack implementation for C and C++

本文关键字:链接 错误 实现 C++ MessagePack 安装      更新时间:2023-10-16

我正在按照指示下载适用于C&C++

在Windows上,从这里下载源程序包并提取它。打开msgpack_vc8.vcproj或msgpack_vc2008文件并使用批处理构建它。它在lib/文件夹中构建库,在include/文件夹中生成头文件。

您可以使用以下命令行进行构建:

vcbuild msgpack_vc2008.vcproj
dir lib       % DLL files are here
dir include   % header files are here

vcbuild msgpack_vc2008.vcproj已被MSBuild msgpack_vc8.vcxproj替换。我使用Visual studio 2012将该项目转换为正确的.vcxproj。Visual studio中的批量构建和运行MSBuild会得到相同的结果,所以从现在开始,我将为他们两个说话

转换项目后,我注意到项目被设置为输出到.lib,而不是.dll,所以我更改了该设置以满足我的需求。编译时出现一个小错误:

...microsoft visual studio 11.0vcincludestdint.h(8): error C2371: 'int8_t' : redefinition; different basic types
...msgpack-0.5.4srcmsgpacksysdep.h(23) : see declaration of 'int8_t'

所以我换了

typedef __int8 int8_t;

typedef signed __int8 int8_t;

这解决了这个小问题。但后来我们到达了我现在的位置。此链接器错误:

objectc.obj : error LNK2019: unresolved external symbol __imp__ntohl@4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohl@4
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohs@4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohs@4
...msgpack-0.5.4DebugMessagePack.dll : fatal error LNK1120: 2 unresolved externals

我已经搜索了这个错误的部分:

在sysdep.h中:

#define _msgpack_be16(x) ntohs(x)
#define _msgpack_be32(x) ntohl(x)

在object.c:中

case MSGPACK_OBJECT_ARRAY:
{
int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; }
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
}

在开箱中。c:

static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
{
o->type = MSGPACK_OBJECT_ARRAY;
o->via.array.size = 0;
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
if(o->via.array.ptr == NULL) { return -1; }
return 0;
}

我只知道这些。如果有另一种方法可以获得.dll,那也会很有帮助。提前谢谢。:)

由于ntohl是一个winsocket API函数,因此需要链接ws2_32.lib库。

这应该能解决问题!