在我的机器ubuntu 16.0上编译pthread_join时发出警告

时间:2017-11-05 12:25:47

标签: linux ubuntu-16.04

我在添加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;
}

2 个答案:

答案 0 :(得分:1)

使用gcc进行编译时,请使用-lpthread标志。

例如:

gcc -o main main.c -lpthread

答案 1 :(得分:0)

你有一个错字:

pthread_created不是pthread_create