我用下面的代码创建两个线程:
//header files
#include <pthread.h>
struct thread_arg
{
int var1;
int var2;
};
void *serv_com(void *pass_arg)
{
struct thread_arg *con = pass_arg;
//required statements irrelevant to the issue
pthread_exit(NULL);
}
void *cli_com(void *pass_arg)
{
struct thread_arg *con = pass_arg;
//required statements irrelevant to the issue
pthread_exit(NULL);
}
int main()
{
pthread_t inter_com;
//necessary code
while(1)
{
th_err_s = pthread_create(&inter_com, NULL, serv_com, (void *)&pass_arg);
th_err_c = pthread_create(&inter_com, NULL, cli_com, (void *)&pass_arg);
if (th_err_s || th_err_c)
{
printf("Alert! Error creating thread! Exiting Now!");
exit(-1);
}
}
pthread_exit(NULL);
return 1;
}
然后我编译使用下面的命令在linux上面的代码:
gcc -o sample sample.c
它返回以下错误信息:
inter.c:(.text+0x374): undefined reference to `pthread_create'
inter.c:(.text+0x398): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
我应该怎么做才能正确编译该文件。 我相信这是因为当我评论过的一切,而循环,程序正确编译内,我验证了在pthread_create语法是正确的没有语法错误或任何东西。 我一定要发出一些其他的命令编译文件?
编辑:有两个线程在上面的代码产生任何问题吗? 这个计划只是与错误信息一旦退出正在运行。 有什么可以可能的问题,我该怎么解决呢? 提前致谢。