Discussion:
[zeromq-dev] Static library in Visual Studio (SOLVED)
malist
2010-11-09 01:36:17 UTC
Permalink
Here is what I have done to get zmq to compile as static library using Visual Studio Express 2008 in WinXP:

First I had to add 2 more includes to my project to be able to compile in Windows:

#if VERSIONWIN
#include "..\zmq\zeromq2\src\stdint.hpp" // for windows
#include "..\zmq\zeromq2\include\zmq.h" // for windows
#else
#include "zmq.h" // zmq library, OSX XCode
#endif
---

Static compile with newbie-try-and-see-what-happens-method:

1) I changed in libzmq project Property Pages / General / Configuration type --> Static Library (.lib)

2) I changed C/C++ Code generation Runtime Library --> Multi-threaded (/MT)
* for debug build --> Multi-threaded debug (/MTd)

Then I tried to compile my program and I got link errors like this:
MA_Zmq_Plugin.obj : error LNK2019: unresolved external symbol __imp__zmq_version referenced in function...

3) Then I googled discussions (http://www.mail-archive.com/zeromq-***@lists.zeromq.org/msg03925.html) and changed both zmq.h and zmq_utils.h:

/* Win32 needs special handling for DLL exports */
#if defined _WIN32
# if defined DLL_EXPORT
# define ZMQ_EXPORT __declspec(dllexport)
# else
# define ZMQ_EXPORT __declspec(dllimport)
# endif
#else
# define ZMQ_EXPORT
#endif

-->

#if defined _MSC_VER
# if defined ZMQ_STATIC
# define DLL_EXPORT
# endif
# if defined DLL_EXPORT
# define ZMQ_EXPORT __declspec(dllexport)
# else
# define ZMQ_EXPORT __declspec(dllimport)
# endif
#else
# define ZMQ_EXPORT
#endif

4) I added to C/C++ / Preprocessor / Preprocessor definitions --> ZMQ_STATIC

5) I compiled zeromq and copied zeromq2\builds\msvc\Release\libzmq.lib to my own project folder as libzmq.lib. Library size was 18467 Kb.
* for debug build I copied zeromq2\builds\msvc\Debug\libzmq.lib to my own project folder as libzmq_debug.lib. Library size was 7356 Kb.

I got 47 warnings like one below, I just ignored them:
z:\cpp\examples\zmq\zeromq2\include\zmq.h(48) : warning C4005: 'DLL_EXPORT' : macro redefinition
command-line arguments : see previous definition of 'DLL_EXPORT'

6) I added Linker / Input / Additional dependencies --> libzmq.lib
* for debug build: libzmq_debug.lib

I compiled my own project and got more link errors, then I Googled again and added:
7) Linker / Input / Additional dependencies --> ws2_32.lib
8) I added C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib\RpcRT4.lib to my project with drag & drop.


Now I was able to get zmq to build statically and it worked, both release and debug!
My final non-debug product size is 351 Kb.

---

I still get 1 link warning when I build non-debug version, but I don't know if it has any side effects:

libzmq.lib(zmq.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance.

---

Now I had only one problem. Debug build did not stop into breakpoints when debugging. Debugger said that symbols have not been loaded.

After debug build I got 47 linker warnings like this:
libzmq_debug.lib(clock.obj) : warning LNK4204: 'Z:\cpp\MA_Plugin_v12\Objs\Debug\vc90.pdb' is missing debugging information for referencing module; linking object as if no debug info.

Then I Googled again and found:
http://www.gamedev.net/reference/articles/article1583.asp
"I recommend compiling with C7 compatible debug (/Z7) for a static library. This allows debugging info to be placed inside the .lib files, so it saves the hassle of locating the program database if you generate it separately."

I recompiled libzmq debug build with with C7 compatible debug (/Z7). Library size increased to 16899 Kb and I got rid of all linker warnings in my own program. Still debug breakpoints do not work.


So one question remains after almost 4 hours of testing:
How can I can stop in my own project breakpoints?

Thanks for help.


Regards,

Pasi Mankinen
Finland
malist
2010-11-09 10:02:48 UTC
Permalink
Post by malist
How can I can stop in my own project breakpoints?
I answer my own question. After more Googling I found:
http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/132b7383-7941-4665-b0f1-ccf024b9c601

* I changed libzmq debug project so that it generated libzmq_debug.lib and libzmq_debug.pdb.
* I copied libzmq_debug.pdb to my project Objs\Debug -folder.
* I added Tools / Options / Symbols new line: Objs\Debug

Now I can debug my project.


Pasi Mankinen
Finland
Pieter Hintjens
2010-11-09 11:10:44 UTC
Permalink
Hi Pasi,

Would you like to create a wiki page for this on the zeromq.org site,
maybe in the docs section? Then people can use your experience, and
turn that into a guide.

Thanks
Pieter
Post by malist
Post by malist
How can I can stop in my own project breakpoints?
http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/132b7383-7941-4665-b0f1-ccf024b9c601
* I changed libzmq debug project so that it generated libzmq_debug.lib and libzmq_debug.pdb.
* I copied libzmq_debug.pdb to my project Objs\Debug -folder.
* I added Tools / Options / Symbols new line: Objs\Debug
Now I can debug my project.
Pasi Mankinen
Finland
_______________________________________________
zeromq-dev mailing list
http://lists.zeromq.org/mailman/listinfo/zeromq-dev
malist
2010-11-09 21:40:16 UTC
Permalink
Post by Pieter Hintjens
Hi Pasi,
Would you like to create a wiki page for this on the zeromq.org site,
maybe in the docs section? Then people can use your experience, and
turn that into a guide.
Thanks
Pieter
Hi Pieter.

It is better to:
a) add static and static debug targets to master
b) let someone who knows what he/she is doing also explain it :)

Feel completely free to use my posts.


Regards,

Pasi Mankinen
Finland
Pieter Hintjens
2010-11-09 21:46:54 UTC
Permalink
Post by malist
a) add static and static debug targets to master
b) let someone who knows what he/she is doing also explain it :)
I've no idea... :-)

-Pieter
Martin Sustrik
2010-11-10 09:55:54 UTC
Permalink
Post by Pieter Hintjens
Post by malist
a) add static and static debug targets to master
b) let someone who knows what he/she is doing also explain it :)
I've no idea... :-)
Have a look at thread called "Static linking issue on Win32." on the
mailing list. IIRC a solution was proposed for Win32 static builds
there, but the thread kind of got lost without resulting in actual patch.

Martin

Loading...