0 Pluspunkte 0 Minuspunkte

Ich versuche das folgende Programm mit gcc zu kompilieren.

#include <stdio.h>
#include <pthread.h>

void *test(void *arg) {
    int i = 0;
    
    while(i < 10000) {
        i++;
    }
    
    printf("\n-> %d <-\n", i);
    
    return NULL;
}

int main() {
    pthread_t thread;
    
    if (pthread_create(&thread, NULL, test, NULL)) {
        fprintf(stderr, "Fehler beim Erstellen des Threads\n");
        return 1;
    }
    
    // Warte auf das Beenden des Threads
    pthread_join(thread, NULL);
    
    return 0;
}

Dabei bekomme ich die Fehleranzeige

C:\Users\alex\AppData\Local\Temp\ccq3alcI.o:main.c:(.text+0x64): undefined reference to `pthread_create'
C:\Users\alex\AppData\Local\Temp\ccq3alcI.o:main.c:(.text+0xb3): undefined reference to `pthread_join'

Wie kann ich dieses Programm richtig kompilieren?

bezieht sich auf eine Antwort auf: Windows Thread erstellen in C
von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Du musst pthread mit dem -l Parameter verlinken.

gcc file.c -lpthread
von (706 Punkte)