-2

I'm trying to install zeromq but I'm having some problems with undefined reference . I used this tutorial to install zeromq in my machine, with the difference that I downloaded version 4.1.4 and not 4.1.2.

Then I'm trying to run the following code (got from zeromq tutorial) in C:

//  Hello World server

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{
    //  Socket to talk to clients
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);

    while (1) {
        char buffer [10];
        zmq_recv (responder, buffer, 10, 0);
        printf ("Received Hello\n");
        sleep (1);          //  Do some 'work'
        zmq_send (responder, "World", 5, 0);
    }
    return 0;
}

with this line:

gcc program.c -lzmq

and I got this error:

/tmp/cc3OkNsE.o: In function `main':
program.c:(.text+0x18): undefined reference to `zmq_ctx_new'
collect2: error: ld returned 1 exit status

I already did some research but I couldn't find any clear solutions/instructions. Anyone knows how to solve it or what I'm doing wrong?

CoolTarka
  • 103
  • 1
  • 8
  • Have a look at [this question](https://stackoverflow.com/questions/12470117/compile-simple-hello-world-zeromq-c-example-compile-flags). – Maarten Arits Sep 09 '17 at 17:21
  • What is the output of `sudo ldconfig -p | grep libzmq`? Where did you install `libzmq`? – Ralf Stubner Sep 09 '17 at 17:28
  • Ralf is pointing you in the right direction. Most likely you are actually linking with an old version of libzmq (e.g. version 2.*) which did not have this function defined. You should use the -L flag to point to your version 4.1.4 – GroovyDotCom Sep 09 '17 at 17:58

1 Answers1

1

As pointed by Maarten Artis in the comments above, it wasn't actually linking the library. The correct command line is:

gcc -Wall program.c -o prog -L/usr/local/lib -lzmq
CoolTarka
  • 103
  • 1
  • 8