I'm a beginner and I'm new to Ubuntu. I just installed it and want to run a C program. I have no idea what platform to use or where to write the code. I need to use pthread.h header file in the program. Can anyone help me?



gcc MyProgram.c -o MyProgram -lpthread 

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

|improve this answer|||||
  • 2
    by default GCC does not include the pthread library. so you have to include the library using lpthread argument. – Parthiv Shah Apr 5 '14 at 9:30
  • Also note that in gcc-4.8 there is not -lpthread argument in man gcc. But there is a -pthread argument. Both work fine on Ubuntu 14.04 with gcc-4.8. – Elijah Lynn Aug 19 '16 at 0:07

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


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.

|improve this answer|||||

First thing you'll need in Ubuntu to compile C/C++ programs is installing GCC (Gnu Compiler Collection) which is part of build-essential package , do that by running:

sudo apt-get install build-essential

Then you can test if you have it installed by running gcc. If you you see error like Fatal error: file not provided (not sure exact error message, but should be something similar), that means you have compiler ready.

And for editing your Code, you can use already available Gedit, just search for it in Dash.

Now following is the syntax to compile your C source file, run following where your file is:

gcc MyProgram.c -o MyProgram

Where, switch -o is optional, but provided to mention name of Binary file which should be created out of your source.

Then simply run ./MyProgram to run your binary.

Note that pthread.h as you mentioned (POSIX Thread) should be available by default with GCC, so simply including it in your C file will do that job, in case it's not available, a simple Google search should help. ;)


Too long, didn't read? check this. :D

|improve this answer|||||

If it gives error than you can try out same command by interchanging the parameters as like

gcc -lpthread -o output_file program_pthread.c

after that ./output_file provides output for program and here program_pthread.c is the pthread program you have implemented.

|improve this answer|||||

For C++ program:

g++ -std=c++11 Myprogram.cpp -o MyProgramTest -lpthread
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.