user2628946
user2628946

Reputation: 103

Golang zmq binding, ZMQ4, returns package error not finding file zmq.h

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

Upvotes: 5

Views: 4384

Answers (5)

Amirreza Noori
Amirreza Noori

Reputation: 1525

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.

Upvotes: 1

nmz_razor
nmz_razor

Reputation: 91

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

Upvotes: 0

Joels Elf
Joels Elf

Reputation: 784

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

Upvotes: 0

user2172816
user2172816

Reputation: 1200

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

Upvotes: 2

ivarg
ivarg

Reputation: 785

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.)

Upvotes: 2

Related Questions