简体   繁体   中英

Pthreads and undefined reference

I'm approaching pthreads library in C, I have written down some dummy code in order to get acquainted, but as I try to compile this code, I get an undefined reference to pthread_create, even if I have included the proper library and the number of parameters passed to the function is correct. Can you please help me out?

My code is:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

void *dummy_thread(void *arg)
{
    pthread_exit(NULL);
}

void *dummy_fork(void *arg)
{    
    exit(0);
}

void pthread_perror(char *string, int errcode)
{
    char errmsg[256];
    strerror_r(errcode, errmsg, 256);
    printf("%s:%s\n", string, errmsg);
}

int main(int argc, char** argv) 
{
    pthread_t *threads;
    int rc, nt, *pids;
    long threads_microsecs, fork_microsecs, t;
    struct timeval ora, dopo;
    float perc;

    if (argc != 2)
    {
        printf("Numero di argomenti %d non valido.\n", argc);
        exit(1);
    }
    nt = strtol(argv[1], NULL, 10);
    if (nt < 0)
    {
        printf("Numero di thread non valido: %d", nt);
        exit(1);
    }

    threads = (pthread_t *) malloc (nt * sizeof(pthread_t));
    pids = (int *) malloc (nt * sizeof(int));

    if (pids == NULL)
    {
        perror("malloc");
        exit(1);
    }

    gettimeofday(&ora, NULL);

    for (t = 0; t < nt; t++)
    {
        if (rc = pthread_create(&threads[t], NULL, dummy_thread, NULL)) 
        {
            pthread_perror("pthread_create", rc);
            exit(1);
        }
        gettimeofday(&dopo, NULL);
        threads_microsecs = dopo.tv_usec - ora.tv_usec;
        threads_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
        printf("Tempo per la creazione dei thread %ld nsec.\n", threads_microsecs);
        gettimeofday(&ora, NULL);

        for (t = 0; t < nt; t++)
        {
            if ((pids[t] = fork()) < 0)
            {
                perror("fork");
                exit(1);
            }
            if (pids[t] == 0)
            {
                dummy_fork(NULL);
            } 
        }

         gettimeofday(&dopo, NULL);
         fork_microsecs = dopo.tv_usec - ora.tv_usec;
         fork_microsecs += 1000000 * (dopo.tv_sec - ora.tv_sec);
         printf("Tempo per la creazione dei processi %ld nsec", fork_microsecs);
         perc = 100 * (((float) fork_microsecs - threads_microsecs)/(float)fork_microsecs);
         printf("(%.2f%%)\n", perc);


    }
    return (EXIT_SUCCESS);
}

Compile (err link) with -pthread

gcc -Wall -Wextra -o prog prog.c -pthread

In eclipse add "pthread" to GCC C Linker Libraries.

Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC Linker -> Libraries -> Libraries (-l) -> Add -> "pthread"

After this Build your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM