5

I am trying to include ZMQ sockets in a Go app but both zmq4 and gozmq (the referred ZMQ binding libraries for Go) are giving me problems. I would like to understand why zmq4 specifically isn't importable on my system.

I am running a Windows 8 system and I used the windows installer from the ZMQ website for version 4.0.3. I am primarily concerned about getting zmq4 set up and here is the result of my "go get" query on the github library's location:

> go get github.com/pebbe/zmq4
# github.com/pebbe/zmq4
polling.go:4:17: fatal error: zmq.h: No such file or directory
compilation terminated.

This issue is not alleviated by cloning the Github repository - the error remains the same.

I know the issue has to do with the C library zmq.h that is located in the "include" folder of my ZMQ installation, but whether the dependency is held up by a pathing issue or an external tool issue is a mystery to me.

A similar error has come up in regards to node.js and is the solution I see others referred to, outside of node scripting, but it was unsuccessful in my case.

I've so far included the path to the "include" folder in my PATH environment variable and previously placed zmq.h inside of the zmq4 top-level folder. I don't have much of an arsenal otherwise to understand this problem because I am new to C and C-importing packages in Go

user2628946
  • 103
  • 2
  • 6
  • I don't think you'll get an answer without providing more information. What platform are you using? How did you install ZMQ? What commands did you use to try and build gozmq? – James Henstridge Mar 07 '14 at 06:32
  • I've edited for more information, let me know what else is needed if I missed something. Also I'm primarily interested in a solution to my zmq4 error; I have a better idea of what is hanging up gozmq – user2628946 Mar 07 '14 at 08:55
  • @user2628946 Any progress? I have the same issue with zmq4, and gozmq gives me :`# pkg-config --cflags libzmq libzmq libzmq libzmq exec: "pkg-config": executable file not found in %PATH%` – 10 cls Mar 09 '14 at 16:59
  • 1
    @user2628946: try setting the environment variables `C_INCLUDE_PATH` and `LIBRARY_PATH` to include your zmq installation or, if zmq4 uses pkg-config, adding a .pc file with your installation information... it sounds like you have zmq installed in a non-standard location and `go build` isn't picking it up. @10cls: looks like gozmq uses pkg-config to get the flags for building against libzmq and you don't have pkg-config installed. Install it and make sure you have a valid *.pc file for your zmq installation and try again. – photoionized Apr 08 '14 at 07:16

5 Answers5

2

I wanted to do the same thing, but on Windows 7, and here is what I had to do.

Since the Go bindings are using cgo to integrate with zeromq, you need zeromq built with gcc. There are no pre-built binaries, so you'll have to build them yourself, with mingw or similar, but this process is easier than it may sound, and nicely described on the zeromq site.

As @photoionized pointed out, C_INCLUDE_PATH and LIBRARY_PATH need to be set when building the Go bindings.

(In my case, I ran into a problem when compiling libzmq with IN6_ADDR not being defined. The only solution I found was, inspired by this issue, to manually add the line #include <in6addr.h> to the windows.hpp file.)

ivarg
  • 755
  • 1
  • 8
  • 11
2

The Windows installer version of ZeroMQ won't work with zmq4, you need to compile from source with gcc, I recommend using MSYS2.

  1. Install and update MSYS2 following the instructions from http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/
  2. Start the mingw32_shell.bat or mingw64_shell.bat based on Go arch (32bit or 64bit)
  3. pacman -S mingw-w64-(x86_64|i686)-toolchain make (x86_64 for 64bit, i686 for 32bit)
  4. cd into zeromq src folder (C:\ path starts with /c/ inside the shell)
  5. ./configure
  6. make
  7. make install
  8. CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go get github.com/pebbe/zmq4
  9. Copy the following dlls and put them next to your go program (.exe): /usr/local/bin/libzmq.dll /mingw(32|64)/bin/libgcc*.dll /mingw(32|64)/bin/libwinpthread*.dll /mingw(32|64)/bin/libstdc++*.dll
user2172816
  • 1,200
  • 8
  • 11
0

Here's updated steps for @user2172816's MSYS2 solution:

  1. Install and update MSYS2 following the instructions from http://sourceforge.net/p/msys2/wiki/MSYS2%20installation/
  2. Start the mingw32_shell.bat or mingw64_shell.bat based on Go arch (32bit or 64bit)
  3. pacman -S mingw-w64-(x86_64|i686)-toolchain make (x86_64 for 64bit, i686 for 32bit)
  4. Add C:\msys64\mingw64\bin to your Path (pkg-config is there)
  5. Restart the msys2 shell to get the new Path
  6. Download and unzip libsodium source: https://github.com/jedisct1/libsodium/releases
  7. cd into libsodium folder (C:\ path starts with /c/ inside the shell)
  8. ./configure --build=(x86_64|i686)-w64-mingw32
  9. make
  10. make install
  11. Add /usr/local/lib to PKG_CONFIG_PATH (export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig)
  12. cd into zeromq src folder
  13. ./configure --build=(x86_64|i686)-w64-mingw32
  14. Add

    #ifdef ZMQ_HAVE_MINGW32

    #include <winsock2.h>

    #include <windows.h>

    #include "netioapi.h"

    #endif

To the top of src/tcpaddress.cpp

  1. make
  2. make install
  3. CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go get github.com/pebbe/zmq4
  4. CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go build in your project directory
  5. Copy the following dlls and put them next to your go program (.exe):

    /usr/local/bin/libzmq.dll /mingw(32|64)/bin/libgcc*.dll /mingw(32|64)/bin/libwinpthread-*.dll /mingw(32|64)/bin/libstdc++*.dll /usr/local/bin/libsodium-*.dll

maybe? /usr/local/bin/libsodium-*.def

Joels Elf
  • 714
  • 6
  • 10
0

An updated answer using MSYS2.

  1. Install MSYS2 MSYS2 installation guide.
  2. Make sure to choose the correct installation 32bit or 64bit.
  3. Open the appropriate shell MSYS2 MinGW 64-bit or MSYS2 MinGW 32-bit. All further steps assume you are using this shell.
  4. Update packages following instructions at the installation guide.
  5. Install libtool pacman -Sy libtool.
  6. Download zmq source code to a location of your choice.
  7. Navigate to the zmq source folder.
  8. To generate the configure file, run the autogen tool by running ./autogen.sh.
  9. In the probable case that step 8 fails:

    1. Find the file at fault (probably version.sh).
    2. Replace line endings by (replace file by the actual filename).
      cp file file.bak

      tr -d '\r' <file.bak> file

    3. If this fails you'll have to dive in the code and find the problem.
  10. Run the configure tool ./configure.

  11. In the probable case of failure. Comment out empty else clauses in the configure file.

  12. Add Go to Path: PATH=${PATH}:<go bin directory>.

  13. Install Go Package: CGO_CFLAGS=-I/usr/local/include CGO_LDFLAGS=-L/usr/local/lib go get github.com/pebbe/zmq4
nmz_razor
  • 81
  • 1
  • 2
0

To install ZMQ in windows: Problem in Installing Golang ZMQ for windows - fatal error: czmq.h: No such file or directory

First of all, install the msys64. Download the software from https://www.msys2.org/ and install it on C:\msys64.

Then add C:\msys64\mingw64\bin to PATH environment variable of the windows.

Then run the following commands (in CMD) one by one.

pacman -Su
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
pacman -S base-devel gcc vim cmake
pacman -S mingw-w64-x86_64-libsodium
pacman -S mingw-w64-x86_64-zeromq

Finally, run the Go install command:

go get github.com/pebbe/zmq4

Finished.

Amirreza Noori
  • 1,456
  • 1
  • 6
  • 20