- 论坛徽章:
- 0
|
本人新手,在学习APUE,遇到下面的问题,哪位朋友能帮帮忙解决下
注:在ubuntu环境下
#include "apue.h"
#include <pthread.h>
struct foo {
int a, b, c, d;
};
void
printfoo(const char *s, const struct foo *fp)
{
printf(s);
printf(" structure at 0x%x\n", (unsigned)fp);
printf(" foo.a = %d\n", fp->a);
printf(" foo.b = %d\n", fp->b);
printf(" foo.c = %d\n", fp->c);
printf(" foo.d = %d\n", fp->d);
}
void *
thr_fn1(void *arg)
{
struct foo foo = {1, 2, 3, 4};
printfoo("thread 1:\n", &foo);
pthread_exit((void *)&foo);
}
void *
thr_fn2(void *arg)
{
printf("thread 2: ID is %d\n", pthread_self());
pthread_exit((void *)0);
}
int
main(void)
{
int err;
pthread_t tid1, tid2;
struct foo *fp;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0)
err_quit("can't create thread 1: %s\n", strerror(err));
err = pthread_join(tid1, (void *)&fp);
if (err != 0)
err_quit("can't join with thread 1: %s\n", strerror(err));
sleep(1);
printf("parent starting second thread\n" ;
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != 0)
err_quit("can't create thread 2: %s\n", strerror(err));
sleep(1);
printfoo("parent:\n", fp);
exit(0);
}
编译没通过,不知为啥,哪位朋友帮忙解决下
编译的结果:fay@ubuntu:~/APUE/programming/11-3$ gcc 111-3.c error.c -o 111-3
/tmp/cceOIH2c.o: In function `main':
111-3.c .text+0x67): undefined reference to `pthread_create'
111-3.c .text+0xae): undefined reference to `pthread_create'
111-3.c .text+0xe4): undefined reference to `pthread_join'
111-3.c .text+0x12d): undefined reference to `pthread_join'
collect2: ld 返回 1
很郁闷为什么会出现上面的结果,还有,请帮帮忙解释一下pthread_exit((void *)&foo)和pthread_join(tid1, (void *)&fp)参数到底是怎么回事阿? |
|