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?
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.
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
-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.
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