How to compile a C program that uses pthread.h?

Solution 1:

Use:

gcc MyProgram.c -o MyProgram -lpthread 

and dont forget to include the POSIX library in your code. It will compile your code.

Solution 2:

If you are going to compile a C program with pthread.h in LINUX using GCC or G++ you will have to use –lpthread option after the compile command.

gcc xyz.c -o xyz -lpthread

Here,

gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to create objcect file.
xyz is the name of object (binary) file.
-lpthread is an option for pthread.h

for more details here is the link conatining complete article on it.
Compiling C program with pthread.h in Linux.