【问题标题】:Warning while compiling pthread_join on my machine ubuntu 16.0在我的机器 ubuntu 16.0 上编译 pthread_join 时出现警告
【发布时间】:2017-11-05 12:25:46
【问题描述】:

我在包含 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;
}

【问题讨论】:

  • 这是一个链接器错误。你是如何链接这个的?

标签: linux ubuntu-16.04


【解决方案1】:

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

例如:

gcc -o main main.c -lpthread

【讨论】:

    【解决方案2】:

    你有一个错字:

    pthread_created 不是pthread_create

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 2015-08-11
      • 2019-11-23
      相关资源
      最近更新 更多