pBook‎ > ‎ZMQ‎ > ‎

Getting Started with the ZeroMQ (ØMQ) C++ Bindings

posted May 29, 2016, 6:06 AM by Javad Taghia   [ updated May 29, 2016, 6:06 AM ]

Getting Started with the ZeroMQ (ØMQ) C++ Bindings

February 28, 2015

Why ØMQ?

At a previous job some friends were working on a team creating a satellite communication system for cube-sat swarms. They started with the Node.js platform running on all the satellites, and for their communication protocol they decided on ZeroMQ.

Recently I became curious about running a service that would use High Performance Computing to solve a geopotential calculation. At first I thought it might be interesting to create a native add-on for Node.js that would provid access to the GPGPU processing power of one of Amazon's compute instances. But writing a native add-on isn't a trivial task and it seems more valuable to learn ZeroMQ and have node communicate directly with a C++ ZeroMQ enabled server.

This post is only about the process of getting the ZeroMQ hello world application up and running.

Installing ZeroMQ on a Macbook

I'm going to use Apple's XCode (you must install XCode before proceeding further through this tutorial) to write my C++ code in, so I need to setup a project that references the zeromqlib. This requires installing a bunch of build tools. I'll be using homebrew for my instalation supporting libraries. The requirements for building from source are listed on the ZeroMQ doc page. Depending on your setup a lot of these libraries are already installed.

zeromq-4.1.0-rc1.tar.gz is the POSIX tarball I downloaded from http://zeromq.org/area:download.

$ brew update
$ brew install libtool --universal
$ brew update libtool
$ brew install pkg-config --universal
$ brew update pkg-config
$ brew install autoconf --universal
$ brew update autoconf
$ brew install automake --universal
$ brew update automake
$ tar -xvzf zeromq-4.1.0-rc1.tar.gz
$ cd zeromq-4.1.0
$ ./configure
$ make
$ make install

Side note: build-essential is specific to Ubuntu therefore and is therefore excluded from the above bash commands for my mac install.

You can do all the above installing and making from scratch OR you can run the infinitely easier brew install call:

$ brew install --universal zeromq

ZeroMQ C++ Bindings

Next you'll need to download the C++ binding file from github. Then you'll copy the binding header file zmq.hpp into the include directory for the zeromq library you just built. In my case I copied it into /usr/local/include. That was easy, right?

XCode Project Creation and Setup

Now we need to create the client and server "Hello World" XCode projects from the ZeroMQ tutorial. In XCode, create a new command line application project for both the client "Hello World" application and the server "Hello World" application.

create xcode command line project

Once you click next you'll name the projects hello-world-client and hello-world-server and from the languages dropdown you'll select C++. For the client application you'll delete the main.cpp file and copy in the hwclient.cpp from the zguide github. And for the server application you'll delete the main.cpp file and copy in the hwserver.cpp from the zguide github.

If you build now you will get a build error "Lexical or Preprocessor Issue 'zmq.hpp' file not found". In order to get rid of this we need to adjust our Header Search Paths to include the location of our zeromq C++ binding header file and our other zeromq library headers. Add /usr/local/include to the Header Search Paths in the Search Pathssection of the Build Settings
header search paths

Now if we build we'll probably get a lot of "Apple Mach-O Linker(id) Error" errors. Add -lzmq to the Other Linker Flagslist in the Linking section of the Build Settings
lzmq other linker flags

Now if we build we'll have one last "Apple Mach-O Linker(id) Error" error. This is because we haven't indicated the location of our zeromq libraries. Add /usr/local/lib to the Library Search Paths in the Search Paths section of theBuild Settings
Library Search Paths

Our build should be successful! After you've updated the Header Search Paths, the Other Linker Flags, and the Library Search Paths for both projects we should be building without any problems. Now if we run both projects we should have successful communication between the client and server!!!!

Successful communications!!

Comments