
linux c语言问题
/*hello_multi.c-amulti-theadedhelloworldprogram*/#include<stdio.h>#include<pthread.h>...
/*hello_multi.c - a multi - theaded hello world program*/
#include <stdio.h>
#include <pthread.h>
#define NUM 5
int main(void)
{
pthread_t t1,t2;
void *print_msg(void *);
pthread_create (&t1,NULL,print_msg,(void *)"hello");
pthread_create (&t2,NULL,print_msg,(void *)"world\n");
pthread_join(t1,NULL);
pthread_join (t2,NULL);
return 0;
}
void *print_msg(void *m)
{
char *cp=(char *)m;
int i;
for(i=0;i<NUM;i++){
printf("%s",m);
fflush(stdout);
sleep(1);
}
return NULL;
}
有这些错误
main.c: 在函数‘print_msg’中:
main.c:21:9: 警告: 格式 ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘void *’ [-Wformat]
/tmp/ccHzcazW.o:在函数‘main’中:
main.c:(.text+0x29):对‘pthread_create’未定义的引用
main.c:(.text+0x4d):对‘pthread_create’未定义的引用
main.c:(.text+0x61):对‘pthread_join’未定义的引用
main.c:(.text+0x75):对‘pthread_join’未定义的引用
collect2: 错误: ld 返回 1
为什么 展开
#include <stdio.h>
#include <pthread.h>
#define NUM 5
int main(void)
{
pthread_t t1,t2;
void *print_msg(void *);
pthread_create (&t1,NULL,print_msg,(void *)"hello");
pthread_create (&t2,NULL,print_msg,(void *)"world\n");
pthread_join(t1,NULL);
pthread_join (t2,NULL);
return 0;
}
void *print_msg(void *m)
{
char *cp=(char *)m;
int i;
for(i=0;i<NUM;i++){
printf("%s",m);
fflush(stdout);
sleep(1);
}
return NULL;
}
有这些错误
main.c: 在函数‘print_msg’中:
main.c:21:9: 警告: 格式 ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘void *’ [-Wformat]
/tmp/ccHzcazW.o:在函数‘main’中:
main.c:(.text+0x29):对‘pthread_create’未定义的引用
main.c:(.text+0x4d):对‘pthread_create’未定义的引用
main.c:(.text+0x61):对‘pthread_join’未定义的引用
main.c:(.text+0x75):对‘pthread_join’未定义的引用
collect2: 错误: ld 返回 1
为什么 展开
3个回答
展开全部
main函数第二行
void *print_msg(void *);
这个是函数声明,应该放在main函数的上面,不要放在里面。
void *print_msg(void *);
这个是函数声明,应该放在main函数的上面,不要放在里面。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编译的时候有没有加上-lpthread命令?
还有printf("%s",m);应该是printf("%s",cp);吧
还有printf("%s",m);应该是printf("%s",cp);吧
追问
为什么要加-lpthread
追答
lpthread表示我要用到这个库,pthread_create等等线程函数这些都依赖这个库,编译的时候得告诉编译器我需要链接这个库
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
编译时加上-lpthread
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询