簡體   English   中英

為什么我得到對 pthread_create 的未定義引用?

[英]Why am I getting an undefined reference to 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

它看起來像你缺少編譯標志-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.`

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM