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

by nixcraft · 15 comments

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:

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 15 comments… read them below or add one }

1 Mps 03.21.06 at 7:43 pm

Hello,

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

2 nixcraft 03.21.06 at 10:50 pm

Thanks for heads up.

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

4 conrad sobol 06.11.07 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.

5 nixcraft 06.11.07 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.

6 haneefa 06.22.07 at 10:26 am

i want a C program Compiler Code In VI editor

7 king777 01.22.08 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.

8 anonymous 10.10.08 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.

9 liju 02.21.09 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

10 Zashk 03.29.09 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

11 DarkBattM14 05.26.09 at 5:14 am

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

12 Omar 08.24.09 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?

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

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

15 solomon komba 02.03.10 at 11:52 am

i want to know how to create executable file.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All