0

i am trying to compile this code in C. I am using

 gcc -o -pthread test1.c

I am using Linux. Unfortunately I get:

undefined reference to `pthread_create'
 undefined reference to `thread_join'
  collect2: error: ld returned 1 exit status

When I try to compile:

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


    void* thread_func(void* parameter){

        int i =0;
        for(i=0; i<5;i++)
        {
            printf("%d\n", i);
        }

      return 0;


    }


 int main (int argc, char *argv)
 {
     pthread_t thread_handle;
     int ret = pthread_create(&thread_handle,0,thread_func,0);
     if(ret != 0) {
        printf("Create thread failed! error: %d",ret);
        return 1;
      }

      thread_join(thread_handle,0);

      return 0;

}

When I use the second solution from here: Undefined reference to pthread_create in Linux

I get another error. I am in the directory where my test1.c .

no input files





ADDIN ERRORS:
1)using gcc -o someFile -pthread test1.c
-undefined reference to `thread_join'
2)gcc -o -pthread test1.c
-undefined reference to `thread_join'
-undefined reference to `pthread_create'

3)gcc test1.c -o test1.o -I. -lpthread 
- undefined reference to `thread_join'
4)gcc -pthread -o test.c
- no input files

I would be grateful for help because I can't learn threads because I can't compile it.

Community
  • 1
  • 1
Dago
  • 788
  • 2
  • 9
  • 24

3 Answers3

1

Word after option -o is the output file, i.e. you specified -pthread to be the output file. Change to gcc -o someFile -pthread test1.c should do the trick. Your executable output file would then be someFile.

geert3
  • 7,086
  • 1
  • 33
  • 49
  • `gcc -o someFile` (without the `-`, `someFile` being your output file name) – geert3 Nov 25 '14 at 19:22
  • To run the output you would run `someFile`. – geert3 Nov 25 '14 at 19:26
  • @SzymonSutek: Could you add to your question the other commands that you have tried and what error they have produced? – Bill Lynch Nov 25 '14 at 19:37
  • OK missed it first time. So in addition to the main answer above, you need `pthread_join` in your .c source, not `thread_join`. All pthread calls start with a `p`. – geert3 Nov 25 '14 at 20:04
0

try compiling with -lpthread

gcc -lpthread test1.c -o test1
antonpuz
  • 3,256
  • 4
  • 25
  • 48
0

The linker takes things in the order written on the command line.

So, in your example, when the linker gets to your .o file, and needs to find the external reference for the pthread calls, it has nothing to look in.

You could try:

gcc test1.c -o test1.o -I. -lpthread 

The -I. says to look for #include "header.h" files in the current directory the -lpthread says to use the libpthread.a library file.

Mat
  • 202,337
  • 40
  • 393
  • 406
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • 1
    `-lpthread` is bad advice. Always use `-pthread` unless you know _exactly_ what you're doing. `-pthread` is not only a linker flag, it's a compiler flag too. – Mat Nov 25 '14 at 19:38
  • also `gcc` without `-c` compiles to executable, not to object file. – geert3 Nov 25 '14 at 19:48