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

Requirement:

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

 

Steps:

Login to your Linux machine.

vi sampleC.c

Press I to enter Insert mode.

Type the following C source code:

#include<stdio.h>

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

or

Method 2 – compile using makefile

create a new make file

vi sampleMakefile

Type the following commands in makefile:

#applicationFile

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.

Note:

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.

./sampleC

Sample output:

Hello world

No comments:

Post a Comment