17

When I tried to compile this program, it failed:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void *WriteNumbers(void *threadArg)
{
    int start, stop;

    start = atoi((char *)threadArg);
    stop = start + 10;

    while (start < stop)
    {
         printf("%d\n", start++);
         sleep(1);
    }
    return 0;
}

int main(int argc, char **argv)
{
        pthread_t thread1, thread2;

        // create the threads and start the printing
        pthread_create(&thread1, NULL, WriteNumbers, (void *)argv[1] );
        pthread_create(&thread2, NULL, WriteNumbers, (void *)argv[2]);

        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);

        return 0;
}

It gave me the following errors:

tmp/ccrW21s7.o: In function `main':
pthread.c:(.text+0x83): undefined reference to `pthread_create'
pthread.c:(.text+0xaa): undefined reference to `pthread_create'
pthread.c:(.text+0xbd): undefined reference to `pthread_join'
pthread.c:(.text+0xd0): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

Why does it give me these undefined reference errors even though I had included pthread.h, which declares these functions?

David Brigada
  • 594
  • 2
  • 10
Cold-Blooded
  • 420
  • 2
  • 8
  • 16
  • 1
    You've got other problems too: you aren't checking the value of `argc` but you are using the values of `argv`. Your `WriteNumbers` method does not return a value. – dreamlax Nov 08 '10 at 03:59
  • @dreamlax: I fixed the 'no return' problem; I didn't notice the argc/argv problem. – Jonathan Leffler Nov 08 '10 at 04:28

5 Answers5

25

You probably forgot to link with the Pthreads library (using -lpthread on the command line).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
21

Others have mentioned that you haven't linked with the pthread library using the -lpthread flag. Modern GCC (not sure how modern, mine is 4.3.3) allows you to use just -pthread. From the man page:

-pthread
Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Actually, it's "old" gcc which allows that. The -pthread is very old syntax, and provided only for compatibility with older compilers that didn't have -l support... – Wouter Verhelst May 29 '15 at 14:24
  • 1
    @WouterVerhelst I don't think that's true. `-pthread` is still the recommended flag to use, because it also sets any required preprocessor and compilation flags, whereas `-lpthread` only directs the linker to link to libpthread. – Jonathan Wakely May 14 '16 at 12:08
4

You need to link pthread library to your binary, like this:

cc -o myapp myapp.c -lpthread
qrdl
  • 34,062
  • 14
  • 56
  • 86
3

Do

gcc -pthread -o name filename.c (cpp)

to compile the program, then

./name

to run the program.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
0

For folks looking for the csapp solution. Compile "csapp.c" first, then

gcc -o filename filename.c csapp.o -lpthread
Shashanoid
  • 33
  • 4