24.02.2014 Aufrufe

4 in 1 - Medieninformatik - Hochschule RheinMain

4 in 1 - Medieninformatik - Hochschule RheinMain

4 in 1 - Medieninformatik - Hochschule RheinMain

MEHR ANZEIGEN
WENIGER ANZEIGEN

Erfolgreiche ePaper selbst erstellen

Machen Sie aus Ihren PDF Publikationen ein blätterbares Flipbook mit unserer einzigartigen Google optimierten e-Paper Software.

Programmieren <strong>in</strong> C<br />

E<strong>in</strong>führung <strong>in</strong> C<br />

Programmieren <strong>in</strong> C<br />

E<strong>in</strong>führung <strong>in</strong> C<br />

Übersetzen und Starten<br />

Übersetzungsprozess<br />

Separate Übersetzung <strong>in</strong> e<strong>in</strong>e ausführbare Datei<br />

Was passiert bei der Übersetzung im Detail<br />

Datei hello.c mit Editor erstellen<br />

• Emacs oder vi empfohlen<br />

• Geany oder auch IDE möglich<br />

Übersetzen mit C-Compiler<br />

• (g)cc, (GNU) C Compiler<br />

• Erzeugt ausführbares Programm,<br />

Datei a.out<br />

Compiler-Optionen -g -ansi -Wall -o<br />

• Debug, ANSI-Standard, Warnung<br />

• Ausgabedatei ist hello<br />

Ausführen<br />

• Aufruf Name erzeugter Datei<br />

• Auf Kommandozeile<br />

• ./ davor wenn . nicht im Pfad<br />

$ ls -l hello.c<br />

-rwx------ 1 pb pb 80<br />

30. Aug 13:33 hello.c<br />

$ gcc hello.c<br />

$ ls -al a.out<br />

-rwx------ 1 pb pb 4699<br />

30. Aug 13:46 a.out<br />

$ ./a.out<br />

Hello C World<br />

$ gcc -g -ansi -Wall hello.c -o hello<br />

$ ls -l ./hello<br />

-rwx------ 1 pb pb 5871<br />

30. Aug 13:47 ./hello<br />

$ ./hello<br />

Hello C World<br />

Präprozessor<br />

• Fügt Quelltexte/Include-Dateien e<strong>in</strong>, Konvention<br />

Endung .h (Header), sucht Dateien <strong>in</strong> bestimmten<br />

Verzeichnissen /usr/<strong>in</strong>clude/...,<br />

• Textuelle Ersetzungen<br />

C-Compiler<br />

• Übersetzt C-Quellcode <strong>in</strong> hardwarespezifische<br />

Assemblersprache<br />

• Optimierungen<br />

Assembler<br />

• Übersetzt Assembler <strong>in</strong> Masch<strong>in</strong>ensprache<br />

L<strong>in</strong>ker<br />

• B<strong>in</strong>det externen Objektcode e<strong>in</strong> (z.B. pr<strong>in</strong>tf), sucht<br />

Bibliotheken <strong>in</strong> bestimmten Verzeichnissen<br />

/usr/lib/...,<br />

hello.c<br />

gcc hello.c -o hello<br />

Präprozessor<br />

C-Compiler<br />

Assembler<br />

L<strong>in</strong>ker<br />

hello<br />

Prof. Dr. Peter Barth (HS-Rhe<strong>in</strong>Ma<strong>in</strong>) Programmieren 3 29. Januar 2013 13 / 326<br />

Prof. Dr. Peter Barth (HS-Rhe<strong>in</strong>Ma<strong>in</strong>) Programmieren 3 29. Januar 2013 14 / 326<br />

Programmieren <strong>in</strong> C<br />

E<strong>in</strong>führung <strong>in</strong> C<br />

Programmieren <strong>in</strong> C<br />

E<strong>in</strong>führung <strong>in</strong> C<br />

Makefiles<br />

Automatisierung des Übersetzungsprozesses ...<br />

Variablen<br />

1 Def<strong>in</strong>ition: CC=gcc<br />

• Verwendung mit $(CC)<br />

• Variablennamen per Konvention<br />

GROßBUCHSTABEN<br />

Regeln<br />

4 Ziel hello und Abhängigkeit hello.c<br />

• Um Ziel zu machen, schaue ob<br />

Abhängigkeit neuer ist als hello, wenn<br />

ja dann mache<br />

5 Führe Befehl (mit Ersetzungen) aus<br />

Verwenden auf Kommandozeile<br />

• Auf Kommandozeile make<br />

• Ziel als Parameter möglich<br />

Makefile<br />

1 CC=gcc<br />

2 CFLAGS=-ansi -g -Wall<br />

3<br />

4 hello: hello.c<br />

5 $(CC) $(CFLAGS) hello.c -o hello<br />

$ make<br />

gcc -ansi -Wall hello.c -o hello<br />

$ ./hello<br />

Hello C World<br />

Makefiles<br />

... etwas ausführlicheres Beispiel, Details im Informationsblatt<br />

Ziel all<br />

• Erstes Ziel ist Default-Ziel<br />

• Konvention all<br />

Trennen Compile/L<strong>in</strong>k<br />

• Erst Objekt-Datei mit -c<br />

• Dann B<strong>in</strong>den/L<strong>in</strong>ken<br />

Beispiel<br />

• Mache rot13 falls rot13.o neuer<br />

• Mache rot13.o falls rot13.c ...<br />

Ziel clean<br />

• Konvention, zum Löschen aller<br />

erzeugten Dateien<br />

• Ohne Abhängigkeit (Datei clean nicht<br />

vorhanden), immer ausführen<br />

Makefile<br />

1 CC=gcc<br />

2 CFLAGS=-ansi -g -Wall<br />

3<br />

4 all: hello rot13<br />

5<br />

6 hello: hello.c<br />

7 $(CC) $(CFLAGS) hello.c -o hello<br />

8<br />

9 rot13.o: rot13.c<br />

10 $(CC) -c $(CFLAGS) rot13.c -o rot13.o<br />

11<br />

12 rot13: rot13.o<br />

13 $(CC) $(CFLAGS) rot13.o -o rot13<br />

14<br />

15 clean:<br />

16 # - vor Befehl, ignoriere Fehlschlag<br />

17 -/b<strong>in</strong>/rm -f hello rot13 rot13.o<br />

Prof. Dr. Peter Barth (HS-Rhe<strong>in</strong>Ma<strong>in</strong>) Programmieren 3 29. Januar 2013 15 / 326<br />

Prof. Dr. Peter Barth (HS-Rhe<strong>in</strong>Ma<strong>in</strong>) Programmieren 3 29. Januar 2013 16 / 326

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!