Linux Programming for beginners

This section should help you to start programming in Linux. I will cover here only the basics like installation of gcc compiler, make utility and I show you, how to write makefile.

If you want to start programming in Linux, you can of course choose to use IDE (Integrated Development Environment) as Code Blocks etc.

1. gcc and make

Before you start programming, you have to check whether you have already had installed gcc compiler and make utility by typing:
which gcc
which make
The output of these commands should be the paths where they are located. If so, you can skip the Installation of gcc and make paragraph. If you get no output read the next paragraph. Also you can find out the version of gcc and make by typing:
gcc -v
make -v
of course only when they are installed.

2. Installation of gcc and make

This varies on different Linux distributions.
On Debian and his derivates (Ubuntu/Xubuntu, etc) type:
sudo apt-get install gcc
sudo apt-get install make
to install gcc compiler and make utility.
On Fedora use yum instead of apt-get:
sudo yum install gcc
sudo yum install make
If it doesn't work, you have to download gcc and make from their official sites here: gcc, make
You will find everything about installing them on this sites.

3. The C code

Now you have to write some code in C and save it into file with extension .c for example main.c
The basic Hello World program should looks like this:
#include <stdio.h>

int main(void)
{
	printf("Hello World\n");
	return 0;
}
This code will write Hello World on the output after launching. Before launching you need to compile it by using gcc with make.

4. make and makefile

make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. Now you need to create a file called makefile.
Make sure that makefile is in the same directory as your .c file (main.c).
#This is a comment in makefile
NAME=main				#the name of program
CC=gcc					#using of gcc compiler
CFLAGS=-std=c99 -pedantic -Wall		#compiler flags

$(NAME): $(NAME).c			#the rule for making main
	$(CC) $(CFLAGS) $(NAME).c -o $(NAME)
-std=c99	# this flag tells the compiler to use the standard ISO C99
-pedantic	# takes care that the program strictly follows the standard ISO C99
-Wall		# turns on warning messages
Rule for making:
$(NAME): $(NAME).c
will replace $(NAME) by main because we declared:
NAME=main
and it tells that main (executable) will be created from main.c
In Linux - main is like .exe file in Windows.

5. Compiling

You should read something about basic Linux/UNIX commands. You can learn the basics for example here. Or try to search for 'basic linux commands' on Google.

So we have main.c and makefile both in the same directory. Now we have to run make utility which builds program. You need to run terminal and get to the directory where you saved main.c and makefile files.

For example, if you have main.c and makefile in the Desktop directory, run terminal and type:
cd Desktop
this command will get you into Desktop folder.
You can check whether you are in right directory by typing:
pwd
and you can also use:
ls
command to list directory contents.
When you are in the right directory with main.c and makefile you just need to type:
make
make will create executable main in the same directory.
Start the program by typing:
./main
Author: Matus Marhefka. Last modification: 11.12.2012

/Linux
Programming