我正在尝试在Visual Studio 2013上用C ++构建这个简单的ZeroMQ服务器。
#include "stdafx.h"
#include "zmq.hpp"
#include <string>
#include <iostream>
#include <windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// Prepare context and socket
zmq::context_t ctx(1);
zmq::socket_t sckt(ctx, ZMQ_REP);
sckt.bind("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
sckt.recv(&request);
std::cout << "Received Hello" << endl;
Sleep(1);
// Send reply back to client
zmq::message_t reply(5);
memcpy((void*)reply.data(), "World", 5);
sckt.send(reply);
}
return EXIT_SUCCESS;
}
然而,当我尝试构建VS项目时,我收到错误LNK2019。下面是编译器输出:
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_errno referenced in function "public: __thiscall zmq::error_t::error_t(void)" (??0error_t@zmq@@QAE@XZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_strerror referenced in function "public: virtual char const * __thiscall zmq::error_t::what(void)const " (?what@error_t@zmq@@UBEPBDXZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_ctx_new referenced in function "public: __thiscall zmq::context_t::context_t(int,int)" (??0context_t@zmq@@QAE@HH@Z)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_ctx_set referenced in function "public: __thiscall zmq::context_t::context_t(int,int)" (??0context_t@zmq@@QAE@HH@Z)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_ctx_destroy referenced in function "public: void __thiscall zmq::context_t::close(void)" (?close@context_t@zmq@@QAEXXZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_msg_init referenced in function "public: __thiscall zmq::message_t::message_t(void)" (??0message_t@zmq@@QAE@XZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_msg_init_size referenced in function "public: __thiscall zmq::message_t::message_t(unsigned int)" (??0message_t@zmq@@QAE@I@Z)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_msg_send referenced in function "public: bool __thiscall zmq::socket_t::send(class zmq::message_t &,int)" (?send@socket_t@zmq@@QAE_NAAVmessage_t@2@H@Z)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_msg_recv referenced in function "public: bool __thiscall zmq::socket_t::recv(class zmq::message_t *,int)" (?recv@socket_t@zmq@@QAE_NPAVmessage_t@2@H@Z)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_msg_close referenced in function "public: __thiscall zmq::message_t::~message_t(void)" (??1message_t@zmq@@QAE@XZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_msg_data referenced in function "public: void * __thiscall zmq::message_t::data(void)" (?data@message_t@zmq@@QAEPAXXZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_socket referenced in function "public: __thiscall zmq::socket_t::socket_t(class zmq::context_t &,int)" (??0socket_t@zmq@@QAE@AAVcontext_t@1@H@Z)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_close referenced in function "public: void __thiscall zmq::socket_t::close(void)" (?close@socket_t@zmq@@QAEXXZ)
1>ZeroMQServer.obj : error LNK2019: unresolved external symbol __imp__zmq_bind referenced in function "public: void __thiscall zmq::socket_t::bind(char const *)" (?bind@socket_t@zmq@@QAEXPBD@Z)
1>D:\[ Source Code ]\C++\ZeroMQServer\Debug\ZeroMQServer.exe : fatal error LNK1120: 14 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我已经安装了ZeroMQ binary v4.0.4并更新了VS项目属性以指向ZeroMQ的include和lib路径。具体来说,这是我在项目属性中更新的内容(我怀疑其中一些设置可能是多余的):
我在Win 7,64位版本上。 请帮我解决这个问题。
答案 0 :(得分:6)
根据zeromq.org上的库名称,您尝试将同一个库链接两次:首先是发布版本,然后是调试版本。
从发布配置中删除libzmq-v120-mt-gd-4_0_4.lib
,然后从调试配置中删除libzmq-v120-mt-4_0_4.lib
。
答案 1 :(得分:1)
由于我想静态链接zmq,因此必须定义ZMQ_STATIC。 默认情况下,所有这些功能定义为
elif定义的DLL_EXPORT
答案 2 :(得分:-1)
我有同样的错误。但对我来说,他们是由一套错误的召集惯例制作的。 (只是想在这里提一下,因为跟踪它需要一个多小时。)