Pramod
Pramod

Reputation: 5208

Problem in compiling C function with threading on Fedora

When I try to compile a C-program with multithreading in Fedora, I get the following error.

The file name is abc.c

abc.c:(.text+0x39): undefined reference to `pthread_create'
abc.c:(.text+0x61): undefined reference to `pthread_create'
abc.c:(.text+0x79): undefined reference to `pthread_join'
abc.c:(.text+0x8d): undefined reference to `pthread_join'

I checked in /usr/include and I found that pthread.h is present. Also I tried copying pthread.h to the same directory as abc.c

How do I resolve these linking errors?

Upvotes: 0

Views: 773

Answers (1)

Jesus Ramos
Jesus Ramos

Reputation: 23266

As pointed out by George you must link with the thread library gcc -o abc abc.c -pthread The reason you are getting those errors is because during the linking stage the compiler tries to fill in all the slots where it had placed placeholders for method calls that it knew were defined but currently did not know their locations because the appropriate library had not been linked yet. As pointed out by caf using the -pthread flag in both compiling and linking stages allows for the compiler to make smarter choices about what it needs to use to be thread-safe in certain conditions.

Upvotes: 3

Related Questions