Compiling C code using zmq API

c, linker, zeromq

Solution

Order of arguments to `gcc` does matter a lot.

Try

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

You need first the warning and optimizations or debugging flags (e.g. `-Wall` for all warnings, `-g` for debugging information), then the optional preprocessor flags (like `-D` or `-I` but you have none of them), then the source files, and at last the libraries `-lzmq` (order is relevant: from high level libraries to low level ones) and the output option `-o hwserver` (which could be elsewhere).

Read the `gcc` documentation, notably the chapter about invoking GCC.

Don't forget the `-Wall` : you really want to get all warnings, and you should improve your code till no warnings are given. You could even want `-Wextra` to get more extra warnings.

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

Later, use `-O` or `-O2` to optimize the binary program (the compiler will then produce more efficient, but less debuggable, machine code). Care about that only when your program is debugged.

As soon as you want to develop real-sized C programs (i.e. your project made of several source files and some header file[s]), you'll need a builder infrastructure, like GNU make (a very common builder; you could try omake instead).

See also this answer to a related question.

Problem

I am failed to compile builtin example hwserver.c using ZeroMQ APIs. I have tried every possible way. ``` gcc -lzmq hwserver.c -o hwserver ``` It prompts me with: ``` 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'm using zeromq-3.2.2 on ubuntu-12.10 Any help really appreciated. Thanks, -Sam

Original source

Related problems