Reputation: 4966
I am trying to use zmq in a C++ project. I am quite confused when I want to build a simple example.
For that, I have built libzmq v3.2.3 separately using the .sln for VisualC++ 2010. Then I have downloaded zmq.hpp and included it. The example is the first one of the zmq guide:
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received Hello" << std::endl;
// Do some 'work'
sleep (1);
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data (), "World", 5);
socket.send (reply);
}
return 0;
}
Compilation is ok, but linkage is not: I get errors like this one:
error LNK2019: external symbol unresolved __imp__zmq_term refrenced in function "public: void __thiscall zmq::context_t::close(void)" (?close@context_t@zmq@@QAEXXZ)
Path to zmq seem to be ok; I just don't understand why VC++ cannot find the code of the little methods inside zmq.hpp
Do you see what I have forget to do?
EDIT: Here is the content of the output window (sorry, it's the french version,but I think it's quite similar):
1>------ Début de la génération : Projet : libzmq, Configuration : Debug Win32 ------
1> 1 fichier(s) copié(s).
1> libzmq.vcxproj -> <...>\..\Ouptut\Debug\libzmq_d.lib
2>------ Début de la génération : Projet : HelloServeur, Configuration : Debug Win32 ------
2> HelloServeur.cpp
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_init référencé dans la fonction "public: __thiscall zmq::context_t::context_t(int)" (??0context_t@zmq@@QAE@H@Z)
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_errno référencé dans la fonction "public: __thiscall zmq::error_t::error_t(void)" (??0error_t@zmq@@QAE@XZ)
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_strerror référencé dans la fonction "public: virtual char const * __thiscall zmq::error_t::what(void)const " (?what@error_t@zmq@@UBEPBDXZ)
2>HelloServeur.obj : error LNK2019: symbole externe non résolu __imp__zmq_term référencé dans la fonction "public: void __thiscall zmq::context_t::close(void)" (?close@context_t@zmq@@QAEXXZ)
2><...>\..\Ouptut\Debug\HelloServeur.exe : fatal error LNK1120: 4 externes non résolus
========== Génération : 1 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========
Upvotes: 1
Views: 2587
Reputation: 186
We weren't using VisualC++, but were getting similar errors while linking. Compiling with -lzmq flag solved the issue for us.
g++ zmq.cpp -lzmq
Upvotes: 1
Reputation: 4966
Ok, we have found why. There was some awful mix in the vcxproj of libzmq between compilation for static and dynamic Library. Everything is alright after recovering the proper project. Thanks to everybody for your help
Upvotes: 0
Reputation: 19232
If you are using a static library, you will need to add the .lib to the linker settings, under the linker | input| additional dependencies in the project settings.
Upvotes: 1