0% found this document useful (0 votes)
41 views

Compiling, Linking, Modular Programming

The document discusses the compilation and linking process in C programming. It begins with an overview of the three main steps: preprocessing, compilation, and linking. It then provides more details on the C preprocessor (cpp), how it handles includes and defines, and conditional compilation. Finally, it describes the linker (ld) and how it combines object files and libraries into an executable program.

Uploaded by

lordofdankness
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Compiling, Linking, Modular Programming

The document discusses the compilation and linking process in C programming. It begins with an overview of the three main steps: preprocessing, compilation, and linking. It then provides more details on the C preprocessor (cpp), how it handles includes and defines, and conditional compilation. Finally, it describes the linker (ld) and how it combines object files and libraries into an executable program.

Uploaded by

lordofdankness
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Lecture 7/8: Compiling, linking, modular

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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


The compilation process
What happens after you invoke gcc ?
1. Pre-processing (cpp): takes care of #include directives etc.
2. Compilation:
i. translate to assembler code ( *.s)
ii. assembly ( as), creates object file *.o (machine code)
3. Linking ( ld): includes library functions, creates executable p rogram

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


The compilation process
What happens after you invoke gcc ?
1. Pre-processing (cpp): takes care of #include directives etc.
2. Compilation:
i. translate to assembler code ( *.s)
ii. assembly ( as), creates object file *.o (machine code)
3. Linking ( ld): includes library functions, creates executable p rogram

Try out the following:


● download hello.c (from lecture 1)

● gcc -E hello.c (only runs preprocessor and sends output to stdout )

● gcc -S hello.c (creates assembler code hello.s)

● 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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


cpp – the C preprocessor

● cpp is basically a specialized text editor


● cpp is also used with other programming languages
● cpp processes the source-code file and eventually adds , removes or
changes parts of the text

preprocessor directives (editing commands for the p reprocessor) start
with ` #`

preprocessor directives are line-based, they do not end with a
semicolon `;`

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#include statement

#include <stdio.h>
#include “my_own_lib.h”

#include /home/archert/include/const.h

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#include statement
C's standard header files are
written in angle brackets ( <>)
#include <stdio.h>
#include “my_own_lib.h”

#include /home/archert/include/const.h

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#include statement
C's standard header files are
written in angle brackets ( <>)
#include <stdio.h>
#include “my_own_lib.h”

#include /home/archert/include/const.h

You can also include your own library


header files using double quotes ( “ ”)

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#include statement
C's standard header files are
written in angle brackets ( <>)
#include <stdio.h>
#include “my_own_lib.h”

#include /home/archert/include/const.h

You can also include your own library


header files using double quotes ( “ ”)
#include directives can contain
relative and absolute pathnames

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#include statement
C's standard header files are
written in angle brackets ( <>)
#include <stdio.h>
#include “my_own_lib.h”

#include /home/archert/include/const.h

You can also include your own library


header files using double quotes ( “ ”)
#include directives can contain
relative and absolute pathnames

Mind: do not accidentally include the same file twice ( see example later in
this lecture)

(more about header files in lecture 8)

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#define statement
Use #define to define constants:
#define SIZE 50

int data[SIZE];
...
for (i=0; i < SIZE; ++i)
... ;

Use #define to define macros:


/* square a number */
#define SQR(x) ((x)*(x))
...
printf(“%d %d”, i+1, SQR(i+1));

/* unsafe macro definition */


#define SQUARE(x) (x*x)
printf(“%d”, SQUARE(i+1));

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#define statement
Use #define to define constants:
#define SIZE 50 replaces every occurrence
of “SIZE” by “50”
int data[SIZE];
...
for (i=0; i < SIZE; ++i)
... ;

Use #define to define macros:


/* square a number */
#define SQR(x) ((x)*(x))
...
printf(“%d %d”, i+1, SQR(i+1));

/* unsafe macro definition */


#define SQUARE(x) (x*x)
printf(“%d”, SQUARE(i+1));

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#define statement
Use #define to define constants:
#define SIZE 50 replaces every occurrence
of “SIZE” by “50”
int data[SIZE];
...
for (i=0; i < SIZE; ++i)
... ;

Use #define to define macros:


/* square a number */
#define SQR(x) ((x)*(x))
... Parentheses indicate
printf(“%d %d”, i+1, SQR(i+1)); macro parameter

/* unsafe macro definition */


#define SQUARE(x) (x*x)
printf(“%d”, SQUARE(i+1));

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


#define statement
Use #define to define constants:
#define SIZE 50 replaces every occurrence
of “SIZE” by “50”
int data[SIZE];
...
for (i=0; i < SIZE; ++i)
... ;

Use #define to define macros:


/* square a number */
#define SQR(x) ((x)*(x))
... Parentheses indicate
printf(“%d %d”, i+1, SQR(i+1)); macro parameter

/* unsafe macro definition */


#define SQUARE(x) (x*x)
printf(“%d”, SQUARE(i+1)); Note: incorrect use of parentheses,
expands to: i+1*i+1 == 2*i+1
→ put parentheses around the
parameters and around the whole
macro!

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Conditional compilation

#ifdef DEBUG
printf(“Debug version (more output)\n”);
#else
printf(“Production version\n”);
#endif

→ You can now compile a “debug” version by setting t he corresponding #define


flag in the command line: gcc -DDEBUG program.c

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Conditional compilation

#ifdef DEBUG
printf(“Debug version (more output)\n”);
#else
printf(“Production version\n”);
#endif

→ You can now compile a “debug” version by setting t he corresponding #define


flag in the command line: gcc -DDEBUG program.c

● 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_*/

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Conditional compilation

#ifdef DEBUG
printf(“Debug version (more output)\n”);
#else
printf(“Production version\n”);
#endif

→ You can now compile a “debug” version by setting t he corresponding #define


flag in the command line: gcc -DDEBUG program.c

● 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
...

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


The linker
The linker (ld) combines object files (*.o) and library archives (*.a) into an
executable program (binary).

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)

Directories where ld searches for library files:


● standard directories (defined in /etc/ld.so.conf): /lib, /usr/lib,

/usr/local/lib, ...
● directories specified with -L on command line

● directories listed in environment variable LD_LIBRARY_PATH

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Example: Complex numbers
(see: Lecture 3 – Advanced data structures)

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.

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Example: Complex numbers

● Header file: complex.h


typedef struct
{
double real;
double imag;
} complex;

complex add(complex, complex);

/* multiply two complex numbers */


complex mult(complex, complex);

/* calculate the absolute square value of a complex number */


double abs_sqr(complex);

/* calculate the complex conjugate */


complex conjug(complex);

● File containing the function source code: complex.c

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Example: Complex numbers

● Program that uses complex number module: test_compl.c


#include <stdio.h>
#include "complex.h"

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);
}

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Example: Complex numbers

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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Example: Complex numbers

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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


make
Utility that simplifies compilation of many inter-r elated sources!
http://www.tcd.ie/Physics/People/Claude.Ederer/teac hing/3C01/makefile
#
# Makefile for complex number example Comments start with '#'
#
CC=gcc
CFLAGS=-g -Wall -ansi

all: prog

prog: test_compl.o complex.o


$(CC) $(CFLAGS) -o prog test_compl.o complex.o

test_compl.o: test_compl.c complex.h


Definition of “rules”:
complex.o: complex.c complex.h target: source [source2] [...]
command
clean:
rm -f prog test_compl.o complex.o [command2]
[...]
● To compile the program just type 'make'
● make compiles only files that have been changes since th e last compile
● For more information: man make

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Libraries

Two different types of libraries:

1) static: Included in executable at compilation time from l ibrary archive


file (usually called *.a), produces larger code but executable can run
independently
2) dynamic: called only at runtime, leads to slim code but li brary has to
be present at runtime (shared library files are cal led *.so)

You can easily create your own C-libraries and use them in your future
programs!

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Create your own library (static)
1) Create source code containing library functions
2) Test your library functions thoroughly!
3) Compile object file containing your library functions
gcc -c mylib.c → produces mylib.o
7) Create indexed library archive file
ar rs mylib.a mylib.o → produces mylib.a
You can also create an archive file from multiple o bject files!
9) Create header file mylib.h for your library containing all necessary
variable and function declarations
11)Use #include “mylib.h” in your program
13)Compile your program
gcc -o myprog myprog.c mylib.a

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Create your own library (static)
1) Create source code containing library functions
2) Test your library functions thoroughly!
3) Compile object file containing your library functions
gcc -c mylib.c → produces mylib.o
7) Create indexed library archive file
ar rs mylib.a mylib.o → produces mylib.a
You can also create an archive file from multiple o bject files!
9) Create header file mylib.h for your library containing all necessary
variable and function declarations
11)Use #include “mylib.h” in your program
13)Compile your program
gcc -o myprog myprog.c mylib.a

Exercise: Create a library complex.a from the corresponding module files.


Then compile test_compl.c and link it with your newly created library.

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Create your own library (dynamic)

1) Create object files containing your library functions and definitions


gcc -c -fpic complex.c

generates “position independent code”

3) Generate shared library


gcc -shared -o libcomplex.so complex.o

5) You still need the corresponding header file!

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Create your own library (dynamic)

1) Create object files containing your library functions and definitions


gcc -c -fpic complex.c

generates “position independent code”

3) Generate shared library


gcc -shared -o libcomplex.so complex.o

5) You still need the corresponding header file!

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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Create your own library (dynamic)

1) Create object files containing your library functions and definitions


gcc -c -fpic complex.c

generates “position independent code”

3) Generate shared library


gcc -shared -o libcomplex.so complex.o

5) You still need the corresponding header file!

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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Lecture 7/8: Compiling, linking, modular
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

PY 3C01 – Computational Methods II: Lecture 7/8 – C ompiling, linking, ...


Lecture 7/8: Compiling, linking, modular
8ΕΩΟ
programming

The compilation process
0ΣΣΟ ΞΛςΣΨΚΛ ΞΛΙ ΓΣΘΤΠΙ∴ ΡΨΘΦΙςΩ Ι∴ΕΘΤΠΙ
● The C preprocessor (cpp)

∋ΣΘΤΜΠΙ ΞΛΙ(ld)
● The linker ΓΣΗΙ ΦΣΞΛ [ΜΞΛ ΕΡΗ [ΜΞΛΣΨΞ ΞΛΙ
ΨΩΙ● Σϑ Ε ΘΕΟΙ
Variable scope ΠΙ
● Using modules

● make

● Example: complex number module

● Create your own libraries

● Debugging

● Style

PY 3C01 – Computational Methods II: Lecture 7/8 – Compiling, linking, ...

You might also like