Howto Compiling C program and creating executable file under Linux / UNIX / *BSD

by on March 14, 2006 · 69 comments· LAST UPDATED October 27, 2009

in , ,

How do I compile C program and create an executable file under Linux or UNIX operating systems?

You need GNU project C and C++ compiler for compiling C program and creating an executable file. Most Unix and Linux (*BSD) user start compiling their C program by the name cc. But you can use gcc command to compile program. First make sure you have gcc installed:

Make Sure Compiler Is Installed

Type the following command to verify that gcc is installed:
which gcc
Output:

/usr/bin/gcc

Find out version of gcc:
$ gcc --version
Output:

gcc (GCC) 4.0.3 20060212 (prerelease) (Debian 4.0.2-9)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

To compile C program you need to use syntax as follows:
gcc program.c -o program-output

Writing Your First C Program Under Linux / UNIX

Use a text editor such as vi or gedit to create a C program called first.c:
$ vi first.c
Type the following lines (program):

#include <stdio.h>
int main(void){
printf("My first C program\n");
return 0;
}

How Do I Compile My Program?

To compile C program first.c, and create an executable file called first, enter:
$ gcc first.c -o first
OR
$ cc first.c -o first

To execute program first, enter:
$ ./first
Output:

My first C program

However, both FreeBSD and Linux support direct make (GNU make utility to maintain groups of programs) command on C program without writing a make file. Remove, first program using the rm command:
$ rm first
$ make first

Output:

cc   first.o   -o first

Execute program first:
$ ./first
Please note that above hack works with GNU make only.

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 69 comments… read them below or add one }

1 Mps March 21, 2006 at 7:43 pm

Hello,

Your c code is missing the filename of the #include directive.

Reply

2 nixcraft March 21, 2006 at 10:50 pm

Thanks for heads up.

Reply

3 Edan March 8, 2007 at 11:14 am

Hi, I was just wondering how would you create an executable for numerous C programs? I am modifying the Windows Frotz interpreter for a college assignment with the aim to output the game data to a html page. The source for the interpreter contains numerous C files in a number of subdirectories. I have made the modifications but need to create the executable to see if it works properly. This is my first time creating an executable so i’m a bit unsure how to go about it. Any help would be greatly appreciated.

Reply

4 conrad sobol June 11, 2007 at 10:04 am

I just want to thank you. I am 57 years old and I just started computing two years ago. I have built three towers, which is no big deal; but, I have been running a lot of different systems. I want to learn c and c++ and you have helped me a lot. I am grateful for any help I can get. I usually do stuff on my own.I still wonder what the greatest difference is between BSD and Debian other than software installation. I do think that BSD is closer to being pure UNIX than Debian, but the kernels are so alike or an I wrong? It world be nice to know. Anyway, this letter is to thank you.

Reply

5 nixCraft June 11, 2007 at 5:42 pm

Nice to see at 57, you are learning BSD and C/C++ development.

*BSD is under BSD vs Linux is under GPL License
Network stack

Little change in command syntax for few utilities

A BSD and Linux kernel are different and follows different development methods and approach.

Please see this post along with discussion for more info.

Reply

6 haneefa June 22, 2007 at 10:26 am

i want a C program Compiler Code In VI editor

Reply

7 king777 January 22, 2008 at 5:51 pm

hi, i ve installed red hat linux.and trying to compile C++ prg.but getting a err msg saying that gcc not found.then i tried to see which compiler installed on my linx.To do so i am giving cmd – which
gcc – output is /usr/bin/which: – no gcc.

and i am not able to compile my prog..

plz help me in dis regard.

Reply

8 EdsonP(nenao) November 19, 2011 at 1:09 pm

You need to install install update your repositories, and install it thru the RPM comander or do it with de packgesmanager.

bye

Reply

9 gaurav March 27, 2012 at 1:55 pm

install g++ . in redhat and centos or fedora yum install g++

Reply

10 anonymous October 10, 2008 at 5:21 pm

king777 – you need to make sure the gcc development package is selected when you install redhat. Otherwise, you have to download/install gcc manually.

Reply

11 liju February 21, 2009 at 4:16 am

i don’t know about software creation iam from a village school of india
shall you pls send any program in c for me iam too interest in these

Reply

12 Zashk March 29, 2009 at 12:30 pm

If using the Unix system

Follow the below steps:
$ vi welcome.c
Save the content written below (This is called programme)

include
int main(void)
{
printf(“WELCOME TO THE WORLD OF C\n”);
return 0;
}

Step 3 ) comiple the progamme
gcc welcome.c -o WELCOME

Step 4) For execution
$ ./WELCOME

Reply

13 diptesh maity January 31, 2012 at 10:29 am

good answer………………

Reply

14 DarkBattM14 May 26, 2009 at 5:14 am

THNX!!! very useful!!!! :D :D :D

Reply

15 Omar August 24, 2009 at 7:16 am

After typing the following command “$ which gcc” to verify that gcc is installed, the Output was “/usr/bin/which: no gcc in (/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/omar/bin)
” , instead of being “/usr/bin/gcc”. what i have to do now?

Reply

16 mominmyheart December 2, 2009 at 3:35 pm

I would like to thank you very much. I am new user of linux and C language, I was trying to compile a small program for hours. You made it so easy thank you again and god bless you.

Reply

17 tom_g December 26, 2009 at 8:01 pm

Many thanks for this! I love programming in C and perl and getting my feet wet with Linux/UNIX.

As a followup question, how would I take a program I’ve created (a calculator, for example), and package it so it can run as an executable on another computer (or even embed in a linux distro)?

Thanks again.

Reply

18 solomon komba February 3, 2010 at 11:52 am

i want to know how to create executable file.

Reply

19 badshah November 13, 2010 at 7:05 pm

when u compile any *.c file automatically the executable file is created, which is linked to ld further to get the output,the executable file created after compiling is called as intermediate object file or executable

Reply

20 EdsonP(nenao) November 19, 2011 at 1:13 pm

I file con be executable thru the permitions
chmod +x program make in a sudo way

edpes

Reply

21 nits gup February 20, 2010 at 12:05 pm

how to compile a user defined function in c using gcc.
what do you mean by segmentation fault.

Reply

22 Mojtaba_Sky April 13, 2010 at 5:38 pm

Hi
i’m new linux worker.
befor linux i work on windows7 and now for robotic programing and… work in linux fedora12.
thank u for this note.
and i have a question!(excuse me for my bad english,becous i’m iranian)
–> how we can load and open the serial port in program??
–>how can i open the camera on linux?
–>and how we can comunicate with other laptop with wi-fi?

so thank a lot.

Reply

23 Jhuma Basu April 15, 2010 at 1:08 pm

I followed the steps mentioned above.I m able to compile the code.But when i m givin “./first” to
run it its giving the following “-bash: ./first: Permission denied”. Please help me

Reply

24 MeierSom October 26, 2012 at 8:03 am

Usually the compiled programm. If that’s not the case run
sudo chmod u+x
which gives the user permission to execute the file.

Reply

25 cool18 May 17, 2010 at 3:45 pm

hii…iam on redhat 5.0….whenever i try to compile c program…iam gettin an error msg stating gcc command not found….explain me the easy way to install the gcc….please help me

Reply

26 cool18 May 18, 2010 at 3:37 am

i tried which gcc as you said but iam getting gcc not in and ./a.out is also not working…please someone help me out

Reply

27 Gagan August 6, 2010 at 2:11 am

HI! I’m new for linux and I’m using Fedora 13 actually I wnt to install compiler for c and c++ someone told me gcc is inbuilt in fedora 13, is it possible tell me how to know it’s installed or not or how to install it? how to create c or c++ programs in fedora 13 or any linux system, how to install other IDE’s for development. Please someone help me out……

Reply

28 Anjan August 20, 2010 at 4:53 am

Hi,

I would like to know how can I execute the file “first” without using preceeding it
with the characters “./” . Any help is appreciated.

Regards

Reply

29 Manoj August 21, 2010 at 12:30 am

when i try to compile a C pogram in centos 5.2…i get the following error
$ cc prog.c
bash: cc: command not found

how to get rid of this prob…pls help

Reply

30 sagar sukumar August 31, 2010 at 3:52 am

actually it is because gcc is not installed so type “yum install gcc” if u have a centos or fedora

Reply

31 er.prabintripathi@gmail.com September 1, 2010 at 6:25 am

i tried it as su -c’yum install gcc’
but it also get faliure.to install the gcc so hw could i hv 2 go for install.

Reply

32 amit November 10, 2010 at 9:14 am

yum only works if you have a yum repository… otherwise you have to do it manually..

Reply

33 Prabin kumar tripathi August 29, 2010 at 1:51 pm

when i try to compile a C pogram in Fedora 13 .i get the following error
$ cc prog.c
bash: cc: command not found
how to get rid of this prob…pls help

Reply

34 Koushik Khan August 19, 2011 at 7:32 am

Dear friend,
I cannot also create,compile & execute C program in Fedora 15.Please help me to do this mailing me the required commands.

Reply

35 noton September 4, 2010 at 3:36 am

I can’t not compile program

Reply

36 PsykoNerd September 18, 2010 at 1:48 pm

TopDollar !! :)
My first c-compilation yummie!
And it works perfect following your instructions.
Peace out!

Reply

37 pradeep November 19, 2010 at 10:20 am

hi friends can any one say me how can i add libraries(.h) to my gcc compiler

Reply

38 LostInCyberspace January 15, 2011 at 4:10 am

This really doesn’t help me to understand how to make the compiled program executable. When I say executable I mean a *.exe file that I can call by name (such as typing “first_program.exe” in a terminal & having it execute), or by double clicking in a graphical environment.

Does ANYBODY know how to do THAT?!

Reply

39 Max February 4, 2011 at 4:14 pm

Can I use script on Windows XP??

Reply

40 Tomahawk2001913 June 14, 2013 at 2:58 am

If we have a program with multible *.c or *.h files how do we compile those together? And how do we make this a *.exe file or some sort of exacutable?

Reply

41 raaz February 15, 2011 at 6:15 pm

i just want to know how to install gcc files in fedora???????????/

Reply

42 Aamir Adnan Rind February 17, 2011 at 1:08 pm

use the following command in terminal (it works for me in fedora 14)
yum groupinstall ‘Development Tools’

This will download (approx 105Mb) of data and automatically install it on your system. the best thing is you dont have to worry about the dependencies ;-)
then type gcc and press enter
you should get this error “no input files selected” this means gcc is installed.

Reply

43 Nivi March 13, 2011 at 9:16 am

i tried but it says that you need to be root to perform this command. now what should i do?

Reply

44 ramya April 26, 2011 at 9:25 am

thanks you

Reply

45 Mostafa Hoseinimehr June 3, 2011 at 9:31 pm

in Ubuntu installed gcc by default.
thanks a lot for this.

Reply

46 balu June 27, 2011 at 5:21 am

i want how to understand linux progams.

Reply

47 meenakshi sharma July 3, 2011 at 1:48 am

sir ,
I want to know is c has executable files?if yes then can a virus infect c?

Reply

48 meenakshi sharma July 3, 2011 at 1:51 am

Sir,
There is a problem when i write a command in MS-Word like
=rand(200,99)
then some text appears.I m not able to understand the internal working for this command.Can you explain please?

Reply

49 kaleeswari August 16, 2011 at 11:12 am

I want to execute the shell program in ubunto os . but, the vi editor didnot support the delete operation . how to delete the mistake lines in vi editor .

Reply

50 sahil August 17, 2011 at 1:48 pm

i want to know that

if i made my own header file…………then where to place that header file to use it.

or there is any change in compiling the file.

Reply

51 aaa August 26, 2011 at 2:57 am

when I try compile a C program and create an executable file , I fail and get this massage : Access is denied .
I do not know what does mean
I want help

Reply

52 Arman September 23, 2011 at 6:24 pm

Hi, Thanks a lot

Reply

53 arif November 5, 2011 at 6:25 pm

write a c under the unix/linux environment to do the following:create two concurrent processes A and B, with a shared memory segment M attached to the address spaces of those two processes,process A generates 100 pseudo-random integer numbers, and stores them in M,process B sorts the numbers in M by any sorting algorithm,process a displays the sorted list in M.

Reply

54 EdsonP(nenao) November 19, 2011 at 1:04 pm

Nice !!! very clear, thanks…

I love LINUX>> yahhhhhhhh

Reply

55 Baba Saleem November 24, 2011 at 7:26 am

Lots of thanks to people who have provided solution for professionals.
I love Ubuntu 10.04 LTS.
Everything Amazing and easy.

Reply

56 saurabh May 21, 2012 at 1:48 pm

i use ubuntu 11.10 and why it is not working
sumit@saurabh:~$ gcc –version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

sumit@saurabh:~$ $ vi first.c
$: command not found
sumit@saurabh:~$ gedit first.c
sumit@saurabh:~$ $ gcc first.c -o first
$: command not found
sumit@saurabh:~$ $ cc first.c -o first
$: command not found
sumit@saurabh:~$ $ gcc first.c -o first
$: command not found

Reply

57 Mayank July 1, 2012 at 12:47 pm

@ saurabh…
Bro…u must remove the ‘$’ sign before the commands.
instead of $ vi first.c it shud be vi fisrt.c
and same for rest of commands.
shud work…else do below
whereis gcc
It will give u path of gcc executable like /usr/bin/gcc
then try as below:
/usr/bin/gcc first.c -o first
this wud work..
-Mayank

Reply

58 saurabh August 10, 2012 at 3:59 pm

thanks…Mayank bhai

Reply

59 neha August 6, 2012 at 9:29 am

your ans are really very helpfull thanx

Reply

60 Anil Sharma September 1, 2012 at 7:52 am

Hii ,
I am trying to run the same C program as mentioned at the top of this page. But it is saying that commant not found or directory no found. Can you please help me in this.

Thanks and Regards,
Anil

Reply

61 abdulla ragif ali mohammed mustafa karim achmed September 1, 2012 at 8:41 pm

goodhoallo i are to write in terminel but no working for avover the coder. plis help me i is not geting work fix to be……..,,,mngrw

hello

Reply

62 pesho September 9, 2012 at 8:51 am

i dont get why u guys goes thru all that pain with linux :D
i can do all this for 200 % faster then any of u on my windows
threat closed !

Reply

63 Taufique October 14, 2012 at 8:08 pm

hey,Anil sharma!
first of all ,where have you saved *.c file,keep in mind this location.Next, open your terminal(ctrl+t) and type cd location and press enter.Now compile your c file using this command line gcc filename.c .Now a.out file will create where you have saved filename.c file.For run this filename.c type ./a.out in the terminal and this will give you output of your filename.c file.

Reply

64 kanchan October 15, 2012 at 6:32 am

i’m a newbie to linux,kindly help me complile this c++ code.I got this error while trin to compile the same.

root@ubuntu:/home/kanchan# gcc codel.cc -o codel
codel.cc:37:20: fatal error: config.h: No such file or directory
compilation terminated.

Reply

65 Amir February 5, 2013 at 8:59 pm

Thanks

Reply

66 Tanvi March 5, 2013 at 6:31 pm

M using red hat linux nd it contain a gcc but der is advance version of gcc…………..
i cant compile a c code y????????

Reply

67 Tanvi March 5, 2013 at 6:33 pm

Reply me as early as u guies psble…….

Reply

68 Mia Sanders October 18, 2013 at 1:16 pm

Hi , I’m not an expert or anything like that, but I need to compile a program from an existing script.. I have none around to help me. If I type what you said:

gcc script.c -o script

I get:
12:22 fatal error: plataform.h: No such file or directory
compilation terminated

What does that means?

Thanks in advance.

Mia

Reply

69 kwadwo January 19, 2014 at 4:15 am

I took some of your code for bash c compile and run and I lift my head of the table. You are genius

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: