Compile C program on Linux, using gcc command



Configuration for GNU Linux Fedora Computer using Linux Command

 

    The C program on Linux can be compile using gcc command line.  The command example below show the step by step using gcc program or GNU project C and C++ compiler to compile c code / compile c program.

1. Verify the gcc program is install on your Linux box.

verify gcc installation on Linux box

[root@fedora ~]# gcc --version

gcc (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)

Copyright (C) 2008 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.

 

[root@fedora ~]#

 

2. Locate your c program code... usually have the filename end with .c, or Create a simple example of c program code:

create c program on Linux Fedora

[root@fedora ~]# cat > mycprogram.c

#include <stdio.h>

int main(void){

printf("My C program\n");

return 0;

}

             <--- Hit Ctrl+D key here

[root@fedora ~]#

 

4. Type in on the terminal, gcc and then your c program file name

Compile c program on Linux Fedora

[root@fedora ~]# gcc mycprogram.c

[root@fedora ~]#

 

5.  The default output from c program above is a.out, and you can execute the c program that you compile on above command by type ./a.out

C program output

[root@fedora ~]# ./a.out

My C program

[root@fedora ~]#

 

Compile C program summary:

 

[root@fedora ~]# gcc mycprogram.c <--  This gcc command default output will be a.out and can be execute by

[root@fedora ~]# ./aoutput

 

[root@fedora ~]# gcc mycprogram.c -o thecprogram  <-- Output of the program will be named as the program and can be execute by

[root@fedora ~]# ./thecprogram

 

[root@fedora ~]# cc mycprogram.c -o thecprogram  <-- the cc command also can be use to compile c program and the output of the program will be named as thecprogram and can be execute by

[root@fedora ~]# ./thecprogram

 




2 comments:

vaibhav said...

Dear sir,
Hi i am Vaibhav K. Rajolkar studying in third sem computer science and we have Unix subject. for practice of Unix i am using Linux 9 (red hat version 2.6).
while compiling c program by using cc -o test file_name.c it is showing that cc command not found. so i came to your website for different command.when i tried this command it is showing that gcc command not found.so please tell solution .
I am waiting for your quick responce.

Thanking you,

Yours faithfully
vaibhav.

Anonymous said...

to compile c code, first you need the gcc compiler installed. type-->

whereis gcc

into a terminal, if nothing comes up then it isn't installed. my gcc is installed in: /usr/bin/gcc and other files in /usr/lib/gcc /usr/lib64/gcc /usr/share/man/man1/gcc.1.gz