Reputation: 808
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.
Upvotes: 0
Views: 989
Reputation: 7321
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
.
Upvotes: 1
Reputation: 16540
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.
Upvotes: 0