首页 > 解决方案 > 为什么我得到一个未定义的 pthread_create 引用?

问题描述

所以我正在用 C 语言编写一个程序,它创建 4 个从缓冲区产生/使用的线程。我初始化了主函数中的所有线程,但出现以下错误。有谁知道为什么?我在 macOS 上的本地 zsh shell 上运行它,它运行良好。但是当我尝试在我学校的服务器上运行它时,我认为它是带有 bash 的 linux,它给了我错误。

flip1 ~/CS344/assignment4 1022$ gcc -std=gnu99 -o line-processor line_processor2.c
/tmp/ccYF7Kqe.o: In function `main':
line_processor2.c:(.text+0x7b5): undefined reference to `pthread_create'
line_processor2.c:(.text+0x7d9): undefined reference to `pthread_create'
line_processor2.c:(.text+0x7fd): undefined reference to `pthread_create'
line_processor2.c:(.text+0x821): undefined reference to `pthread_create'
line_processor2.c:(.text+0x832): undefined reference to `pthread_join'
line_processor2.c:(.text+0x843): undefined reference to `pthread_join'
line_processor2.c:(.text+0x854): undefined reference to `pthread_join'
line_processor2.c:(.text+0x865): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

下面是我的主要功能

int main()
{
    pthread_t inputThread, lineSeparatorThread, plusSignThread, outputThread;
    pthread_attr_t attr;
    
    // Set up Sentinal Values at the begining of each buffer to indicate whether or not
    // the buffer line has been read or not
    for (int i = 0; i < BUFSIZE; i++)
    {
        buffer1[i][0] = -1;
        buffer2[i][0] = -1;
        buffer3[i][0] = -1;
    }

    // Initialize a pthread attribute structure to set up joinable threads
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    // Initialize mutex and condition variables
    pthread_mutex_init(&mutex1, NULL);
    pthread_mutex_init(&mutex2, NULL);
    pthread_mutex_init(&mutex3, NULL);

    pthread_cond_init(&readyBuffer1, NULL);
    pthread_cond_init(&readyBuffer2, NULL);
    pthread_cond_init(&readyBuffer3, NULL);

    // Set up the thread in reverse order so that the readers/consumers will pend
    // waiting for the writers/consumers to start up

    pthread_create(&outputThread, &attr, output_thread, NULL);
    usleep(100); // Force the program to allow output thread to actually come up and pend on readyBuffer 3 first

    pthread_create(&plusSignThread, &attr, plus_sign_thread, NULL);
    usleep(100); // Force the program to allow plus_sign_thread thread to actually come up first

    pthread_create(&lineSeparatorThread, &attr, line_separator_thread, NULL);
    usleep(100); // Force the program to allow line_separator_thread thread to actually come up first

    pthread_create(&inputThread, &attr, input_thread, NULL);

    pthread_join(inputThread, NULL);
    pthread_join(lineSeparatorThread, NULL);
    pthread_join(plusSignThread, NULL);
    pthread_join(outputThread, NULL);

    // Freeing up memory.
    pthread_attr_destroy(&attr);
    pthread_mutex_destroy(&mutex1);
    pthread_mutex_destroy(&mutex2);
    pthread_mutex_destroy(&mutex3);
    pthread_cond_destroy(&readyBuffer1);
    pthread_cond_destroy(&readyBuffer2);
    pthread_cond_destroy(&readyBuffer3);

    return 0;
}

最后,我的#include 语句和缓冲区变量。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <ctype.h>


#define BUFSIZE 50

// Buffers can hold up to 50 lines that can be 1000 characters.
char buffer1[BUFSIZE][1000]; //inputThread + lineSeparatorThread
char buffer2[BUFSIZE][1000]; //lineSeparatorThread + plus_sign_thread
char buffer3[BUFSIZE][1000]; //plus_sign_thread + output_thread

标签: cmultithreadingpthreads

解决方案


它看起来像你缺少编译标志-pthread

试着把旗帜放在最后。gcc test.c -o test -std .... -pthread

来自 man7:

   int pthread_create(pthread_t *restrict thread,
                      const pthread_attr_t *restrict attr,
                      void *(*start_routine)(void *),
                      void *restrict arg);

   Compile and link with -pthread.`

https://man7.org/linux/man-pages/man3/pthread_create.3.html


推荐阅读