0
votes

I have a multi threaded program in C that was working well but was in one single main.cpp file.

I have moved the thread in another .cpp file and added it's signature, void* displayScreen(void*); , in the header. I include the header in my initial main.cpp file.

Compiling works but the linker returns an error when trying to call pthread_create(): undefined reference to `displayScreen(void*)'

It looks like it compiled displayScreen(void *) fine but does not know where to load it from. Is there a way for me to tell the linker where to find it or am I doing it wrong please?

Thank you very much.

1
Post the commands you used to compile and link or this can't be answered. - bmargulies
your error message makes no sense - linkers don't "call" functions. Did you remember to include both object files in the compiled target? - Alnitak

1 Answers

0
votes

Adding the signature alone lets you compile the main translation unit, but you still have to compile the implementation of the function separately and link the two:

main.cpp

void* displayScreen(void*);

int main()
{
  /* .... */
}

display.cpp

void* displayScreen(void*)
{
  /* implementation */
}

Compile:

g++ -O2 -o main.o main.cpp
g++ -O2 -o display.o display.cpp

Link:

g++ -o myprogram main.o display.o -lpthread -s