问题
"I'm learning to create a tcp echo server from cesanta/mongoose https://github.com/cesanta/mongoose/blob/master/examples/tcp_echo_server/echo_server.c but when compiling it with cygwin using Makefile it won't work.
I tried learning Makefile too but so far I'm having a hard time understanding it. I also have included the header file on the current directory of the source file but it still won't work.
error:
$ make
gcc -o echo_server tcpechoserver.c mongoose.c -I.
/tmp/cct2s339.o:tcpechoserver.c:(.text+0x59): undefined reference to `mbuf_remove' "
There is a documentation on the github, but no explanation about this error. I also tried to copy the actual code and it still produced the same error.
#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
回答1:
there is an open issue of Cygwin compilation
the issue was created on 11 Apr 2018 and remain open till now
i cloned the most recent mongoose repository and compiled under linux and cygwin(64bit)
##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
my suggestion right now , use mongoose 6.6 as a workaround
it should make little difference in learning
and fixing such error is too much trouble for a learner
to compile the tcp_echo_server example from mongoose 6.6, use the following commands in cygwin
no extra copying, just make
you should find tcp_echo_server.exe in the current directory
git clone --branch 6.6 https://github.com/cesanta/mongoose
cd mongoose/examples/tcp_echo_server/
make
来源:https://stackoverflow.com/questions/57764329/cygwin64-terminal-undefined-reference-to-mbuf-remove