3

I am trying to run this C program using gcc -Wall -std=c99 hilo.c - ./a.out hilo.c and I am getting this error message:

hilo.c: In function ‘func’:
hilo.c:6:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘pthread_t’ [-Wformat]
hilo.c: In function ‘main’:
hilo.c:14:3: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void)’
hilo.c:15:3: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void)’
hilo.c:24:3: warning: statement with no effect [-Wunused-value]
/tmp/cchmI5wr.o: In function `main':
hilo.c:(.text+0x52): undefined reference to `pthread_create'
hilo.c:(.text+0x77): undefined reference to `pthread_create'
hilo.c:(.text+0x97): undefined reference to `pthread_join'
hilo.c:(.text+0xab): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

No idea what's wrong with the code so if anyone could help me would be it would be appreciated.

This is the code:

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

void func(void){

         printf("thread %d\n", pthread_self());
         pthread_exit(0);

}

   int main(void){

        pthread_t hilo1, hilo2;

        pthread_create(&hilo1,NULL, func, NULL);
        pthread_create(&hilo2,NULL, func, NULL);

        printf("the main thread continues with its execution\n");

        pthread_join(hilo1,NULL);
        pthread_join(hilo2, NULL);

        printf("the main thread finished");

        scanf;

  return(0);

}
Mestica
  • 1,489
  • 4
  • 23
  • 33
user2188946
  • 35
  • 1
  • 1
  • 4
  • 1
    @MichaelBurr: It's unfortunate, but I don't want to mark as a duplicate if the other question has an incorrect answer accepted. – Dietrich Epp Mar 24 '13 at 00:10
  • @Dietrich: It's too bad that there's not some sort of community/moderator/whatever override for accepted answers on SO (I guess one could argue that the number of votes should act as such). We have yet to see if the correct answer gets accepted here. – Michael Burr Mar 24 '13 at 00:16

3 Answers3

8

You should compile and link with -pthread.

gcc -Wall -std=c99 hilo.c -pthread

It is not sufficient to use -lpthread. The -pthread flag will change how some libc functions work, in order to make them work correctly in a multithreaded environment.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    Your answer depends upon the platform. On *some*, -pthread is necessary, even though I suspect in this case you are correct. It's just not a given that it is that way on every system with pthreads support. – Randy Howard Mar 24 '13 at 23:37
5

You haven't linked the pthread library. Compile with:

gcc -Wall -std=c99 hilo.c -lpthread
P.P
  • 117,907
  • 20
  • 175
  • 238
  • I should also add that this is a POSIX way to link *any* library (-l) in general. While in case of pthreads, it's a case of compiler and libc dependent i.e. when `-pthread` is used gcc sets additional options/switches which may not be set with `-lpthread` (and could even lead to incorrect functoning of libc). This is heavily gcc specific. The more general answer is: use `-pthread` whenever available. If not, use `-lpthread` *and* make sure you set any other options/compiler switches necessary for your platform/compiler's correct functioning by reading it's documentation. – P.P Sep 10 '14 at 07:31
2

Change

void func(void)

to

void* func(void *)

and compile with

gcc hilo.c -pthread

You will only have errors in printing pthread_self() because it is not an int.

Mestica
  • 1,489
  • 4
  • 23
  • 33