cwindowscompiler-errorsundefined-referencemongoose-web-server

Undefined reference even with mongoose.h included


I am trying to run the mongoose c server examples but when I try to compile the examples. I get the following error. How can these reference be missing if I include it in the header? I am compiling under windows with mingw.

gcc echo_server.c -out echo_server
echo_server.c:(.text+0x35): undefined reference to `mg_send'
echo_server.c:(.text+0x4a): undefined reference to `mbuf_remove'
echo_server.c:(.text+0x7f): undefined reference to `mg_mgr_init'
echo_server.c:(.text+0x9b): undefined reference to `mg_bind'
echo_server.c:(.text+0xb7): undefined reference to `mg_bind'
echo_server.c:(.text+0xe7): undefined reference to `mg_mgr_poll'

This is the echo_server.c

#include "mongoose.h"

static void ev_handler(struct mg_connection *nc, int ev, void *p) {
  struct mbuf *io = &nc->recv_mbuf;
  (void) p;

  switch (ev) {
    case MG_EV_RECV:
      mg_send(nc, io->buf, io->len);  // Echo message back
      mbuf_remove(io, io->len);        // Discard message from recv buffer
      break;
    default:
      break;
  }
}

int main(void) {
  struct mg_mgr mgr;
  const char *port1 = "1234", *port2 = "127.0.0.1:17000";

  mg_mgr_init(&mgr, NULL);
  mg_bind(&mgr, port1, ev_handler);
  mg_bind(&mgr, port2, ev_handler);

  printf("Starting echo mgr on ports %s, %s\n", port1, port2);
  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}

Solution

  • You need to make sure to link with the mongoose library as well. The -l flag is used (with gcc) to specify a library to link with, you then give the name directly afterwards: -lmongoose

    The full command line command would be:

    gcc -lmongooose echo_server.c -out echo_server

    Edit: OP said that this didn't work because "mingw say it can not find it."

    You may also need to add the library search path flag -L to help gcc find the library where you're trying to link. Usage is as follows:

    gcc -L C:\path\to\library -lmongoose echo_server.c -out echo_server

    This page might have some more information if this still didn't solve OP's question: http://www.mingw.org/wiki/HOWTO_Specify_the_Location_of_Libraries_for_use_with_MinGW