www.thebits.co.uk

Creating Your First Program in Linux

For now, it's not important to understand all those cryptic C commands. All that matters is that minutes from now, you will have learnt how to create a program in C, compile it and run it. Then you can act all smug in front of your mates ;)

1. At the command line, pick a directory to save you program and enter:

VIM firstprog.c

2. Enter the following program (See 'How to Access Your MS-DOS Floppy', for instructions on using VIM):

include <stdio.h>
int main()
{
int index;
for (index = 0; index < 7; index = index + 1)
 printf ("Hello World!\n");
return 0;
}

3. Save your program and exit VIM.

4. Enter the following to create an executable (a program that can be run) from your source code (firstprog.c):

gcc -o myprog firstprog.c
  1. gcc (GNU C Compiler) is passed...
  2. -o which means optimise the executable program (i.e. make it smaller and faster than a plain executable)
  3. call the executable 'myprog'
  4. and the program to compile is 'firstprog.c'

5. To run you program, enter the following:

./myprog

./ means run a file located in the current directory.

You could quite easily have entered something like:

/home/Laurence/myprog

...If that happened to be the directory 'myprog' was in.

Laurence Hunter
thebits@thebits.co.uk