Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programs – Preprocessing, Compilation and Linking

Similar presentations


Presentation on theme: "Programs – Preprocessing, Compilation and Linking"— Presentation transcript:

1 Programs – Preprocessing, Compilation and Linking
CS/COE 0449 (term 2174) Jarrett Billingsley

2 Class announcements I don't know how long it'll take Sam to grade your exams! But he's pretty quick. How was project ooooone? We can look over the ideas briefly. We're "done" with C... But we'll still be learning new things about it. Oh yeah, VLAs and alloca()! 2/7/2017 CS/COE term 2174

3 The compilation toolchain
2/7/2017 CS/COE term 2174

4 Well... as you might expect, it's not entirely as simple as that.
You've seen this before! #include <stdio.h> int main() { printf("Hello!\n"); return 0; } hello.c Linker hello.obj helper.obj dogs.obj libc.a/lib hello.exe Compiler hello.obj Well... as you might expect, it's not entirely as simple as that. 1/10/2017 CS/COE term 2174

5 Nothing to lose but your chains this is the second communist manifesto slide title that's come to mind in two days what the hell The compilation toolchain is the sequence of programs that you use to go from programming-language code to an executable file. There are a few more steps on each end, of course. hmm Preprocessor Processes lines that start with #, like #include cool! Compiler Turns C code into machine code Linker Turns pieces of programs into an executable Loader Loads the executable into RAM and runs it (the preprocessor is pretty much unique to C/C++.) 2/7/2017 CS/COE term 2174

6 The Preprocessor 2/7/2017 CS/COE term 2174

7 In the olden days... Compilers were really, really dumb and simple programs. An easy hack for extending the capabilities of the compiler without adding a bunch of extra features to it was to use a preprocessor – a text processing system that could perform textual substitution. That's literally all it does. It copies and pastes text, automatically. The C preprocessor – a program called cpp (not associated with C++!) – is the first step your code goes through during compilation. Let's use gcc -E to see the results! All preprocessor directives (things that start with #) have to be written at the beginning of the line. 2/7/2017 CS/COE term 2174

8 The #include directive
You've been using #include as a rough synonym for Java's import. What it really does... is copy and paste the entire contents of the given filename right there into your .c file. #include <filename.h> looks for filename.h in the "standard include paths" for the system. (This will be the standard library.) #include "filename.h" looks for it in the current directory and/or any other paths you specify when running gcc. What's in these .h files anyway...? 2/7/2017 CS/COE term 2174

9 Header files (*.h) Header files are usually paired with a corresponding C file. E.g. "user.h" and "user.c" make up two "halves" of the same idea. The header file contains the public interface. Mainly, function prototypes, global variable declarations, constants, enums, #defines.... (we'll get to that) The C file contains the private implementation. It has all the implementations of the functions that are prototyped in the header, as well as other private functions. 2/7/2017 CS/COE term 2174

10 The #define directive #define NUM_ITEMS 1000 #define int float
#define is a weird beast. In its simplest form it lets you make constants through textual replacement. #define NUM_ITEMS 1000 Now whenever you write NUM_ITEMS, the preprocessor will replace it – textually! – with 1000. This is not a variable! You can't &NUM_ITEMS, you can't NUM_ITEMS = 2000, you can't do anything with it. #define int float uh um :V 2/7/2017 CS/COE term 2174

11 #define with parameters...
#define TEST_BIT(v, n) ((v) & (1 << (n))) This is a preprocessor macro. It looks like a function. You can write it like you're calling a function. But again, it's all text replacement. Making and using macros is a bit of an art. It's easy to get overambitious... But you can do some pretty cool things with them. Maybe stay away from making your own macros for now ;o 2/7/2017 CS/COE term 2174

12 Conditional compilation
One important feature the preprocessor lets us do is choose which code to compile, based on some condition. #ifdef SOME_OPTION // code path 1 #else // code path 2 #endif #ifdef SYMBOL tests if SYMBOL has been #defined. If it has, it does the first part, otherwise it does the else (if there is one). Again, this is all textual... you can do really weird things with this. 2/7/2017 CS/COE term 2174

13 The compiler and linker
2/7/2017 CS/COE term 2174

14 Object files You already know what the compiler does!
For every .c file the compiler takes in, it produces one object file. Doesn't matter how many headers it includes. *.c -> *.o (you can also see the assembly language with gcc -S.) The gcc -c option will output the object files. What's in these files...? Let's try objdump -x on one. WOAH WHAT 2/7/2017 CS/COE term 2174

15 Taking it apart An object file has several sections or segments.
The text segment contains not text! Your program code. Yeah, I know. The data segment contains initialized global variables ready to be loaded into memory. Closely related are rodata (read-only data for constants – like string literals!) and bss (says what areas of memory to fill with 0) Next there's the symbol table. Remember, "symbol" = "name". Symbols tell the linker know what functions and variables exist in which object files, so it can link them together by name. Lastly there are relocations for both code and data. When creating the executable, the linker uses these to change the memory addresses of pieces of code and data safely. 2/7/2017 CS/COE term 2174

16 Puzzle pieces from the clay
Object files are an incomplete part of a whole, like a puzzle piece. This is an archive – several objects bundled together. test.o show_message main libc.a It has (or exports) these two functions. printf libc.a exports printf! Now we just have to put the pieces together. printf? But it doesn't have printf. It imports printf. Where is it? 2/7/2017 CS/COE term 2174

17 The adventure of link..er
The linker's job is to put all the pieces – all the object files and libraries – together, to create a final executable. Well, mostly. If you gather up all the code needed for an executable and put it in one file, this is known as static linking. All the pieces are put together! To run the program, you just have to copy it into memory. But there's another technique... 2/7/2017 CS/COE term 2174


Download ppt "Programs – Preprocessing, Compilation and Linking"

Similar presentations


Ads by Google