Simple gcc intro/reminder with 0mq
Just as a quick reminder and test for a very minimal setup with gcc and the 0mq library. This just compiles a static version of 0mq and runs one of the examples.
I've downloaded the 2.2.0 version... Unpack it and go into the directory. Read the INSTALL and README so you know :-|
*You'll need the dependencies like uuid-dev... so install them [^1]
[^1]: apt-get install uuid-dev*
Run the following to configure and compile
./configure --enable-static --disable-shared
make
Fix any errors. Usually that means installing dependencies.... Once you've succesfully compiled you don't need to run 'make install'. That only copies files to your system. You can find the static lib in 'src/.libs/libzmq.a'. That's the candy you need.
A .a file is just an archive of all the .o files. Run the following to prove that
$ ar t src/.libs/libzmq.a
libzmq_la-clock.o
libzmq_la-command.o
libzmq_la-ctx.o
libzmq_la-connect_session.o
libzmq_la-decoder.o
libzmq_la-device.o
libzmq_la-devpoll.o
libzmq_la-dist.o
libzmq_la-encoder.o
libzmq_la-epoll.o
libzmq_la-err.o
libzmq_la-fq.o
libzmq_la-io_object.o
libzmq_la-io_thread.o
libzmq_la-ip.o
libzmq_la-kqueue.o
libzmq_la-lb.o
libzmq_la-mailbox.o
libzmq_la-named_session.o
libzmq_la-object.o
libzmq_la-options.o
libzmq_la-own.o
libzmq_la-pair.o
libzmq_la-pgm_receiver.o
libzmq_la-pgm_sender.o
libzmq_la-pgm_socket.o
libzmq_la-pipe.o
libzmq_la-poll.o
libzmq_la-poller_base.o
libzmq_la-pull.o
libzmq_la-push.o
libzmq_la-reaper.o
libzmq_la-pub.o
libzmq_la-rep.o
libzmq_la-req.o
libzmq_la-select.o
libzmq_la-session.o
libzmq_la-signaler.o
libzmq_la-socket_base.o
libzmq_la-sub.o
libzmq_la-swap.o
libzmq_la-tcp_connecter.o
libzmq_la-tcp_listener.o
libzmq_la-tcp_socket.o
libzmq_la-thread.o
libzmq_la-transient_session.o
libzmq_la-trie.o
libzmq_la-uuid.o
libzmq_la-xpub.o
libzmq_la-xrep.o
libzmq_la-xreq.o
libzmq_la-xsub.o
libzmq_la-zmq.o
libzmq_la-zmq_connecter.o
libzmq_la-zmq_engine.o
libzmq_la-zmq_init.o
libzmq_la-zmq_listener.o
So now create a test.c file containing the following example from the 0mq site:
//
// Hello World server
* Binds REP socket to tcp:**:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main (void)
{
void *context = zmq_init (1);
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
zmq_bind (responder, "tcp://*:5555");
while (1) {
// Wait for next request from client
zmq_msg_t request;
zmq_msg_init (&request);
zmq_recv (responder, &request, 0);
printf ("Received Hello\n");
zmq_msg_close (&request);
// Do some 'work'
sleep (1);
// Send reply back to client
zmq_msg_t reply;
zmq_msg_init_size (&reply, 5);
memcpy (zmq_msg_data (&reply), "World", 5);
zmq_send (responder, &reply, 0);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_term (context);
return 0;
}
Let's compile and not link it yet. Compiling means we make .o file, a translation of the C code to machine code.
gcc -c test.c -I include/
- -c means compile not link
- -I specifies the directory where 'zmq.h' can be found
Now we need to link all of our machine code files so we have our executable. Since 0mq uses c++ we need to use g++ instead of gcc.
g++ -o test test.o src/.libs/libzmq.a -lpthread -luuid -lrt
- -o means set the output file
- test.o src/.libs/libzmq.a means link those two files
- -lpthread -luuid -lrt means link also library pthread, uuid and rt since they are needed
This will provide you with the 'test' exectable. Run it and see that it listens on port 5555.
./test &
netstat -anp | grep 5555
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN 14750/test
That's it.