// printvec.cc
#include <iostream.h>
void PrintVec(const int n, const double x[])
{
...
}
void PrintMat(const int nrow, const int ncol, const double a[])
{
...
}
|
Das File printvec.cc wird nun compiliert (ohne es zu linken!)
LINUX> g++ -c printvec.cc
wodurch das Objektfile printvec.o erzeugt wird.
Das Hauptprogramm in Ex751-old.cc
(siehe Ex751-old.cc)
benötigt nunmehr die Deklarationen der beiden Funktionen.
// Ex751-old.cc
// declarations of functions from printvec.cc
void PrintVec(const int n, const double x[]);
void PrintMat(const int nrow, const int ncol, const double a[]);
main()
{
const int N=4,M=3; // local constant
// static matrix a
double a[N][M] = {4,-1,-0.5, -1,4,-1, -0.5,-1,4, 3,0,-1 },
u[N] = {1,2,3,-2};
PrintMat(N, M, a[0]); // print matrix
PrintVec(N, u); // print vector
}
|
Das Compilieren des Hauptfiles
LINUX> g++ -c Ex751-old.cc
erzeugt das Objektfile Ex751-old.o welches
mit dem anderen Objektfile zum fertigen Programm a.out
gelinkt werden muß
LINUX> g++ Ex751-old.o printvec.o
Sämtliches compilieren und linken läßt sich auch in einer
Kommandozeile ausdrücken
LINUX> g++ Ex751-old.cc printvec.cc
wobei manche Compiler im ersten Quelltextfile (hier Ex751-old.cc)
das Hauptprogramm main() erwarten.
Die Deklarationen im Hauptprogramm für die Funktionen aus printvec.cc schreiben wir in das Headerfile printvec.hh (siehe printvec.hh)
// printvec.hh // declarations of functions from printvec.cc void PrintVec(const int n, const double x[]); void PrintMat(const int nrow, const int ncol, const double a[]); |
und wir ersetzen den Deklarationsteil im Hauptprogramm durch die
von Präprozessoranweisung
#include "printvec.hh"
welche den Inhalt printvec.hh vor dem Compilieren von
Ex751.cc (siehe Ex751.cc)
automatisch einfügt.
// Ex751.cc
#include "printvec.hh"
main()
{
const int N=4,M=3; // local constant
// static matrix a
double a[N][M] = {4,-1,-0.5, -1,4,-1, -0.5,-1,4, 3,0,-1 },
u[N] = {1,2,3,-2};
PrintMat(N, M, a[0]); // print matrix
PrintVec(N, u); // print vector
}
|
Die Anführungszeichen " " um den Filenamen
kennzeichnen, daß das Headerfile printvec.hh
im gleichen Verzeichnis wie das Quelltextfile Ex751.cc
zu finden ist.
Das Kommando
LINUX> g++ Ex751.cc printvec.cc
erzeugt wiederum das Programm a.out.