/tmp/ccDU0AnY.o: In function `thread_loop':
udpclient.c:(.text+0x492): undefined reference to `gettid'
collect2: error: ld returned 1 exit status
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 1
在ubuntu 14.04上面编译程序,出现找不到gettid的问题。
在ubuntu 20.04上没问题
解决办法就是自己实现一个gettid 获取线程的id。
#include <unistd.h>
#include <sys/syscall.h>
#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif
#define gettid() ((pid_t)syscall(SYS_gettid))
#include <stdio.h>
void main()
{
printf("tid is %d\n", gettid());
}
据说也有人这样解决:
I solved my similar problem by define _GNU_SOURCE. Last question :
gcc -D_GNU_SOURCE source.c solve problem. Adding #define _GNU_SOURCE in code, before #include <unistd.h> have no effect.
#define _GNU_SOURCE
#include <unistd.h>
参考
getting error in c program "undefined reference to gettid" - Stack Overflow
还是要懂英文呀