MT7688开发板/openwrt系统--多线程编程1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lllxxxyyy0/article/details/84997830

本节介绍多线程程序在虚拟机上编译遇到的问题

先贴代码

#include <stdlib.h> 
#include <stdio.h> 
#include <errno.h> 
#include <string.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <pthread.h>

void thread1(void)
{
	int i=0;
	for(i=0;i<10;i++){
		printf("This is a pthread1.\n");
		if(i==2)
			pthread_exit(0);
		sleep(1);
		}
}

void thread2(void)
{
	int i;
	for(i=0;i<10;i++)
		printf("This is a pthread2.\n");
	pthread_exit(0);
}

int main(void)
{
	pthread_t id1,id2;
	int i,ret;
	ret=pthread_create(&id1,NULL,(void *) thread1,NULL);
	if(ret!=0){
		printf ("Create pthread error!\n");
	exit (1);
	}
	ret=pthread_create(&id2,NULL,(void *) thread2,NULL);
	if(ret!=0){
		printf ("Create pthread error!\n");
		exit (1);
	}
	pthread_join(id1,NULL);
	pthread_join(id2,NULL);
	exit (0);
}

在虚拟机上编译,出现如下错误,百度下,

问题的原因:pthread不是linux下的默认的库,也就是在链接的时候,无法找到phread库中哥函数的入口地址,于是链接会失败。

解决:在gcc编译的时候,附加要加 -lpthread参数即可解决

加上-lpthread编译,如下,ok了

运行一下:

猜你喜欢

转载自blog.csdn.net/lllxxxyyy0/article/details/84997830