我在添加pthread
库时遇到这些错误:
函数'pthread_created'的隐式声明[-Wimplicit-function-declaration] pthread_created(& id1,0,thread2,NULL);
对'pthread_created'的未定义引用collect2:错误:ld返回1退出状态
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
void* thread1(void* args)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
printf("%d\n", i);
}
}
}
void* thread2(void* args)
{
int i = 1;
for (i = 0; i < 100; i++)
{
if (i % 2!= 0)
{
printf("%d\n", i);
}
}
}
int main()
{
pthread_t id, id1;
printf("before thread 1 is created");
pthread_create(&id, 0, &thread1, NULL);
pthread_join(id, 0);
printf("after thread 1 is created\n");
printf("before thread 2 is created");
pthread_created(&id1, 0, thread2, NULL);
pthread_join(id1, 0);
printf("after thread2 is created");
return 0;
}
答案 0 :(得分:1)
使用gcc进行编译时,请使用-lpthread
标志。
例如:
gcc -o main main.c -lpthread
答案 1 :(得分:0)
你有一个错字:
pthread_created
不是pthread_create