为啥我得到一个未定义的 pthread_create 引用? [复制]

Posted

技术标签:

【中文标题】为啥我得到一个未定义的 pthread_create 引用? [复制]【英文标题】:Why am I getting an undefined reference to pthread_create? [duplicate]为什么我得到一个未定义的 pthread_create 引用? [复制] 【发布时间】:2021-12-27 05:06:24 【问题描述】:

所以我正在用 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

【问题讨论】:

-pthread 添加到您的构建命令中。有关详细信息,请参阅重复的帖子。 嗯,它没有用,但我必须使用 C99 或 GNU99,我认为答案不会使用? 那么完整的命令是什么? gcc -pthread -std=gnu99 -o line_processor line_processor2.c? 【参考方案1】:

看起来你缺少编译标志-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

【讨论】:

这是行:gcc -std=gnu99 -o line_processor line_processor2.c -pthread? 它适用于这个! gcc -pthread -std=gnu99 -o line_processor line_processor2.c

以上是关于为啥我得到一个未定义的 pthread_create 引用? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

为啥我得到“未定义的 main 引用”

为啥我从 html doc 得到函数未定义错误?

cmake 返回“未定义对 pthread_create 的引用”的错误

为啥我在这个 SASS 文件中得到一个未定义的 Foundation 变量错误?

使用 ASIO 和 std::thread 制作 C++11 应用程序时对“pthread_create”的未定义引用错误

为啥我得到:“未定义的对 `glfwInit' 的引用”?