Server Administration & Management

How to compile C program in linux

This article is for those guys who used to write a lot of programs under Windows.. and now have entered the Linux territory. You have probably heard a lot about Linux and how you can do some real good programming under Linux. But right now you cant even get the simplest of Hello World programs to compile. Here’s how you do it –

Procedure :

You can type you C program using any of the editors that are available under Linux such as vi or emacs or any other editor. Once you have written and saved your C program using any editor return to the prompt. An ls command should display your C program. It should have the .c extension. Now at the prompt type the following

# gcc -o firstprogram firstprogram.c

If your file is named firstprogram.c then type ‘-o firstprogram’ as the parameter to gcc. This is basically your suggested name for the executable file that gcc would create. In case you typed something like the following

# gcc firstprogram.c

You would be having a a.out in the same directory as the source C file. This is the default name of the executable that gcc creates. This would create problems when you compile many programs in one directory. So you override this with the -o option followed by the name of the executable

# gcc -o hello secondprogram.c

Would create an executable by the name hello for your source code named secondprogram.c Running the executable that you created is as simple as typing the following at the prompt.

# ./firstprogram “OR”

# ./hello

Or whatever you named your executable.

This is the absolute basics of compiling C programs under Linux. Watch out for the articles that shall explain multiple file program compilation using make and other such tools. Compiling a simple C program. What you need

Debian packages: gcc, cpp, binutils, libc-dev. C++ programs also require: g++, and libstdc++

RedHat packages: egcs, cpp, binutils, glibc-devel. C++ programs also require: egcs-c++, libstdc++. gcc and egcs

gcc is the standard Linux compiler, 2.7.2 is the latest stable version.

egcs is a breakaway development of gcc. This has since been renamed to gcc.

As of gcc-2.95, optimisation at level 2 (-O2) and higher includes an optimisation which breaks C code that does not adhere to the C standard. Such code occurs in the Linux kernel, and probably other places.

There are a couple of 272 packages for Debian potato (unstable) to get around this – gcc272, gcc++272. The program

/* hello.c */
#include <stdio.h>
int main(int argc, char *argv[])
if (argc > 1)
printf(“Greetings %s.n”, argv[1]);
printf(“Please tell me your name.n”);
return 0;

Compile using:
> gcc -Wall -o hello hello.c
Run with:
> ./hello Linus
Greetings Linus.

gcc options

-o –> output file name, (a.out if omitted)
-Wall –> display warnings for many possible errors
hello.c –> file to be compiled, can specify multiple .c files

The info pages (info gcc) has much more information about gcc. These are in the gcc-doc package.

July 20, 2011 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment