An beginners guide to compiling programs under Linux.

Kim Oldfield

kimo@debian.lib.monash.edu.au

Presented to Linux Users of Victoria

This talk is a hands on guide for someone who has never compiled a program under linux before, or someone who has never tried to compile a package from source.

  1. Compiling a simple C program


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.

From /usr/doc/gcc/README.Debian.gz in gcc-2.95.2-0pre2:

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]);
    else
	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.