Development with GNU/Linux

An introduction to the GNU/Linux development platform.
Tools. Examples in C, C++ and Fortran.

Last significant update : 5 June 2004

1 Compilation

2 Programming Languages

3 Preprocessing

4 Compiling

A compiler translates the preprocessed source into text assembly language files (often .s on Unix, or .asm on Windows).

An assembly language is a human-readable notation for the machine language that a specific computer architecture uses.

5 Assembling

An assembler translates assembly files into machine code in binary object files (usually .o on Unix, .obj on Windows).

Object code is hardware architecture and operating system specific.

Object file formats are file formats used for the storage of object code. They include:

Object files can be linked with other object files to generate a final executable or a library.

6 Linking

A library is a collection of subprograms. It is not an independent programs. It provides services to other independent programs.

The linker (or link editor) combines several object files and library files into a single executable.

7 Libraries

8 Static Libraries

Static libraries are simply a collection of ordinary object files.

File name:  lib + library_name + .a

Create, modify, and extract from archives.
GNU linker.

9 Shared Libraries

The name of a static libraries file is like libname.a, while a shared libraries file is named, where:

This is the "real name" of a library file, the name of the file which contains it. In addition, a library has a "soname", the real name with only the major version number, which is a symbolic link to the real name. It may also have a "linker name", without any version number, a symbolic link to a "soname".

Configure dynamic linker run-time bindings.

ldconfig creates the necessary symbolic links and cache (/etc/ for use by the run-time linker, to the most recent shared libraries found in the directories specified on the command line, in the file /etc/, and in the trusted directories /usr/lib and /lib. Libraries listed in /etc/ will take precedence over the standard set.

This process can be overriden with the environment variable LD_LIBRARY_PATH: a colon-separated set of directories where libraries should be searched for first, before the standard set of directories.

Print shared library dependencies.

10 The GNU C Library

Any Unix-like operating system needs a C library. The GNU C library is used as THE C library in the GNU system and most systems with the Linux kernel.

It provides:

Configure dynamic linker run-time bindings.
Print shared library dependencies.

11 The GNU Compiler Collection (GCC)

12 GCC

The GNU Compiler Collection (GCC) is an integrated distribution of compilers for C, C++, Objective-C, Java, Fortran, and Ada.

The language-independent component of GCC includes the majority of the optimizers, as well as the "back ends" that generate machine code for various processors. The part of a compiler that is specific to a particular language is called the "front end". In addition to the front ends that are integrated components of GCC, several other front ends that are maintained separately support languages such as Pascal, Mercury, and COBOL.

Most of the compilers for languages other than C have their own names. They may be referred to by their own name, or as GCC. Either is correct:

13 binutils

The GNU binutils are a collection of binary tools. Some are used by GCC:

They also include:

14 GCC and File Extensions

Extension Meaning
.h C header file (not to be compiled or linked).
.c C source code which must be preprocessed.
.i C source code which should not be preprocessed.
.ii C++ source code which should not be preprocessed.
.cc, .cp, .cxx, .cpp, .c++, .C C++ source code which must be preprocessed.
.f, .for, .FOR Fortran source code which should not be preprocessed.
.F, .fpp, .FPP Fortran source code which must be preprocessed (with the traditional preprocessor).
.r Fortran source code which must be preprocessed with a RATFOR preprocessor (not included with GCC).
.s Assembler code.
.S Assembler code which must be preprocessed.
other An object file to be fed straight into linking. Any file name with no recognized suffix is treated this way.

An alternative is to use the -x option of GCC to specify the language of input files explicitly. -x none is the same as the default behaviour, and causes files to be handled according to their extension. Other options include -x c, -x c++, and -x f77.

Yet another alternative is to use g++ and g77. These programs invoke gcc with appropriate options for C++ and Fortran.

15 GCC Options

GCC command line options
Option Meaning
-E Stop after the preprocessing stage; do not run the compiler proper.
-S Stop after the stage of compilation proper; do not assemble. Transform each non-assembler input file into an assembler code file (.s).
-c Compile or assemble the source files, but do not link. Generate an object file (.o) for each source file.
-o filename Place output in file filename.
-g Produce debugging information.
-O Optimize. Same as -O1.
-Os Optimize for size.
-static Static linking.
-shared Dynamic linking (default).
-pedantic Issue all the mandatory diagnostics listed in the C standard.
-Wall Issue all warnings.
-ansi Specify the standard to which the code should conform.
-v Print the commands executed to run the stages of compilation.

16 C

17 Hello World (C Version)

File hello.c:

#include <stdio.h>
main ()
  printf("Hello world\n");

18 Compiling and Linking in One Step

gcc hello.c

carries out both the compiling (preprocessing, compiling and assembling) and linking processes in a single step. The default name of the output file is a.out (an ELF file despite its name).

Use the -o option to create an output file with a different name:

gcc hello.c -o hello

19 Compilation and Linking in Several Steps

Preprocessing only:

gcc -E hello.c -o hello.i

To stop after the stage of compilation proper and produce assembly language in hello.s:

gcc -S hello.i

Compiling without linking can be done with the -c option. The output file will be hello.o and will contain object code.

gcc -c hello.s


gcc -o hello hello.o

20 C++

21 Hello World (C++ Version)

#include <iostream>

int main(){
  std::cout << ''Hello World" << std::endl;
  return 0;

22 Fortran

23 Fortran Support

Fortran77 has been superseded by Fortran90 and Fortran95. With some extremely minor exceptions, the whole of Fortran77 is a subset of Fortran90, and Fortran95 was a relatively minor revision to Fortran90. Fortran77 is still important because a huge amount of code exists.

GNU's g77 is an excellent Open Source Fortran77 compiler.

There are two incomplete Fortran 90/95 Free/Open Source compilers.

Convert Fortran 77 to C or C++ (also the name of the GNU Fortran library).
Fortran 77 program checker.

24 Fortran Examples

      DIMENSION X(100), Y(100)
      DO 10 I=1,N
      CALL TRIG(N,X,Y)
      DO 20 J=1,5
      PRINT 15, X(J), Y(J)
 15   FORMAT(2X,2F8.3)
      DIMENSION X(1), Y(1)
      DO 10 I=1,N
      Y(I) = SIN(X(I))*EXP(-X(I))

25 The make Utility

26 make

GNU make utility to maintain groups of programs.

make is used to:

A typical makefile (Makefile so that it appears near the top of the directory listing) contains:

27 Makefile

OBJ=file1.o file2.o file3.o

	$(GCC) $(OBJ) -o my_program


	/bin/rm -f $(OBJ) my_program

28 Tools and Resources

29 Programming Resources

30 Tools and Resources