当前位置: 技术问答>linux和unix
我这段程序为什么编译通不过?
来源: 互联网 发布时间:2015-07-16
本文导语: 源程序如下: #include #include #include void *fun() { printf("sub thread.n"); alarm(1); } void main_alarm(int i) { printf("Main gotn"); alarm(3); } main() { pthread_t pid; ...
源程序如下:
#include
#include
#include
void *fun()
{
printf("sub thread.n");
alarm(1);
}
void main_alarm(int i)
{
printf("Main gotn");
alarm(3);
}
main()
{
pthread_t pid;
struct sigaction aa;
sigset_t sigt;
sigfillset(&sigt);
aa.sa_handler = main_alarm;
aa.sa_mask = sigt;
aa.sa_flags = 0;
sigaction(SIGALRM, &aa, NULL);
int ret=pthread_create(&pid, NULL, fun, NULL);
while(1);
return 0;
}
编译信息如下:
gcc threadtest.c -o threadtest
/tmp/ccMHEp4d.o(.text+0xbc): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
make: *** [threadtest] Error 1
请问这是什么问题?
#include
#include
#include
void *fun()
{
printf("sub thread.n");
alarm(1);
}
void main_alarm(int i)
{
printf("Main gotn");
alarm(3);
}
main()
{
pthread_t pid;
struct sigaction aa;
sigset_t sigt;
sigfillset(&sigt);
aa.sa_handler = main_alarm;
aa.sa_mask = sigt;
aa.sa_flags = 0;
sigaction(SIGALRM, &aa, NULL);
int ret=pthread_create(&pid, NULL, fun, NULL);
while(1);
return 0;
}
编译信息如下:
gcc threadtest.c -o threadtest
/tmp/ccMHEp4d.o(.text+0xbc): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
make: *** [threadtest] Error 1
请问这是什么问题?
|
编译时没有链接相应的库
加参数 -lpthread
加参数 -lpthread
|
呵呵,那位老大说过了
gcc -lpthread threadtest.c -o threadtest
补充一点,函数最好写成 void *fun(void *arg);
否则用 g++ 编译类型检测不能通过
gcc -lpthread threadtest.c -o threadtest
补充一点,函数最好写成 void *fun(void *arg);
否则用 g++ 编译类型检测不能通过