linking to a static 0MQ library in VS

2019-03-11 05:45发布

This may be a Visual Studio question more than anything else...
I'm trying to build a 0MQ C++ example using VS10 and ZeroMQ 2.2.0.
I downloaded the windows sources and tried to follow these instructions in order to build 0MQ statically. Specifically:

  • Switched to Release
  • For all 7 projects in the solution:
    • set General\Configuration Type to Static library (.lib)
    • set C/C++\Code Generation\Runtime Library to Multi-threaded (/MT)
    • added ZMQ_STATIC to C/C++\Preprocessor\Preprocessor Definitions
  • Updated zmq.h and zmq_utils.h so that if _MSC_VER and ZMQ_STATIC are defined then DLL_EXPORT will also be defined

At this point 0MQ seems to build well.

  • Created an empty console project:
    • switched to Release
    • added a single cpp file with the example linked above
      • changed random to rand, srandom to srand and snprintf to _snprintf
    • set C/C++\Code Generation\Runtime Library to Multi-threaded (/MT)
    • added ...\zeromq-2.2.0\include folder to C/C++\General\Additional Include Directories
    • added ...\zeromq-2.2.0\builds\msvc\Release\*.lib to Linker\Input\Additional Dependencies

However I still receive the following linking errors:

1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_bind
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_close
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_errno
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_init
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_msg_data
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_strerror
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_socket
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_msg_init_size
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_term
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_msg_close
1>zmqexp.obj : error LNK2001: unresolved external symbol __imp__zmq_send

What have I missed?

3条回答
家丑人穷心不美
2楼-- · 2019-03-11 05:55

You should add ZMQ_STATIC to C/C++\Preprocessor\Preprocessor Definitions in your "empty console project" too. Otherwise, when you compile your application, ZMQ_EXPORT in zmq.h is defined as __declspec(dllimport), and as a result, MSVC looks for __imp__zmq_* symbols instead of zmq_*

查看更多
3楼-- · 2019-03-11 06:13

I had similar errors - not when trying to statically link, but just trying to create a ZMQ project and link the .lib 'stubs' for the dll.

In my case 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.

查看更多
Evening l夕情丶
4楼-- · 2019-03-11 06:16

Is the static linking very important to you? If not, you can try out the second answer by elnino_9 here. Elaborating elnino_9's answer:

  • Download the sources and unzip to some local folder (say C:\dev\zeromq).
  • Go to C:\dev\zeromq-2.2.0\builds\msvc and open the msvc.sln solution. MS2010 will upgrade from a VS2008 to a VS2010 project
  • Build all of the projects.
  • The build should generate the two following files:
    • C:\dev\zeromq\lib\libzmq.lib - this is what you will need to reference in your project's linker options
    • C:\dev\zeromq_boaz\builds\msvc\Release\libzmq.dll - this you will need to copy to the same folder as your project's executable in order to run it (if your build was configured to 'Debug', the path would be C:\dev\zeromq\builds\msvc\ Debug \libzmq.dll)
  • Create your solution and project. Do the following:
    • In the Solution Explorer, right-click on you project, and select 'Properties' (at the very bottom).
    • Navigate to 'C/C++ --> General --> Additional Include Directories' and add C:\dev\zeromq\include. This will reference 0MQ's header files.
    • Navigate to 'Linker --> Input --> Additional Dependencies' and add the full path to the 'libzmq.dll' file from the previous step.
    • Build your project - this should go without errors now.
  • Copy 'libzmq.dll' to the same folder as your executable - your project should now run.

Some comments:

  • I am not sure why you need to reference the 'libzmq.lib' file. It is not needed by the executable (you can delete it, and it will still run, as the necessary logic resides in the dll). Can someone explain this?
  • Notice one caveat in the example for Windows users - the second binding statement (publisher.bind("ipc://weather.ipc");) will cause an exception. As explained here (though in fine-print), the Inter-Process Transport is not supported on Windows.

EDIT

I think the answer to my first comment can be found in MSDN:

"When the source code for the calling executable is compiled or assembled, the DLL function call generates an external function reference in the object code. To resolve this external reference, the application must link with the import library (.lib file) provided by the maker of the DLL."

查看更多
登录 后发表回答