Compiling, Linking, Modular Programming
Compiling, Linking, Modular Programming
programming
●
The compilation process
● The C preprocessor (cpp)
● The linker ( ld)
● Variable scope
● Using modules
● make
● Example: complex number module
● Create your own libraries
● Debugging
● Style
● gcc -c hello.c (creates object file hello.o but does not invoke linker)
● gcc hello.o (links object file with library routines and creat es executable)
● ./a.out
#include <stdio.h>
#include “my_own_lib.h”
#include /home/archert/include/const.h
#include /home/archert/include/const.h
#include /home/archert/include/const.h
#include /home/archert/include/const.h
#include /home/archert/include/const.h
Mind: do not accidentally include the same file twice ( see example later in
this lecture)
int data[SIZE];
...
for (i=0; i < SIZE; ++i)
... ;
#ifdef DEBUG
printf(“Debug version (more output)\n”);
#else
printf(“Production version\n”);
#endif
#ifdef DEBUG
printf(“Debug version (more output)\n”);
#else
printf(“Production version\n”);
#endif
● Example of const.h header file that checks whether it has already bee n included:
#ifndef _CONST_H_INCLUDED_
double pi = 3.141592654;
double euler = 2.718281828;
...
#define _CONST_H_INCLUDED_
#endif /*_CONST_H_INCLUDED_*/
#ifdef DEBUG
printf(“Debug version (more output)\n”);
#else
printf(“Production version\n”);
#endif
● Example of const.h header file that checks whether it has already bee n included:
#ifndef _CONST_H_INCLUDED_
double pi = 3.141592654;
double euler = 2.718281828;
... Check out preproc.c and try:
#define _CONST_H_INCLUDED_ cpp preproc.c
#endif /*_CONST_H_INCLUDED_*/ cpp -DDEBUG preproc.c
cpp -D_CONST_H_INCLUDED_ preproc.c
...
main.o
extern.o ld a.out
lib.a
Libraries are specified with -l option (when invoked via gcc this is already
taken care of)
/usr/local/lib, ...
● directories specified with -L on command line
complex.h
Public definitions for
#include “complex.h”
module “complex”
test_compl.c
#include “complex.h” program that uses
complex.c complex number module
Contains data structure
and basic arithmetic uses functions from complex.c
functions for complex
numbers.
main()
{
complex a={1.0, 0.0}, b={0.0, 1.0};
complex c;
c = add(a,b);
printf("%f %f\n",c.real, c.imag);
a = b;
c = mult(a,b);
printf("%f %f\n",c.real, c.imag);
printf("%f\n", abs_sqr(a));
c = conjug(a);
printf("%f %f\n", c.real, c.imag);
return(0);
}
complex.h
Public definitions for
#include “complex.h”
module “complex”
test_compl.c
#include “complex.h” program that uses
complex.c complex number module
Contains data structure
and basic arithmetic uses functions from complex.c
functions for complex
numbers.
Compile with:
gcc -c complex.c
gcc -c test_compl.c
gcc -o prog test_compl.o complex.o
or:
gcc -o prog test_compl.c complex.c
complex.h
Public definitions for
#include “complex.h”
module “complex”
test_compl.c
#include “complex.h” program that uses
complex.c complex number module
Contains data structure
and basic arithmetic uses functions from complex.c
functions for complex
numbers.
Compile with:
gcc -c complex.c
gcc -c test_compl.c Can get very complicated if
gcc -o prog test_compl.o complex.o many modules are used!
or:
gcc -o prog test_compl.c complex.c
all: prog
You can easily create your own C-libraries and use them in your future
programs!
You can create directories ~/include and ~/lib to hold all your include and
library files. Then you can compile with:
gcc -I~/include test_compl.c -L~/lib -lcomplex
You can create directories ~/include and ~/lib to hold all your include and
library files. Then you can compile with:
gcc -I~/include test_compl.c -L~/lib -lcomplex
Before running the program you have to also tell the linker where to find
libcomplex.so, e.g. by setting export LD_LIBRARY_PATH=~/lib
∋ΣΘΤΜΠΙ ΞΛΙ(ld)
● The linker ΓΣΗΙ ΦΣΞΛ [ΜΞΛ ΕΡΗ [ΜΞΛΣΨΞ ΞΛΙ
ΨΩΙ● Σϑ Ε ΘΕΟΙ
Variable scope ΠΙ
● Using modules
● make
● Debugging
● Style