How To Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD

last updated in Categories , , , , , , ,

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


Sample Unix C ProgramYou 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. [donotprint][/donotprint]But you can use gcc command to compile program. First make sure you have gcc C compiler installed:

Make Sure Compiler Is Installed On a Unix-like System

Type the following which command or type command or command command to verify that gcc is installed:
$ type -a gcc
$ command -V gcc
$ which gcc

Sample outputs:


Find out version of gcc, run:
$ gcc --version
Sample outpust:

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


The syntax is as follows to compile a C program on a Unix-like operating system:
gcc program.c -o program-output
cc program.c -o program-output
make program

Writing Your First C Program Under a Linux / UNIX-like system

Use a text editor such as vi or gedit or nano 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 C Program?

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

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


cc   first.o   -o first

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

See also:
This entry is 9 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

79 comment

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

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

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

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

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


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

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

  7. If using the Unix system

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

    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

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

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

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

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

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

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

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

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

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

  15. 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……

  16. Hi,

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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

  29. @ 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..

  30. 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,

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


  32. 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 !

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

  34. 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 -o codel fatal error: config.h: No such file or directory
    compilation terminated.

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

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


  37. i have a c program in programs folder which is in d drive,but i need to generate .exe file in same source folder,How can i generate?even i set path for c but, i need to acess program from d drive how?
    can anyone know the answer

  38. I have question.
    1) The source code of a C program file is stored in a file with the ____ extension.

    2)____ is an example of how you can declare a string in C.

    Please can you send me Answer.

  39. In the tutorial example :-

    $ make first

    The output to screen is actually…
    cc first.c -o first

    Note the .c not .o as shown in the tutorial.

    Otherwise works as advertised! ;-)

    Ta Ta.

    Still, have a question? Get help on our forum!