首页 > 解决方案 > Cygwin64 终端:未定义对 `mbuf_remove` 的引用

问题描述

“我正在学习从 cesanta/mongoose https://github.com/cesanta/mongoose/blob/master/examples/tcp_echo_server/echo_server.c创建一个 tcp echo 服务器,但是当使用 Makefile 用 cygwin 编译它时,它不会工作。

我也尝试过学习 Makefile,但到目前为止我很难理解它。我还在源文件的当前目录中包含了头文件,但它仍然不起作用。

错误:

$ make
gcc -o echo_server tcpechoserver.c mongoose.c -I.
/tmp/cct2s339.o:tcpechoserver.c:(.text+0x59): undefined reference to `mbuf_remove' "

github上有一个文档,但是没有关于这个错误的解释。我还尝试复制实际代码,但仍然产生相同的错误。

#include "mongoose.h"

/*  ERROR CYGWIN64 TERMINAL
    gcc -o echo_server tcpechoserver.c mongoose.c -I.
    /tmp/cct2s339.o:tcpechoserver.c:(.text+0x59): undefined reference to `mbuf_remove' 
*/

// event handler
static void ev_handler(struct mg_connection *nc, int ev, void *p)
{
    // structure memory buffer descriptor
    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 = "3232", *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);

    // MAIN LOOP
    for (;;)
    {
        mg_mgr_poll(&mgr,1000);
    }
    mg_mgr_free(&mgr);

    return 0;
}
/tmp/cct2s339.o:tcpechoserver.c:(.text+0x59): undefined reference to `mbuf_remove'
/tmp/cct2s339.o:tcpechoserver.c:(.text+0x59): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x6f7a): undefined reference to `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x6f7a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x7df5): undefined reference to `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x7df5): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xb4f9): undefined reference to `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xb4f9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xc121): undefined reference to `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xc121): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xc1d0): more undefined references to `mbuf_remove' follow
/tmp/cca6g7CA.o:mongoose.c:(.text+0xc1d0): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xc22c): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0xc595): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x12e0b): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x13df9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `mbuf_remove'
/tmp/cca6g7CA.o:mongoose.c:(.text+0x1634a): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status
make: *** [Makefile:2: echo_server] Error 1

标签: cmongoosecygwinmongoose-web-serverembeddedwebserver

解决方案


Cygwin 编译有一个未解决的问题
该问题是在 2018 年 4 月 11 日创建的并且一直保持打开状态直到现在
我克隆了最新的 mongoose 存储库并在 linux 和 cygwin(64 位)下编译

##linux
cd mongoose/examples/tcp_echo_server
make all
cc echo_server.c ../../mongoose.c -o echo_server -g -W -Wall -Werror -I../.. -Wno-unused-function   -pthread
##no error with most recent version
##cygwin(64bit)
cd mongoose/examples/tcp_echo_server
make all
gcc echo_server.c ../../mongoose.c -o echo_server -g -W -Wall -Werror -I../.. -Wno-unused-function   -lws2_32
##undefined reference error with most recent version
##no error with tag 6.6


我现在的建议是,使用 mongoose 6.6 作为解决方法
,它应该对学习没有什么影响
,修复此类错误对学习者来说太麻烦了

要从 mongoose 6.6 编译 tcp_echo_server 示例,在 cygwin 中使用以下命令
无需额外复制,只需
在当前目录中找到 tcp_echo_server.exe

git clone --branch 6.6 https://github.com/cesanta/mongoose
cd mongoose/examples/tcp_echo_server/
make

推荐阅读