未定义使用g ++编译时对sem_wait和pthread_create的引用

时间:2016-10-18 22:23:26

标签: c

我是C的新手,当我尝试使用g++ ./main.c编译程序时,我不明白为什么会出现这些错误。我试过看其他资源,我无法找到我需要的答案。如果有您已经知道的解决方案,请在此处发布。

/tmp/ccSGRAcp.o: In function `producer(void*)':
main.c:(.text+0x12): undefined reference to `sem_wait'
main.c:(.text+0x1c): undefined reference to `sem_wait'
main.c:(.text+0xa0): undefined reference to `sem_post'
main.c:(.text+0xaa): undefined reference to `sem_post'
/tmp/ccSGRAcp.o: In function `consumer(void*)':
main.c:(.text+0xc5): undefined reference to `sem_wait'
main.c:(.text+0xcf): undefined reference to `sem_wait'
main.c:(.text+0x153): undefined reference to `sem_post'
main.c:(.text+0x15d): undefined reference to `sem_post'
/tmp/ccSGRAcp.o: In function `main':
main.c:(.text+0x17e): undefined reference to `sem_init'
main.c:(.text+0x192): undefined reference to `sem_init'
main.c:(.text+0x1a6): undefined reference to `sem_init'
main.c:(.text+0x1c1): undefined reference to `pthread_create'
main.c:(.text+0x1dc): undefined reference to `pthread_create'
main.c:(.text+0x1f7): undefined reference to `pthread_create'
main.c:(.text+0x212): undefined reference to `pthread_create'
main.c:(.text+0x22d): undefined reference to `pthread_create'
/tmp/ccSGRAcp.o:main.c:(.text+0x248): more undefined references to `pthread_create' follow
collect2: error: ld returned 1 exit status

我想编译的代码是

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <pthread.h>

#define N 10000000

typedef struct
{
    char const* buf[N];
    char in;
    char out;

    sem_t mutex;
    sem_t full;
    sem_t empty;

} bufferItems;

bufferItems sharedBuffer;


void *producer(void *arg) {

    while(1) {
        sem_wait(&sharedBuffer.empty);
        sem_wait(&sharedBuffer.mutex);
        sharedBuffer.buf[sharedBuffer.in] = "X";
        sharedBuffer.in = (sharedBuffer.in+1)%N;
        printf("Producer ");
        printf("%c", sharedBuffer.in);
        printf("\n");
        sem_post(&sharedBuffer.mutex);
        sem_post(&sharedBuffer.full);


    }

}

void *consumer(void *arg){

    while(1){
        sem_wait(&sharedBuffer.full);
        sem_wait(&sharedBuffer.mutex);
        sharedBuffer.buf[sharedBuffer.out] = NULL;
        sharedBuffer.out = (sharedBuffer.out+1)%N;
        printf("Consumer ");
        printf("%c", sharedBuffer.out);
        printf("\n");
        sem_post(&sharedBuffer.mutex);
        sem_post(&sharedBuffer.empty);

    }

}


int main(void) {

    sem_init(&sharedBuffer.mutex, 0, 0);
    sem_init(&sharedBuffer.full, 0, 0);
    sem_init(&sharedBuffer.empty, 0, N);

    pthread_t p1;
    pthread_t p2;
    pthread_t p3;
    pthread_t p4;

    pthread_t c1;
    pthread_t c2;
    pthread_t c3;
    pthread_t c4;

    // create four producer threads
    pthread_create(&p1,NULL,producer,NULL);
    pthread_create(&p2,NULL,producer,NULL);
    pthread_create(&p3,NULL,producer,NULL);
    pthread_create(&p4,NULL,producer,NULL);
    // create four consumer threads
    pthread_create(&c1,NULL,consumer,NULL);
    pthread_create(&c2,NULL,consumer,NULL);
    pthread_create(&c3,NULL,consumer,NULL);
    pthread_create(&c4,NULL,consumer,NULL);
}

1 个答案:

答案 0 :(得分:5)

添加-pthread参数以拉入所有用于链接的线程内容

g++ ./main.c -pthread