Documente Academic
Documente Profesional
Documente Cultură
9
Compilarea i execuia programelor
15 decembrie 2008
Motto
There are only two kinds of programming languages: those people always bitch about and those nobody uses.
Bjarne Stroustrup
Programe
Ce este un program?
Fiier executabil binar (coninutul este cod main)
La nceput programele erau scrise direct n cod main (binar) (cartele perforate)
Anevoios Dificil de ntreinut
15.12.2008 3
Editoare
Scrierea de fiiere text (n particular cod surs) Editoare
simple one tool for the job
vim, Emacs, nano, joe notepad++, notepad2, ultraedit, Crimson Editor
integrate (IDE)
Visual Studio 2008 (varianta Visual Studio Express Edition este free) Eclipse, NetBeans Emacs, Kdevelop
Faciliti
syntax higlighting, auto indentation, utilitare pentru debugging integrate code folding, code completion (autocompletion)
15.12.2008 4
XEmacs
15.12.2008
Eclipse
15.12.2008
Compilare i interpretare
Un fiier cod surs poate fi compilat sau interpretat Care este deosebirea ntre compilare i interpretare?
Compilare: codul surs este translatat de un program denumit compilator n cod main, dup care poate fi executat Interpretare: un program este executat direct din cod surs prin intermediul unui interpretor
Avantaje/dezavantaje
Interpretare
Sunt, n general, mai uor de neles de programator Debugging facil Execuie lent
Compilare
Debugging greoi (folosirea unui depanator debugger) Vitez mare de execuie
15.12.2008 8
Compilare
Un compilator este un translator
Translateaz codul surs n cod obiect; de la un limbaj de nivel nalt n cod main
razvan@asgard:~/code$ ls main.c razvan@asgard:~/code$ gcc main.c razvan@asgard:~/code$ ls a.out main.c razvan@asgard:~/code$ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0, dynamically linked (uses shared libs), not stripped
15.12.2008
Fazele compilrii
Component/utilitar specializat pentru fiecare faz Preprocesare: cpp
nlocuirea directivelor de preprocesare (#include, #define) nu este specific tuturor limbajelor
Asamblare: as
translaterea fiierului n limbaj de asamblare ntr-un modul obiect (cod main)
Linking: ld
legarea mai multor module obiect i biblioteci ntr-un modul de sine stttor (de obicei un fiier executabil)
15.12.2008 10
15.12.2008
Preprocesare
Fiier cod surs Fiier preprocesat
razvan@asgard:~/code$ gcc -E -o simple.i simple.c razvan@asgard:~/code$ cat simple.i # 1 "simple.c" # 1 "<built-in>" #define ITERATIONS #define INITIAL #define HEADER_STRING 10 1 "Number sum: " # 1 "<command line>" # 1 "simple.c" # 1 "/usr/include/stdio.h" 1 3 4 ............. int main (void) { int i; int sum; int main (void) { int i; int sum; printf (HEADER_STRING); for (i = INITIAL; i < INITIAL + ITERATIONS; i++) sum += i; printf ("Number sum: "); for (i = 1; i < 1 + 10; i++) sum += i; printf ("%d\n", sum); return 0; } } printf ("%d\n", sum); return 0; razvan@asgard:~/code$ cat simple.c #include <stdio.h>
15.12.2008
12
Compilare
Program incorect din punct de vedere sintactic sau semantic -> erori la compilare:
razvan@asgard:~/code$ gcc main.c main.c: In function `main': main.c:6: error: `i' undeclared (first use in this function) main.c:6: error: (Each undeclared identifier is reported only once main.c:6: error: for each function it appears in.)
15.12.2008
13
Compilare (2)
Fiier cod surs Fiier dup compilare
#include <stdio.h> #define ITERATIONS #define INITIAL #define HEADER_STRING int main (void) { int i; int sum; sum = 0; printf (HEADER_STRING); for (i = INITIAL; i < INITIAL + ITERATIONS; i++) sum += i; printf ("%d\n", sum); return 0; } 15.12.2008 10 1 "Number sum: " .LC0: .string "Number sum: " .LC1: .string "%d\n" .text .globl main .type main: pushl movl subl andl movl movl call 14 $0, -8(%ebp) $.LC0, (%esp) printf %ebp %esp, %ebp $24, %esp $-16, %esp main, @function razvan@asgard:~/code$ gcc -S simple.c razvan@asgard:~/code$ cat simple.s .file .section "simple.c" .rodata
Compilare (3)
Translatarea fiierului cod surs (eventual preprocesat) n fiier n limbaj de asamblare Cunotine despre arhitectura sistemului de calcul. Cross-compiling
compilarea unui fiier pentru a fi folosit pe un sistem diferit de cel local
15.12.2008
15
Compilare (4)
Se recomand (insistent) folosirea opiunii -Wall Opiunea Wall previne neajunsuri n sintaxa C
folosirea operatorului = n loc de ==
#include <stdio.h> int main() { int i = 0; if (i = 0) printf("Best C program ever!"); else printf ("I have failed!!"); } ~$ gcc -o uso main.c ~$ ./uso I have failed!! ~$ gcc Wall main.c -o uso main.c:7: warning: suggest parentheses around assignment used as truth value
#include <stdio.h> int main() { int i = 0; if ( i == 0 ) printf("Best C program ever!"); else printf ("I have failed!!"); } 15.12.2008
16
Limbaj de asamblare
Form de reprezentare a instruciinilor procesorului
folosete mnemonici (movl,
movl $0, %eax cmpl, addl) ; stocheaza valoarea 0 in eax
sintaxa AT&T
movl cmpl
15.12.2008
15.12.2008
profiling
18
Asamblare
razvan@asgard:~/code$ as -o simple.o simple.s razvan@asgard:~/code$ objdump --disassemble simple.o simple.o: file format elf32-i386
Disassembly of section .text: 00000000 <main>: 0: 1: 3: 6: 9: e: 10: 17: 1e: 23: 4f: 54: 59: 5a: 15.12.2008 e8 fc ff ff ff b8 00 00 00 00 c9 c3 call mov leave ret 19 50 <main+0x50> $0x0,%eax 55 89 e5 83 ec 18 83 e4 f0 b8 00 00 00 00 29 c4 c7 45 f8 00 00 00 00 c7 04 24 00 00 00 00 e8 fc ff ff ff c7 45 fc 01 00 00 00 push mov sub and mov sub movl movl call movl %ebp %esp,%ebp $0x18,%esp $0xfffffff0,%esp $0x0,%eax %eax,%esp $0x0,0xfffffff8(%ebp) $0x0,(%esp) 1f <main+0x1f> $0x1,0xfffffffc(%ebp)
Asamblare (2) Translatarea fiierului n limbaj de asamblare n fiier cod obiect Instruciunile n limbaj de asamblare se traduc una cte una n echivalentul lor binar (cod main) Asamblor Exemplu rulare as/msvc
razvan@asgard:~/code$ as -o simple.o simple.s F:\code>cl /Fasimple.asm simple.c
15.12.2008 20
Biblioteci
Bibliotec = library librrie Colecie de funcii des utilizate de ctre programe stocate ntr-un singur fiier (modul)
de fapt, o colecie de module obiect
15.12.2008
22
Biblioteci (2)
Biblioteci statice (static libraries)
funciile de bibliotec apelate de un program sunt ataate codului executabil la linking codul executabil se mrete eventuale modificri ale bibliotecii din sistemul de operare nu afecteaz n nici un fel programul
Linking
15.12.2008
24
Linking (2)
Mai multe module obiect (inclusiv biblioteci) sunt grupate ntr-un modul de sine stttor (de obicei executabil) La legare/linking se rezolv referinele
1. Un modul apeleaz o funcie (sau o variabil) dintr-un alt modul 2. n urma compilrii se creeaz o referin ctre acea funcie (fr a se ti n ce modul se gsete) 3. Linker-ul rezolv aceste referine, gsind funciile (variabilele) apelate din alte module 4. Dac se apeleaz o funcie dintr-un modul ce nu se regsete la linking, apare eroare la linking
15.12.2008
25
Crearea de biblioteci
Fiierul add.c implementeaz funcia add, iar sub.c implementeaz funcia sub Dorim o bibliotec al crei coninut s fie dat de cele dou module Crearea i legarea unei biblioteci statice
razvan@asgard:~/code$ gcc -Wall -c add.c razvan@asgard:~/code$ gcc -Wall -c sub.c razvan@asgard:~/code$ ar cr libsimple.a add.o sub.o razvan@asgard:~/code$ ar t libsimple.a add.o sub.o razvan@asgard:~/code$ gcc -Wall -L. -o lib_main lib_main.c lsimple
15.12.2008
26
Executabil
Tip special de fiier binar
poate fi ncrcat de sistemul de operare ntr-un proces pentru a fi rulat
Unix: trebuie precizat explicit faptul c rulm executabilul din directorul curent
razvan@asgard:~/code$ ./lib_main add = 2, sub = 0
Execuia i folosirea bibliotecilor partajate sau dinamice au nevoie de suportul sistemului de operare Loader
componenta sistemului de operare care transpune informaia dintr-un executabil ntr-un proces
15.12.2008 28
editor
cod surs
preprocesor
cod preprocesat
compilator
asamblor
cod obiect
executabil
linker bibliotec
cod obiect
15.12.2008
29
Makefile simplu
$ cat Makefile all: test test: test.o gcc -o test test.o test.o: test.c gcc -Wall -c -o test.o test.c $ make gcc -Wall -c -o test.o test.c gcc -o test test.o $ ./test Hello, World!
15.12.2008 31
Makefile upgrade
$ cat Makefile1 CC = gcc CFLAGS = -Wall all: test test: test.o $(CC) -o $@ $^ test.o: test.c $(CC) $(CFLAGS) -c -o $@ $< clean: -rm -f *~ *.o test $ make -f Makefile1 clean rm -f *~ *.o test $ make -f Makefile1 gcc -Wall -c -o test.o test.c gcc -o test test.o
15.12.2008 33
Variabile automate
$@ -> inta (target-ul) $^ -> toate dependinele $< -> prima dependin
Opiunea -f specific un fiier Makefile altul dect cel implicit (Makefile sau GNUMakefile)
15.12.2008 34
Less is better
$ cat Makefile2 CFLAGS = -Wall all: test clean: -rm -f *~ *.o test $ make -f Makefile2 clean rm -f *~ *.o test $ make -f Makefile2 cc -Wall test.c $ ./test Hello, World!
15.12.2008 35
-o test
Less is better Se folosesc reguli implicite 1. 2. 3. 4. test nu depinde de alte target-uri, dar nu exist Se caut test.o sau test.c Se gsete test.c Se ruleaz comanda implicit
$(CC) $(CFLAGS) test.c -o test
CC este implic cc
razvan@anaconda:~$ ls -l /usr/bin/cc lrwxrwxrwx 1 root root 20 Oct 6 2005 /usr/bin/cc -> /etc/alternatives/cc razvan@anaconda:~$ ls -l /etc/alternatives/cc lrwxrwxrwx 1 root root 12 Apr 22 2007 /etc/alternatives/cc -> /usr/bin/gcc
Surse multiple
Un program este constituit, de obicei, din mai multe fisiere surs (module) Fiecare modul implementeaz o component a aplicaiei Exemplu: aplicaie chat
15.12.2008
un modul pentru funcii utile -> util.c un header pentru antetele funciilor utile -> util.h un modul pentru interfaa cu utilizatorul -> ui.c un modul pentru antetele funciilor de interfaare -> ui.h un modul care asigur comunicarea n reea -> net.c un header pentru antete -> net.h un modul de jurnalizare -> log.c un header petnru jurnalizare -> log.h un modul care integreaz celelalte module -> app.c
37
#include <stdlib.h> #include "util.h" int copy_to_list (char *msg, size_t msg_len, struct list *list) { [...] } int find_in_list (int id, struct list *list) { [...] }
struct list { int id; void *generic_data; struct list *next; struct list *prev; }; int copy_to_list (char *msg, size_t msg_len, struct list *list); int find_in_list (int id, struct list *list); #endif
15.12.2008
38
Dependine
util.h
log.h
net.h
ui.h
util.c
log.c
net.c
ui.c
app.c
util.o
log.o
net.o
ui.o
app.o
libm.a
15.12.2008
app
39
Teh Makefile
$ cat Makefile.prj # -g -> compilare cu simboluri de debug CFLAGS = -Wall -g LDLIBS = -lm all: app app: app.o ui.o log.o util.o net.o app.o: app.c util.h log.h ui.h net.h ui.o: ui.c ui.h util.h log.o: log.c log.h util.h util.o: util.c util.h net.o: net.c util.h clean: -rm -f *~ *.o app
15.12.2008 40
$ make -f Makefile.prj cc -Wall -g -c -o app.o app.c cc -Wall -g -c -o ui.o ui.c cc -Wall -g -c -o log.o log.c cc -Wall -g -c -o util.o util.c cc -Wall -g -c -o net.o net.c cc app.o ui.o log.o util.o net.o -lm -o app
Cuvinte cheie
program cod surs editor IDE compiator interpretor coding style preprocesare compilare limbaj de asamblare asamblare cod obiect bibliotec linker executabil cpp, gcc, gas, ld objdump, nm make, Makefile int, dependine variabile automate header
41 41
15.12.2008
Resurse utile
Link-uri utile
15.12.2008
42
42
The End
15.12.2008
43