how to link a static c++ library

1,176 views
Skip to first unread message

Jimmy

unread,
Sep 24, 2015, 9:30:13 AM9/24/15
to golang-nuts
Hi.
In my project, I use  gozmq(https://github.com/alecthomas/gozmq) that  provides binding for c++ zmq library. In my  development environment, I build gozmq  via the standard dynamic linking to zmq successfully. But, I want to  link statically to reduce  dependence.
I have change gozmq source file like this:
package gozmq

/*
-#cgo pkg-config: libzmq
+#cgo CFLAGS: -I/root/install/zeromq-4.1.3/include
+#cgo LDFLAGS: /root/install/zeromq-4.1.3/.libs/libzmq.a
#include <zmq.h>
#include <stdlib.h>
#include <string.h>
But it doesn't work and the compile error is like this(just post a part of errors):
/root/install/zeromq-4.1.3/.libs/libzmq.a(libzmq_la-zmq.o): In function `zmq_ctx_new':
/root/install/zeromq-4.1.3/src/zmq.cpp:155: undefined reference to `std::nothrow'
/root/install/zeromq-4.1.3/src/zmq.cpp:155: undefined reference to `operator new(unsigned long, std::nothrow_t const&)'
/root/install/zeromq-4.1.3/src/zmq.cpp:155: undefined reference to `operator delete(void*, std::nothrow_t const&)'
/root/install/zeromq-4.1.3/.libs/libzmq.a(libzmq_la-zmq.o): In function `zmq_msg_gets':
/root/install/zeromq-4.1.3/src/zmq.cpp:666: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
/root/install/zeromq-4.1.3/.libs/libzmq.a(libzmq_la-zmq.o): In function `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_M_dispose(std::allocator<char> const&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:232: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_empty_rep_storage'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:236: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_M_destroy(std::allocator<char> const&)'
/root/install/zeromq-4.1.3/.libs/libzmq.a(libzmq_la-zmq.o): In function `zmq_msg_gets':/root/install/zeromq-4.1.3/src/zmq.cpp:666: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
.
.
.
.
.
.
Have I do something wrong?Is there a way to link a static c++ library like zmq in my go project?

Ian Lance Taylor

unread,
Sep 24, 2015, 11:18:11 AM9/24/15
to Jimmy, golang-nuts
These errors are occurring because you are not linking against the C++
libraries. One simple fix might be to put a .cc file in the
directory; it can probably even be empty. The presence of a .cc file
should cause the go tool to link your program with the C++ driver,
which should add the required C++ libraries. Or, you can those
libraries yourself on your #cgo LDFLAGS line.

Ian

Jimmy

unread,
Sep 25, 2015, 2:37:26 AM9/25/15
to golang-nuts, guiyo...@gmail.com, andrey mirtchovski
After adding -lstdc++ to the LDFLAGS, it works. Thanks to Ian and andrey mirtchovski.
Reply all
Reply to author
Forward
0 new messages