
http://labor-liber.org
Last significant update : 5 June 2004
Copyright © 2004 Cédric Musso, http://labor-liber.org.
Some rights reserved, according to the terms of theCreative Commons - Attribution-ShareAlike 1.0 license. http://creativecommons.org/licenses/by-sa/1.0/
#),
performing macro substitution, file inclusion, conditional inclusion, and other transformations.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.
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.
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.
Static linking includes a copy of the object files from libraries used by a program in the executable. The output file is large but it is a stand-alone executable.
Dynamic linking uses shared libraries which exist in only one location on the entire system. Dynamic linking is carried out at compile time. The resulting executable includes pointers to shared libraries which are loaded by programs when they start. The output file code is much smaller but it depends upon shared libraries installed on the system. Version inconsistencies may cause problems.
Runtime linking also uses
shared libraries, but they are not linked against at
compile time. It uses the dlopen()
API.
Libraries are loaded only when they are needed (plugin).
Static libraries are simply a collection of ordinary object files.
File name: lib + library_name + .a
arldThe name of a static libraries file is like libname.a, while
a shared libraries file is named libname.so.x.y.z, where:
x is the major version number. It is incremented
whenever the interface to the library changes.y is the minor version number.z is the optional release number.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".
ldconfigldconfig creates the necessary symbolic links and cache
(/etc/ld.so.cache) for use by the run-time linker,
ld.so) to the most recent shared libraries
found in the directories specified on the command line, in the file
/etc/ld.so.conf, and in the trusted directories
/usr/lib and /lib.
Libraries listed in /etc/ld.so.preload 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.
lddAny 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.
libc6 = glibc: version 2 of the GNU C Library,
and the sixth major version of the Linux C library.libc5: the first version of the Linux C Library based on
ELF.libc4: based on the a.out binary format.It provides:
ldconfiglddThe 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:
g77.g++.The GNU binutils are a collection of binary tools.
Some are used by GCC:
ld - the GNU linker.as - the GNU assembler.ar - A utility for creating, modifying and extracting from
archives (libraries).They also include:
addr2line - Converts addresses into filenames and line numbers.c++filt - Filter to demangle encoded C++ symbols.gprof - Displays profiling information.nlmconv - Converts object code into an NLM.nm - Lists symbols from object files.objcopy - Copys and translates object files.objdump - Displays information from object files.ranlib - Generates an index to the contents of an archive.readelf - Displays information from any ELF format object file.size - Lists the section sizes of an object or archive file.strings - Lists printable strings from files.strip - Discards symbols.windres - A compiler for Windows resource files.| 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.
| 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. |
File hello.c:
#include <stdio.h>
main ()
{
printf("Hello world\n");
}
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
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
Linking:
gcc -o hello hello.o
#include <iostream>
int main(){
std::cout << ''Hello World" << std::endl;
return 0;
};
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.
f2cftnchek
PROGRAM F77DEMO
DIMENSION X(100), Y(100)
PI=2.*ACOS(0.)
N=100
DO 10 I=1,N
X(I)=I*(2*PI/N)
10 CONTINUE
CALL TRIG(N,X,Y)
DO 20 J=1,5
PRINT 15, X(J), Y(J)
15 FORMAT(2X,2F8.3)
20 CONTINUE
STOP
END
C
SUBROUTINE TRIG(N,X,Y)
DIMENSION X(1), Y(1)
DO 10 I=1,N
Y(I) = SIN(X(I))*EXP(-X(I))
10 CONTINUE
RETURN
END
makemake is used to:
A typical makefile (Makefile so that it appears near
the top of the directory listing) contains:
$(foo) or ${foo}
is a valid reference to the variable foo.
target: dependencies ... <TAB>commands <TAB>...
#).
GCC=gcc OBJ=file1.o file2.o file3.o all:$(OBJ) $(GCC) $(OBJ) -o my_program file1.o:file1.c file2.o:file2.c file3.o:file3.c clean: /bin/rm -f $(OBJ) my_program