0

我从 [Geeks for Geeks][1] 复制了一个简单的代码来使用 pthread_create 创建线程

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
    sleep(1);
    printf("Printing GeeksQuiz from Thread \n");
    return NULL;
}

int main()
{
    pthread_t thread_id;
    printf("Before Thread\n");
    pthread_create(&thread_id, NULL, myThreadFun, NULL);
    pthread_join(thread_id, NULL);
    printf("After Thread\n");
    exit(0);
}

但是当我使用命令gcc 04_1.c -o 04_1 (04_1 是文件名)在我的机器( Ubuntu 20.04 )中运行它时,它会给出如下所示的错误。

/usr/bin/ld: /tmp/ccDru27X.o: in function `main':
04_1.cpp:(.text+0x6d): undefined reference to `pthread_create'
/usr/bin/ld: 04_1.cpp:(.text+0x7e): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

任何人都可以指导哪里出错了吗?以及如何解决这个问题?[1]:https ://www.geeksforgeeks.org/multithreading-c-2/

4

0 回答 0