# Build and static link ZeroMQ on Windows

ZeroMQ ( http://zeromq.org and https://github.com/zeromq/libzmq ) is a library that allows code to communicate between threads, processes, or computers in just a few lines and uses simple, composable patterns (like publish-subscribe and broadcast).

In this post, we’ll build ZeroMQ on Windows as a static library (to take advantage of the “static linking exception” in its license: http://zeromq.org/area:licensing ) and then bake that static library into a simple Windows executable.  There are many posts around the web that show you how to do this … here’s one more:

### Step 1: Build Vcpkg ( https://github.com/Microsoft/vcpkg )

Notes:

• Vcpkg does not manage pre-built binaries (like NuGet or Homebrew)
• Vcpkg manages source code and builds that source code on Windows, Linux, and MacOS.
• At the time of this post, the documentation for Vcpkg was located here:
https://docs.microsoft.com/en-us/cpp/vcpkg

In a Visual Studio 2017 developer command prompt (with Git installed), execute the following commands:

cd /d C:\
mkdir Repos
cd /d C:\Repos\
git clone https://github.com/Microsoft/vcpkg
cd /d C:\Repos\vcpkg\
bootstrap-vcpkg.bat


… now, C:\Repos\vcpkg\vcpkg.exe should exist.

### Step 2: Use vcpkg to build ZeroMQ ( vcpkg install zeromq:x64-windows-static )

Note:

• On purpose … to make things more explicit and more difficult in Step 3, we do not execute the following command:
vcpkg integrate install

Now that C:\Repos\vcpkg\vcpkg.exe exists, execute the following commands:

cd /d C:\Repos\vcpkg\
vcpkg install zeromq:x64-windows-static

… now, we should have the following:

• ZeroMQ source code folder: C:\Repos\vcpkg\buildtrees\zeromq\src
• ZeroMQ debug build folder: C:\Repos\vcpkg\buildtrees\zeromq\x64-windows-static-dbg
• ZeroMQ release build folder: C:\Repos\vcpkg\buildtrees\zeromq\x64-windows-static-rel
• ZeroMQ target folder: C:\Repos\vcpkg\packages\zeromq_x64-windows-static
• ZeroMQ target include folder: C:\Repos\vcpkg\packages\zeromq_x64-windows-static\include
• ZeroMQ target debug lib folder: C:\Repos\vcpkg\packages\zeromq_x64-windows-static\debug\lib
• At the time of this post, the static library built was: libzmq-mt-sgd-4_3_1.lib
• Note: The mt and d in libzmq-mt-sgd-4_3_1.lib means multi-threaded debug (requiring the debug executable in the next step to be compiled using /MTd)
• ZeroMQ target release lib folder: C:\Repos\vcpkg\packages\zeromq_x64-windows-static\lib
• At the time of this post, the static library built was: libzmq-mt-s-4_3_1.lib
• Note: The mt in libzmq-mt-s-4_3_1.lib means multi-threaded (requiring the release executable in the next step to be compiled using /MT)

### Step 3: Build an executable that statically links ZeroMQ

In Visual Studio 2017, do the following:

• File / New / Project…

• Paste the following code into Source.cpp:
#include <zmq.h>
#include <iostream>

int main()
{
int major = 0;
int minor = 0;
int patch = 0;
zmq_version( &major, &minor, &patch );
std::wcout << "Current 0MQ version is " << major << '.' << minor << '.' << patch << '\n';
}
• Change the Solution Platform to x64:

• Select the “ZeroMQ-Version” project, select Project / Properties, change the Configuration to “All Configurations“, select Configuration Properties / C/C++ / General, and then add the following include folder to “Additional Include Directories“:
C:\Repos\vcpkg\packages\zeromq_x64-windows-static\include

• Next, for “All Configurations“, select Configuration Properties / C/C++ / Preprocessor, and then add the following preprocessor definition to “Preprocessor Definitions“:
ZMQ_STATIC

• Next, change the Configuration to “Debug“, select Configuration Properties / C/C++ / Code Generation, and then change the “Runtime Library” to “Multi-threaded Debug (/MTd)“:

• Next, change the Configuration to “Release“, select Configuration Properties / C/C++ / Code Generation, and then change the “Runtime Library” to “Multi-threaded (/MT)“:

• Next, we’ll add the static libraries … and while we could break these up into separate lib folder and lib file entries, I’ll just use each lib’s full file path here.
• First, switch the Configuration back to “Debug“, select Configuration Properties / Linker / Input, and then add the following entries to “Additional Dependencies“:
C:\Repos\vcpkg\packages\zeromq_x64-windows-static\debug\lib\libzmq-mt-sgd-4_3_1.lib
Ws2_32.lib
Iphlpapi.lib

• Second, switch the Configuration back to “Release“, select Configuration Properties / Linker / Input, and then add the following entries to “Additional Dependencies“:
C:\Repos\vcpkg\packages\zeromq_x64-windows-static\lib\libzmq-mt-s-4_3_1.lib
Ws2_32.lib
Iphlpapi.lib

• Build / Rebuild Solution:

Note:

• When building for Debug, my Output window reads:
1>------ Rebuild All started: Project: ZeroMQ-Version, Configuration: Debug x64 ------
1>Source.cpp
1>ZeroMQ-Version.vcxproj -> C:\Repos\ZeroMQ-Version\x64\Debug\ZeroMQ-Version.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
• When building for Release, my Output window reads:
1>------ Rebuild All started: Project: ZeroMQ-Version, Configuration: Release x64 ------
1>Source.cpp
1>Generating code
1>All 3752 functions were compiled because no usable IPDB/IOBJ from previous compilation was found.
1>Finished generating code
1>ZeroMQ-Version.vcxproj -> C:\Repos\ZeroMQ-Version\x64\Release\ZeroMQ-Version.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Hope This Helps!

Tagged on: , ,

### 5 thoughts on “Build and static link ZeroMQ on Windows”

1. Joshua Burkholder

ZeroMQ has the best layout of MSBuild files of any project I’ve seen to date. When you clone ZeroMQ, these MSBuild files are located under its builds/deprecated-msvc/ path. For Visual Studio 2017, the combination of the *.props file under the builds/deprecated-msvc/properties/ path and the files under the builds/deprecated-msvc/vs2017/ path contains the magic. Unfortunately, as the path indicates, these MSBuild files are currently deprecated. This is one reason why my instructions above utilized Vcpkg instead of ZeroMQ’s own MSBuild files. Another reason is that Vcpkg is currently essential technology for rapid application development in C++ … esp. for MSBuild-based projects. Ensure you take a look at Vcpkg’s ability to export *.h/*.hpp, *.lib, *.dll, and *.exe to a NuGet package that you can then import into your C++ MSBuild projects and your MSBuild-based build systems (like Azure DevOps / VSTS / VSO):

vcpkg export package1:x64-windows ... packageN:x64-windows --nuget

… or, in the case of this post:

vcpkg export zeromq:x64-windows-static --nuget

… and adding the following to the *.vcxproj file where you installed this NuGet package (since you’ve exported a x64-windows-static package):

<PropertyGroup Label="Globals">
<!-- Keep the stuff that was already in your "Globals" PropertyGroup here and then add the following: -->
<VcpkgTriplet Condition="'$(Platform)'=='Win32'">x86-windows-static</VcpkgTriplet> <VcpkgTriplet Condition="'$(Platform)'=='x64'">x64-windows-static</VcpkgTriplet>
</PropertyGroup>

… Note: You’ll still need to add the ZMQ_STATIC define, static link the C++ runtimes, and add the Ws2_32.lib and Iphlpapi.lib libraries to your C++ project, but you won’t need to add the ZeroMQ include path nor the ZeroMQ libraries (as the *.targets files inside that NuGet package you just installed hooked your C++ project up with all those entries).
You can read more about vcpkg export here:

https://blogs.msdn.microsoft.com/vcblog/2017/05/03/vcpkg-introducing-export-command/

… and here:

https://github.com/Microsoft/vcpkg/blob/master/docs/users/integration.md#export
2. Cosma Cristian-Sebastian

Hi there,
this example you shown us is for c language…
what should i do to get the correct c++ libraries?

Best Regards Cris

1. Joshua Burkholder Post author

For portability reasons, the main interface for ZeroMQ is its C interface. Based on that C interface, bindings exists for various languages … including C++. Take note that a binding may bind a previous version of the C interface, not necessarily the current version of the C interface. Because most of C is a subset of C++, the C++ bindings are extremely trivial … so some people just create their own RAII wrappers around those C interfaces; however, ZeroMQ advertises the following C++ bindings:

In addition to the ZeroMQ port, Vcpkg contains ports for the first two C++ bindings mentioned above:

• cppzmq: vcpkg install cppzmq:x64-windows-static
• azmq: vcpkg install azmq:x64-windows-static
3. Surya Karmakar

Do i need to do any other steps to use in my source files of different projects? I can’t get it to work with an empty project in VS. Also can i use this on other C++ compiler, like devC++ or codeBlocks?

1. Joshua Burkholder Post author

The output of Step 2 above is a Header file (zmq.h) and some Static Libraries (libzmq-mt-sgd-4_3_1.lib for Debug and libzmq-mt-s-4_3_1.lib for Release). These ZeroMQ Static Libraries depend on some Windows Import Libraries that are found in the Windows SDK (like Ws2_32.lib, Iphlpapi.lib, and some of the other common Import Libraries). The Header, Static Libraries, and Import Libraries are all you need to use ZeroMQ in any other project on Windows. Step 3 above is a simple example of this. Since we are using a Static Library, we should use the same version of the compiler/linker toolchain for both Steps 2 and 3.

Sometimes things are clearer if we have another example to compare and contrast things against. In that vein, here’s one of several different ways to build and static link ZeroMQ on Windows using MinGW-w64 (the GCC compiler/linker toolchain on Windows) and CMake:

2. Download and install CMake. Note: I’m using version 3.13.2 via cmake-3.13.2-win64-x64.msi.
3. Download the latest release of libzmq. Note: I’m using libzmq 4.3.0.
4. From the Start menu, select MinGW-W64 project / Run terminal to launch a command prompt with all the MinGW-w64 environment variables set.
5. From that MinGW-w64 Run terminal command prompt, execute "C:\Program Files\CMake\bin\cmake-gui.exe" to launch the CMake GUI with access to those same MinGW-w64 environment variables.
6. Unzip the libzmq source code and create a build folder. Note: I unzipped my libzmq source code to C:\Repos\libzmq-4.3.0 and created the build folder C:\Repos\libzmq-4.3.0-build.
7. In the CMake GUI, enter the source code folder, enter the build folder, press Configure, and specify the following generator for this project: MinGW Makefiles … ensuring that the Use default native compilers radio button is selected … and then press Finish.
8. Once the initial configuration is complete and the usual red-highlights-indicating-new-changes are shown, press Configure again to keep the default entries and then press Generate.
9. Back in the MinGW-w64 Run terminal command prompt, execute the following:
cd /d C:\Repos\libzmq-4.3.0-build
mingw32-make.exe


… in order to kick off the build of ZeroMQ.

10. Once the build completes, locate the following files of interest:
• Header: C:\Repos\libzmq-4.3.0\include\zmq.h
• Static Library: C:\Repos\libzmq-4.3.0-build\lib\libzmq.a
11. Create a folder for a new project using the ZeroMQ bits we just built and copy the Source.cpp file from Step 3 above into that folder. Note: I created the following folder: C:\Repos\ZeroMQ-Version-MinGW containing C:\Repos\ZeroMQ-Version-MinGW\Source.cpp.
12. In the MinGW-w64 Run terminal command prompt, execute the following:
cd /d C:\Repos\ZeroMQ-Version-MinGW
g++ -o ZeroMQ-Version-MinGW.exe Source.cpp -O3 -Wall -Wextra -Werror -IC:\Repos\libzmq-4.3.0\include -DZMQ_STATIC -static -LC:\Repos\libzmq-4.3.0-build\lib -lzmq -lWs2_32 -lIphlpapi
ZeroMQ-Version-MinGW.exe


… in order to build and run the ZeroMQ-Version-MinGW.exe executable.

Note: Based on the choices I noted above, my output was:

Current 0MQ version is 4.3.0


Dev-C++ and Code::Blocks are both IDEs, not compiler/linker toolchains. Any IDE that wraps a compiler/linker toolchain with access to the Import Libraries from the Windows SDK will be good to go. If memory serves, both Dev-C++ and Code::Blocks wrap the MinGW toolchain … so if that’s still the case, you should be able to translate the MinGW-w64 steps above to the respective GUI-isms of those IDEs.

Hope This Helps.

This site uses Akismet to reduce spam. Learn how your comment data is processed.