Pthreads 和未定义的引用

发布于 11-18 15:34 字数 2460 浏览 3 评论 0原文

我正在接近 C 中的 pthreads 库,我写下了一些虚拟代码以便熟悉,但是当我尝试编译此代码时,我得到了对 pthread_create 的未定义引用,即使我已经包含了正确的库和数量传递给函数的参数是正确的。你能帮我一下吗?

我的代码是:

#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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念2024-11-25 15:34:01

使用 -pthread 编译(错误链接)

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

Compile (err link) with -pthread

gcc -Wall -Wextra -o prog prog.c -pthread
甜是你2024-11-25 15:34:01

在 Eclipse 中将“pthread”添加到 GCC C 链接器库。

项目->属性-> C/C++ 构建 ->设置->工具设置-> GCC 链接器 ->图书馆 ->库 (-l) ->添加-> “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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文