0

I saw from other posts on StackOverflow that the undefined reference error means that a definition is missing and that generally to fix it, files must be linked in compilation. But I am only compiling one file. I am getting this error for the functions pthread_detach and pthread_create.

/tmp/ccAET7bU.o: In function `sample_thread1':
foo.c:(.text+0x2a): undefined reference to `pthread_detach'

/tmp/ccAET7bU.o: In function `main':
foo.c:(.text+0x85): undefined reference to `pthread_create'

collect2: error: ld returned 1 exit status

The code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <signal.h>
#include <string.h>
#include <linux/unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <netinet/in.h>
#include <pthread.h>

pid_t gettid(void)
{
    return syscall(__NR_gettid);
}

void *sample_thread1(void *x_void_ptr)
{
    FILE *fp;
    int counter;

    pthread_detach(pthread_self());

    if((fp = fopen("thread_data.txt", "w")) == NULL)
    {
        printf("ERROR : Thread cannot open data file.\n");
        return;
    }   
    counter = 1;
    while(1);
    {
        fprintf(fp, "INFO : Thread1 id %d output message %10d\n",
                gettid(), counter);
        fflush(fp);
        counter++;
        sleep(1);
    }
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    int counter;
    pthread_t sample_thread_t1;

    if(pthread_create(&sample_thread_t1, NULL, sample_thread1, NULL))
    {
        fprintf(stderr, "Error creating thread\n");
        exit(-1);
    }
    system("clear");

    counter = 1;
    while(1)
    {
        printf("INFO : Main Process Counter = %d\n", counter);
        counter++;
        sleep(1);
    }
    return(0);
} 
alk
  • 69,737
  • 10
  • 105
  • 255
Luca
  • 515
  • 5
  • 17
  • 3
    I think you need to pass -lpthread to gcc to tell it to link against the pthread library. undefined reference just means the linker can't find the functions you called in the code – Einheri May 02 '15 at 16:49
  • Which OS are you running for this one? – alk May 02 '15 at 17:07
  • For Linux pease read here: http://stackoverflow.com/q/1662909/694576 – alk May 02 '15 at 17:07
  • Btw: I feel typos in titles are embarrassing ... – alk May 02 '15 at 18:07

1 Answers1

2

You have to link this with a pthread library using -lpthread or -pthread options of GCC or clang.

Examples:

gcc your_file.c -o program -lpthread
# or gcc your_file.c -o program -pthread
# the same for clang
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • `-lpthread` and `-pthread` are not necessarily the same. One should consult the implementations documentation for which one has to be used. For Linux for example the man-page (http://man7.org/linux/man-pages/man3/pthread_create.3.html) expliciltly states to use `-pthread`! – alk May 02 '15 at 17:05
  • @alk, three or more times I faced a problem with `-lpthread` on a jailbroken iPhone and used `-pthread` which worked perfectly well. That's why I suggested two options top try. – ForceBru May 02 '15 at 17:12
  • `-pthread` would always imply linking the PThreads library, which is all that `-lpthread` does, as opposed to `-pthread` which might do much more, like setting compile time "switches". – alk May 02 '15 at 17:15