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?

7
  • 1
    pass the -lpthread option to your compiler Commented Mar 2, 2017 at 15:00
  • 3
    Actually you should pass -pthread to your gcc compiler, and then the -lpthreadlibrary would be linked Commented Mar 2, 2017 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. Commented Mar 2, 2017 at 15:09
  • 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 (minimal reproducible example) for your new problem. Commented Mar 2, 2017 at 15:21
  • 1
    Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it? Commented Mar 2, 2017 at 15:33

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.