.: Most Read :.

Join Churp Churp

Join Churp Churp
Click on the image to find out more

Thursday, November 07, 2013

Tutorial: Simple C in Linux (Ubuntu)

Create, and compile simple C file on Linux terminal.


You have updated and upgraded your Linux OS.

sudo apt-get update

sudo apt-get upgrade

Installed gcc

sudo apt-get install gcc

Installed make (optional)

sudo apt-get install make



Login to your Linux machine.

vi sampleC.c

Press I to enter Insert mode.

Type the following C source code:


void main(void)


printf("Hello world\n");


Done with writing the source code.

Press Esc

Then :wq [write and quit vi tool]

To compile, you can do this in many ways;

Method 1 – direct compilation

gcc sampleC.c -o sampleC


Method 2 – compile using makefile

create a new make file

vi sampleMakefile

Type the following commands in makefile:


all: sampleC

#.o file

   sampleC: sampleC.o

   gcc -o sampleC.o

#.c file

   sampleC.o: sampleC.c

   gcc -o sampleC.c

Done with writing the commands in makefile.


You may replace “sampleC” by using your own filename.

# is used for command.

Press Esc

Then :wq [write and quit vi tool]

Execute the make file

make -f sampleMakefile

Make sure there is no error.

Now, your program is ready to be executed.


Sample output:

Hello world

No comments:

Post a Comment