getting error in c program "undefined reference to gettid"
33,621
Solution 1
Try
#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
Solution 2
Macro to be pasted (improved over previous answer):
#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))
Example:
#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());
}
Solution 3
If you're using glibc or musl, define _GNU_SOURCE
to have this symbol defined by unistd.h.
Solution 4
I have followed the suggestions provided on errro and corrected the source *below is the error free code *
#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;
}
Related videos on Youtube
Author by
PravinY
Updated on April 07, 2021Comments
-
PravinY about 3 years
This is my thread sub routine... Here, I am creating 4 threads and passing structure as a argument to thread sub routine.
I am trying to print thread id with
getid()
function,I am getting error saying "undefined reference to gettid()".
I have added necessary header files...
#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; }
-
user1781290 over 10 yearsIf you are using libc,
gettid()
is not implemented. You need to create it yourself -
PravinY over 10 years@user1781290 Thanks for the answer....! how to implement gettid function
-
user1781290 over 10 yearsNever tried it myself. But according to the link below, you can simply
(long int)syscall(224)
. Maybe that helps you ubuntuforums.org/showthread.php?t=345317 -
Sergey L. over 10 years@user1781290 Do not hardcode syscall ids in your code please. It can be different on different Linux distros. Mine (Red Hat 6) for instance has
gettid
on ID 186. Use theSYS_*
Macros instead. -
PravinY over 10 years@user1781290 thanks for the information, I have checked the syscall.h file and replaced the syscall gettid function id with sys_gettid instade of using 224 / 186. now the statement is tid1 = syscall(SYS_gettid);.
-
EdH over 8 yearssyscall numbers are definitely different on different archs. be aware - use SYS_gettid instead.
-
-
Gabriel Staples almost 3 yearsAwesome! Best answer. Works on Ubuntu 20.04 with glibc Version 2.31-0ubuntu9.2, as shown by
apt show libc6
. -
Gabriel Staples over 2 yearsOn Linux Ubuntu 18.04 with
gcc --version
gcc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
I had to also add#define _GNU_SOURCE
above all of the includes in order forsyscall()
to be defined. I learned that from the example at the bottom of this documentation here: man7.org/linux/man-pages/man2/syscall.2.html.