pebbe / zmq4

A Go interface to ZeroMQ version 4
BSD 2-Clause "Simplified" License
1.17k stars 163 forks source link

Cannot do static build using zmq4 #60

Closed paulbdavis closed 8 years ago

paulbdavis commented 8 years ago

I am trying to build a static binary and I get the following error

CGO_ENABLED=0 go get -a -ldflags '-s' github.com/pebbe/zmq4
# github.com/pebbe/zmq4
/go/src/github.com/pebbe/zmq4/auth.go:564: undefined: Socket
/go/src/github.com/pebbe/zmq4/auth.go:573: undefined: Socket
/go/src/github.com/pebbe/zmq4/auth.go:582: undefined: Socket
/go/src/github.com/pebbe/zmq4/auth.go:594: undefined: Socket
/go/src/github.com/pebbe/zmq4/auth.go:603: undefined: Socket
/go/src/github.com/pebbe/zmq4/reactor.go:10: undefined: State
/go/src/github.com/pebbe/zmq4/reactor.go:11: undefined: State
pebbe commented 8 years ago

zmq4 is a binding for the C library ZeroMQ. You need CGO enabled.

paulbdavis commented 8 years ago

Just FYI, I was able to get a static binary by doing the following

CGO_CPPFLAGS="-I/usr/include" \
CGO_LDFLAGS="-L/usr/lib -L/usr/lib/z86_64-linux-gnu -lzmq -lpthread -lsodium -lrt -lstdc++ -lm -lc -lgcc" \
go get --ldflags '-extldflags "-static"' -a myapp/usingzmq4

I know lots of people like using static binaries for deployment. Not sure if this info is helpful to you or others

For searchability golang go static compile zeromq zmq zmq4

aletheia7 commented 8 years ago

Thank you for the answer. This was on my todo list.

paulbdavis commented 8 years ago

@aletheia7 You may need to adjust the paths depending on what you are using to compile. The above works inside the golang docker container in which I manually compiled zeromq 4.1.3 (installing it from debian jessie gives 4.0.1)

pebbe commented 8 years ago

This may be useful too in some cases:

http://statifier.sourceforge.net/

https://groups.google.com/forum/m/#!topic/golang-nuts/7YH5CseB4pc

pebbe commented 8 years ago

I could get a static binary like this:

PKG_CONFIG_PATH=/my/opt/zeromq-4.1.3/lib/pkgconfig \
CGO_LDFLAGS="-L/my/opt/libsodium-1.0.3/lib -lsodium -lstdc++ -lrt" \
go build -v --ldflags '-extldflags "-static"' -a program.go

But first I had to install ZeroMQ without libnorm, because there is no static version of that library.

scragg0x commented 8 years ago

If you were to build it statically, where would this not work For example, if I build on Ubuntu 16.04 and try to deploy on CentOS 6, would that be ok? Would it just work on any linux amd64 distro?

aletheia7 commented 7 years ago

@pebbe,

A static norm build of the norm library can be built, but it is not built by the waf script.

Steps to build libnorm.a

  1. cd <norm source>
  2. waf configure
  3. waf
  4. cd makefiles
  5. make -f Makefile.linux # choose the Makefile.* for your platform.
  6. cd ..
  7. ls -l lib # libnorm.a should be present.

Linking libnorm.a requires libprotokit.a under /protolib/lib. Use "-lprotokit."

Use libprotokit, not libprotolib.

RickCastle2018 commented 2 years ago

@pebbe, could you say, what's required to statically build zmq4 these days?

RickCastle2018 commented 2 years ago

I get errors like this, tried everything already image

aletheia7 commented 2 years ago

I have an app that uses github.com/pebbe/zmq4 v1.2.8 . I made a branch. I changed zmq4.go .

Removed

/*
#cgo !windows pkg-config: libzmq

Added

/*
#cgo !windows CPPFLAGS: -I/usr/local/include
#cgo !windows LDFLAGS: -L/usr/local/lib -L/usr/lib/i386-linux-gnu -lzmq -lpthread -lsodium -lrt -lstdc++ -lm -lc -lgcc
  1. You need to statically compile libsodium first. Use configure and only build statically and use an install prefix of /usr/local (or adjust /usr/loca/... above).
  2. You need to statically compile libzmq with configure. It should use the static libsodium.

Be sure you have previously removed libsodium and libzmq packages so that you do not have multiple builds.