包含mongoose.h的未定义引用

Lea*_*g C -1 c windows compiler-errors undefined-reference mongoose-web-server

我试图运行mongoose c服务器示例,但是当我尝试编译示例时.我收到以下错误.如果我将它包含在标题中,这些引用怎么会丢失?我在windows下用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'
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*ock 5

您还需要确保链接到mongoose库.使用该-l标志(使用gcc)指定要链接的库,然后直接给出名称:-lmongoose

完整的命令行命令是:

gcc -lmongooose echo_server.c -out echo_server

编辑:OP说这不起作用,因为"mingw说它无法找到它."

您可能还需要添加库搜索路径标志,-L以帮助gcc找到您尝试链接的库.用法如下:

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

如果仍然无法解决OP的问题,此页面可能会提供更多信息:http://www.mingw.org/wiki/HOWTO_Specify_the_Location_of_Libraries_for_use_with_MinGW


归档时间:

查看次数:

515 次

最近记录:

9 年,8 月 前