Compiling C code using the zmq API

I was unable to compile the built-in hwserver.c example using the ZeroMQ APIs. I tried all possible ways.

gcc -lzmq hwserver.c -o hwserver 

This tells me:

  hwclient.c:(.text+0x22): undefined reference to `zmq_ctx_new' hwclient.c:(.text+0x3a): undefined reference to `zmq_socket' hwclient.c:(.text+0x52): undefined reference to `zmq_connect' hwclient.c:(.text+0x94): undefined reference to `zmq_send' hwclient.c:(.text+0xb8): undefined reference to `zmq_recv' hwclient.c:(.text+0xe4): undefined reference to `zmq_close' hwclient.c:(.text+0xf0): undefined reference to `zmq_ctx_destroy' collect2: error: ld returned 1 exit status 

I am using zeromq-3.2.2 on ubuntu-12.10

Any help is really appreciated.

Thanks,

-Sam

+4
source share
2 answers

The order of the gcc arguments is important.

Try

  gcc -Wall -g hwserver.c -lzmq -o hwserver 

You need to first warn, optimize or debug flags (for example, -Wall for all warnings, -g for debugging information), and then optional preprocessor flags (for example, -D or -I , but you don’t have them), then the source files and finally, the -lzmq libraries (the order is relevant: from high-level libraries to low-level ones) and the output version is -o hwserver (which may be in another place).

Read the gcc documentation, in particular the chapter calling GCC .

Remember -Wall : you really want to get all warnings, and you should improve your code until warnings are given. You may even want -Wextra to receive additional warnings.

Don't forget the -g debug info flag: you will need to use the gdb debugger to debug your program.

Later use -O or -O2 to optimize the binary program (then the compiler will produce more efficient, but less debugged machine code). Only take care of this when your program is debugged.

As soon as you want to develop real-size C programs (that is, your project from several source files and some header files [s]), you will need a builder infrastructure such as GNU make (a very general builder, you can try omake ).

See also this answer to the corresponding question.

+18
source

try again

 gcc hwserver.c -o hwserver -lzmq 

Incorrect order of parameters.

0
source

All Articles