Semaine 10
Compilateur,
inclure des bibliothèques
inclure des bibliothèques
SSIE Programmation en C, Thomas Lochmatter, 2019
1
Compiler un programme C
gcc -Wall projet.c image.c -lpng -lm -o projet
2
#include <stdio.h> #include <math.h> int main(int argc, char * argv[]) { double resultat = sin(15.0 / 180.0 * M_PI); printf("%f\n", resultat); return 0; }
// Dans math.h ... #define M_PI 3.14159265358979323846 ...
3
#define M_PI 3.14159265358979323846 double resultat = sin(15.0 / 180.0 * M_PI); double resultat = sin(15.0 / 180.0 * 3.14159265358979323846);
// Problématique #define M_PI 0 + 3.14159265358979323846 double resultat = sin(15.0 / 180.0 * M_PI); double resultat = sin(15.0 / 180.0 * 0 + 3.14159265358979323846);
4
#ifndef SIE_IMAGE #define SIE_IMAGE ... struct Image { int width; int height; struct Pixel * pixels; }; void image_create(struct Image * image, int width, int height); ... #endif
C preprocessor sur Wikipedia
5
str0000 = constant "%f\n" main: tmpA ← double div, 15.0, 180.0 tmpB ← double mul, tmpA, 3.141 resultat ← call sin, tmpB call printf, str0000, resultat return 0
8
for (int i = 0; i < 360; i++) { double s = sin(M_PI / 180.0 * i); ... } // Optimisé double tmpA = M_PI / 180.0; for (int i = 0; i < 360; i++) { double tmpB = tmpA * i; double s = sin(tmpB); ... }
Loop-invariant code motion sur Wikipedia
9
# Compiler avec optimisations $$ gcc -Wall -O2 ...
-O0 | Pas d'optimisations (idéal pour débugger avec GDB) |
-O1 | Optimisations simples (low-hanging fruits) |
-O2 | Presque toutes les optimisations |
-O3 | Exécutable plus grand, mais encore plus rapide |
-Os | Optimiser la taille de l'exécutable (size) |
10
int gris(struct Pixel * pixel) { return pixel->r + pixel->g + pixel->b; } void traitement() { ... for (int i = 0; i < image.width * image.height; i++) { int gr = gris(image.pixels + i); ... } ... }
11
a ← a + b * c
Processeur sans FMA
tmp ← b * c a ← a + tmp
Processeur avec FMA
fma a, b, c
FMA = fused multiply-accumulate,
Multiply–accumulate operation sur Wikipedia
12
0000000000001135 main 1135: 55 push %rbp 1136: 48 89 e5 mov %rsp,%rbp 1139: 48 83 ec 20 sub $0x20,%rsp 113d: 89 7d ec mov %edi,-0x14(%rbp) 1140: 48 89 75 e0 mov %rsi,-0x20(%rbp) 1144: f2 0f 10 05 c4 0e 00 movsd 0xec4(%rip),%xmm0 114b: 00 114c: f2 0f 11 45 f8 movsd %xmm0,-0x8(%rbp) 1151: f2 0f 10 45 f8 movsd -0x8(%rbp),%xmm0 1156: 48 8d 3d ab 0e 00 00 lea 0xeab(%rip),%rdi 115d: b8 01 00 00 00 mov $0x1,%eax 1162: e8 c9 fe ff ff callq 1030 <printf@plt> 1167: b8 00 00 00 00 mov $0x0,%eax 116c: c9 leaveq 116d: c3 retq 116e: 66 90 xchg %ax,%ax
objdump -d programme
pour afficher le code compilé
13
14
Erreurs du linker
par ex. fonction non trouvée
par ex. fonction non trouvée
Linker (computing) sur Wikipedia,
nm -D libX.so
pour afficher les symboles d'une bibliothèque
15
Compiler fichier par fichier
Pour les grands projets
# Compiler $$ gcc -Wall -O2 -c projet.c -o projet.o $$ gcc -Wall -O2 -c image.c -o image.o # Linker $$ gcc -Wall projet.o image.o -ljpeg -lpng -lm -o projet # Exécuter $$ ./projet
16