7

I want to use the zeroMQ in my project and I run the configure as below to build the libaray into my home folder

./configure --enable-static --disable-shared --prefix=/home/xx/out

then I link my project by

gcc -o myproject x.c y.c /home/xx/out/libzmq.a

but there still a lot of link error like below:

../zmq/lib/libzmq.a(libzmq_la-ip.o): In function zmq::resolve_ip_interface(sockaddr_storage*, unsigned int*, char const*)':
/home/sureone/share/zeromq-2.2.0/src/ip.cpp:221: undefined reference to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/home/sureone/share/zeromq-2.2.0/src/ip.cpp:222: undefined reference to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
../zmq/lib/libzmq.a(libzmq_la-ip.o): In function zmq::resolve_ip_hostname(sockaddr_storage*, unsigned int*, char const*)':
/home/sureone/share/zeromq-2.2.0/src/ip.cpp:314: undefined reference to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, uns

...........

user207421
  • 305,947
  • 44
  • 307
  • 483
sureone
  • 831
  • 2
  • 14
  • 25

1 Answers1

6

gcc -o myproject x.c y.c /home/xx/out/libzmq.a

Since ZeroMQ is (apparently) using C++, you need to use appropriate compiler driver (g++ in this case) to link it.

Try this:

 gcc -c x.c y.c
 g++ -o myproject x.o y.o /home/xx/out/libzmq.a
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 1
    I have resolved this problem , should put -lstdc++ after all the object file and libzmq.a not before them. – sureone Apr 23 '12 at 05:20
  • 1
    @sureone "should put -lstdc++ after all the object" -- while this solution appears to work, it is not necessarily correct, and may not work tomorrow. You *really* ought to use `g++` when linking C++ code. – Employed Russian Apr 23 '12 at 05:36
  • 1
    Thanks for your advice, I tried as your way, it do resolved my issue. – sureone Apr 23 '12 at 08:31
  • I think you have statically compiled the zeroMQ library, but finally linked it dynamically. You should add `-static` to that link step in this answer. I will submit an edit. – C-- Sep 25 '20 at 13:54
  • @SubinSebastian You are mistaken: `-static` is neither necessary, nor desirable here. – Employed Russian Sep 25 '20 at 17:49
  • 1
    Isn't that OP's question? I see s/he want to statically link zeroMQ – C-- Sep 26 '20 at 18:04
  • @SubinSebastian The OP question is how to link against archive version of ZeroMQ and this answer (_without_ `-static`) achieves _exactly_ that. Added `-static` achieves something _else_. If you don't understand the difference, you can ask a question about that. – Employed Russian Sep 27 '20 at 03:08