In my main() method in one source file, I have a call to pthread_create(), as follows:
pthread_t pth_chanrecv;
pthread_create(&pth_chanrecv, NULL, chanrecv_run, NULL);
“chanrecv_run” is a function in another source file that I have written.
This is the function prototype:
void *chanrecv_run(void *arg);
The function prototype is in a header file which I have included in the source file containing the main() method.
I’m compiling my program with the following statement:
gcc -lpthread -o bin/main2 src/main2.c
The chanrecv_run() function is in a file named “chanrecv.c”, also in the src folder.
Every time I try and compile it, I get the following error:
/tmp/ccHxRSTk.o: In function `main':
main2.c:(.text+0x256): undefined reference to `chanrecv_run'
collect2: ld returned 1 exit status
I have no idea what’s going wrong. I’ve scoured the web trying to find another instance of someone trying to call a function that was in another file with the pthread_create function and come up completely blank. Is this even possible, and if so, how?
The issue isn’t pthreads related. Since you’ve not linked your second .c file containing the
chanrecv_runfunction the linker is moaning.You need: