0

I am trying to make sense of a program:

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

int main(int argc, char** argv) {

volatile double fShared = 0.0;
// create pthread structures - one for each thread
pthread_t t0;
pthread_t t1;
//Arbitrary pointer variables - void* is a pointer to anything
void *thread_res0;
void *thread_res1;
int res0,res1;
//create a new thread AND run each function
// we pass the ADDRESS of the thread structure as the first argument 
// we pass a function name as the third argument - this is known as a FUNCTION pointer 
// you might want to look at the man pages for pthread_create 
 res0 = pthread_create(&t0, NULL, do_this, (void*)"");
 res1 = pthread_create(&t1, NULL, do_that, (void*)"");
// Two threads are now running independently of each other and this main function
// the function main is said to run in the main thread, so we are actually running 

// The main code can now continue while the threads run - so we can simply block
 res0 = pthread_join(t0, &thread_res0);
 res1 = pthread_join(t1, &thread_res1);

 printf ("\n\nFinal result is fShared = %f\n",fShared);
 return (EXIT_SUCCESS);
} 

It should be noted that do_this and do_that are functions created earlier in the program. I am getting the following errors:

seed@ubuntu:~/Programs$ Homework2OS.c
/tmp/cceiRtg8.O: in function 'main'
Undefined reference to 'pthread_create' 
Undefined reference to 'pthread_create'
Undefined reference to 'pthread_join' 
Undefined reference to 'pthread_join' 

We were given this bit of code to fix. I have found elsewhere the format for the pthread_create constructor. Do I need to actually define it above the main? I was under the impression it was imported with the library.

I would also venture to guess that it has something to do with the fourth parameter? I understand that the first parameter is the location of the thread (defined above), the second is NULLed, the third is the function call but I do not quite understand what the fourth parameter is supposed to be.

What's wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Show your compilation command, and your `#include` directives. Read some [pthread tutorial](https://computing.llnl.gov/tutorials/pthreads/). What is your operating system? Read [pthread_create(3)](http://man7.org/linux/man-pages/man3/pthread_create.3.html) – Basile Starynkevitch Mar 02 '17 at 14:58
  • 1
    pass the `-lpthread` option to your compiler – StoryTeller - Unslander Monica Mar 02 '17 at 15:00
  • 3
    Actually you should pass `-pthread` to your `gcc` compiler, and then the `-lpthread`library would be linked – Basile Starynkevitch Mar 02 '17 at 15:03
  • 1
    @BasileStarynkevitch - As well as certain appropriate preprocessor flags defined, no? So yes, better use the `-pthread` option if it's supported on your system. – StoryTeller - Unslander Monica Mar 02 '17 at 15:09
  • Alright. I used the compiler command gcc -pthread Homework2OS.c and didn't get any errors, but I also didn't get any printed output. gcc -pthread -o Homework Homework2OS.c gives me a file but it's all garbage – Thomas Sullivan Mar 02 '17 at 15:14
  • 3
    Getting garbage is a separate problem from not being able to link your program. Close this question (delete it). Create a new one with an MCVE ([MCVE]) for your new problem. – Jonathan Leffler Mar 02 '17 at 15:21
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Werner Henze Mar 02 '17 at 15:33

1 Answers1

1

all the code imports is the header for the pthread library. (pthread.h)

What is missing is the linker needs to actually include the library for gcc the parameter -pthread is needed (AT THE END of the list of parameters to gcc.)

It needs to be at the end because the linker processes parameters in the order given on the command line and if the object files are not already processed, then there will not be any unresolved references for the linker to try to resolve via browsing the pthread library.

I.E. this is wrong:

gcc -g -o myprogram -lpthread myprogram.o

This is correct:

gcc -g -o myprogram myprogram.o -lpthread
user3629249
  • 16,402
  • 1
  • 16
  • 17