4
votes

I'm having a problem trying to use ZeroMQ in my new application. Please note: I have no problem downloading and building ZeroMQ itself. I downloaded the ZeroMQ ZIP file, opened the project/solution file in Visual Studio/C++ 2008 and built the ZeroMQ library just fine. The /lib directory of the ZeroMQ install folder contains the .lib, .dll, and other files, so all is well there as far as I know.

My problem is that I am trying to build a simple project consisting of the HelloWorld server example in the ZeroMQ user guide. I created the following file called helloserver.c.

//
// Hello World server
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.h>
#include <stdio.h>
#include <string.h>

int main (void)
{
zmq_msg_t request;
zmq_msg_t reply;

void *context = zmq_init (1);

// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
zmq_bind (responder, "tcp://*:5555");

while (1) {
// Wait for next request from client

    zmq_msg_init (&request);
    zmq_recvmsg (responder, &request, 0);
    printf ("Received Hello\n");
    zmq_msg_close (&request);

// Do some 'work'
    Sleep (1);

// Send reply back to client
    zmq_msg_init_size (&reply, 5);
    memcpy (zmq_msg_data (&reply), "World", 5);
    zmq_sendmsg (responder, &reply, 0);
    zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_term (context);
return 0;
}

I added the ZeroMQ installatiion include directory to the VC++2008 include path (Properties->Configuration Properties->C/C++->General->Additional Include Directories) and I also added the ZeroMQ lib directory to the project: (Properties->Configuration Properties->C/C++->Code Generation->Runtime Library, selected Multithreaded Debug DLL /MDd).

I tried to build the project. Everything compiles fine butI get a bunch of unresolved externals like this when linking:

1>Linking... 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_term referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_close referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_sendmsg referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_msg_data referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_msg_init_size referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_msg_close referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_recvmsg referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_msg_init referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_bind referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_socket referenced in function _main 1>helloserver.obj : error LNK2019: unresolved external symbol _imp_zmq_init referenced in function _main 1>C:\work\visualc++2008\projects\HelloZMQServer\Debug\HelloZMQServer.exe : fatal error LNK1120: 11 unresolved externals

I tried going back to the Code generation->Runtime library settings and tried changing the library switch to ?MD or /MT but nothing worked, I still get these linker errors.

Please note that when I created helloworld.c I used an empty windows console application project, and the only changes I made were to add the ZeroMQ include and lib directories as indicated above. I have not changed any other default project settings. I'm guessing I'm missing a project setting somewhere.

How can I fix this?

3
Also please note that I built ZeroMQ by double clicking on its own solution file, so I built it as its own separate solution. My HelloWorld project is in its own HelloWorld solution. Do I need to bring the ZeroMQ library into the same solution as HelloWorld in order to get it to build properly? If so how do I do this?Marc
Oh, and yes I already added the ZeroMQ lib path to my HellowOrld project (Project->References->Add Path). I can add the path but the Add New reference window pops up and has nothing in it.Marc
OK I got this to work by adding the ZeroMQ project libzmq into my new HelloWorld solution. I was finally able to see libzmq show up as a reference I could add in the Add New Reference window. It now links ok. Is it really the case that if one creates a solution in VC++, one must include the project of external libraries in this solution, along with your own user project? This seems a bit silly to me.Marc

3 Answers

2
votes

You didn't mention that you also added the concrete lib to your project. Note that it doesn't suffice to tell VS only the lib path, but you must also add the lib itself to your project. I don't have Visual Studio at hand right now, but you will find this option within the project's linker settings. There you can mention all libs you want to link against.

6
votes

When I got similar errors it was because I was trying to link the 64-bit libraries into a 32-bit project. I had downloaded the wrong version. When I got the right ones, ie x86 instead of x64, it worked.

4
votes

Add the ZMQ library path (Path where ZMQ libs are installed\zeromq-2.1.10\lib) to your project:

Project->Properties->Linker->General->Additional Library Directories

Also add libzmq.lib to

Project->Properties->Linker->Input->Additional Dependencies

Funny thing is, you need to have a copy of libzmq.dll in your project, else when you run your project, the debugger will complain "Unable to find libzmq.dll". Still searching for a workaround.

Note:This is what I used to run my zmq code in Visual Studio 2010. Should work for VS 2008 too.