【问题标题】:getting error in c program "undefined reference to gettid"在 c 程序中出现错误“对 gettid 的未定义引用”
【发布时间】:2014-02-12 07:36:58
【问题描述】:

这是我的线程子程序... 在这里,我创建了 4 个线程并将结构作为参数传递给线程子例程。

我正在尝试使用 getid() 函数打印线程 ID,

我收到错误提示 “未定义对 gettid() 的引用”。

我已经添加了必要的头文件...

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
        int start, stop;
        int* array; 
};

void* squarer(void* td) 
{
     struct ThreadData* data=(struct ThreadData*) td;

     int start=data->start;
     int stop=data->stop;
     int* array=data->array;
     int i;
     pid_t tid1;

     tid1 = gettid(); //error at this statement//`
     printf("tid : %d\n",tid1);

     for (i=start; i<stop; i++) {
         sleep(1);
         array[i]=i*i;
         printf("arr[%d] = [%d]\n",i,array[i]);
     } 
   return NULL;
}

int main(void) {
    int array[ARRAYSIZE];
    pthread_t thread[NUMTHREADS];
    struct ThreadData data[NUMTHREADS];
    int i;

    int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;

    for (i=0; i<NUMTHREADS; i++) {
            data[i].start=i*tasksPerThread;
            data[i].stop=(i+1)*tasksPerThread;
            data[i].array=array;
    }

    data[NUMTHREADS-1].stop=ARRAYSIZE;

    for (i=0; i<NUMTHREADS; i++) {
            pthread_create(&thread[i], NULL, squarer, &data[i]);
    }

    for (i=0; i<NUMTHREADS; i++) {
            pthread_join(thread[i], NULL);
    }

    for (i=0; i<ARRAYSIZE; i++) {
            printf("%d ", array[i]);
    }
    printf("\n");

    return 0;
}

【问题讨论】:

  • 如果你使用的是 libc,gettid() 是没有实现的。您需要自己创建它
  • @user1781290 感谢您的回答......!如何实现gettid函数
  • 我自己从未尝试过。但是根据下面的链接,您可以简单地(long int)syscall(224)。也许这对你有帮助ubuntuforums.org/showthread.php?t=345317
  • @user1781290 请不要在您的代码中硬编码系统调用 ID。在不同的 Linux 发行版上可能会有所不同。例如,我的(Red Hat 6)在 ID 186 上有 gettid。请改用 SYS_* 宏。
  • @user1781290 感谢您提供的信息,我检查了 syscall.h 文件并将系统调用 gettid 函数 ID 替换为 sys_gettid 代替使用 224 / 186我>。现在语句是 tid1 = syscall(SYS_gettid);.

标签: c undefined-reference


【解决方案1】:

试试

#include <unistd.h>
#include <sys/syscall.h>

#ifdef SYS_gettid
pid_t tid = syscall(SYS_gettid);
#else
#error "SYS_gettid unavailable on this system"
#endif

【讨论】:

  • 在带有gcc --version gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0 的Linux Ubuntu 18.04 上,我还必须在所有包含项之上添加#define _GNU_SOURCE,以便定义syscall()。我从本文档底部的示例中了解到这一点:man7.org/linux/man-pages/man2/syscall.2.html
【解决方案2】:

要粘贴的宏(比以前的答案有所改进):

#include <unistd.h>
#include <sys/syscall.h>

#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif

#define gettid() ((pid_t)syscall(SYS_gettid))

例子:

#include <unistd.h>
#include <sys/syscall.h>

#ifndef SYS_gettid
#error "SYS_gettid unavailable on this system"
#endif

#define gettid() ((pid_t)syscall(SYS_gettid))

#include <stdio.h>

void main()
{
        printf("tid is %d\n", gettid());
}

【讨论】:

  • 太棒了!最佳答案。在 Ubuntu 20.04 上使用 glibc 版本 2.31-0ubuntu9.2,如 apt show libc6 所示。
【解决方案3】:

我已按照 errro 上提供的建议并更正了来源 *以下是无错误代码 *

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
    int start, stop;
    int* array; 
};

void* squarer(void* td) 
{
 struct ThreadData* data=(struct ThreadData*) td;

 int start=data->start;
 int stop=data->stop;
 int* array=data->array;
 int i;
 pid_t tid1;

 tid1 = syscall(SYS_gettid); // here is the correct statement //
 printf("tid : %d\n",tid1);

 for (i=start; i<stop; i++) {
         sleep(1);
         array[i]=i*i;
         printf("arr[%d] = [%d]\n",i,array[i]);
 } 
 return NULL;
}

int main(void) {
    int array[ARRAYSIZE];
    pthread_t thread[NUMTHREADS];
    struct ThreadData data[NUMTHREADS];
    int i;

    int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;

    for (i=0; i<NUMTHREADS; i++) {
        data[i].start=i*tasksPerThread;
        data[i].stop=(i+1)*tasksPerThread;
        data[i].array=array;
    }

    data[NUMTHREADS-1].stop=ARRAYSIZE;

    for (i=0; i<NUMTHREADS; i++) {
            pthread_create(&thread[i], NULL, squarer, &data[i]);
    }

    for (i=0; i<NUMTHREADS; i++) {
            pthread_join(thread[i], NULL);
    }

    for (i=0; i<ARRAYSIZE; i++) {
            printf("%d ", array[i]);
    }
    printf("\n");

    return 0;
}

【讨论】:

    【解决方案4】:

    如果您使用 glibc 或 musl,请定义 _GNU_SOURCE 以使此符号由 unistd.h 定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      相关资源
      最近更新 更多