Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)

Similar presentations


Presentation on theme: "Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)"— Presentation transcript:

1 Compilation and Debugging 101

2 Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)

3 Preprocessor A single-pass program that Include header files Expands macros Control conditional compilation Remove comments

4 #include directive add exp.. #include foo.h Include the file foo.h, from current directory #include Include the file stdio.h from the standard library directory (part of compiler installation)

5 Modules & Header files Complex.c struct Complex { double _real, _imag; }; Complex addComplex(Complex, Complex); Complex subComplex(Complex, Complex);... complex.h #include #include complex.h // implementation Complex addComplex(Complex a, Complex b) { … complex.c #include complex.h int main() { Complex c; … MyProg.c

6 Header files Header file contain Definition of data types Declarations of functions & constants That are shared by multiple modules. #include directive allows several modules to share the same set of definitions/declarations

7 #define directive #define FOO 1 … int x = FOO; is equivalent to … int x = 1;

8 #define with arguments #define square(x) x*x b = square(a); is the same as b = a*a;

9 #define -- cautions #define square(x) x*x b = square(a+1); c = square(a++); Is it what we intended? b = a+1*a+1; // b = 2*a+1; c = a++*a++; // c = a*a; a+=2;

10 #define #define directive should be used with caution Alternative to macros: Constants enum { FOO = 1; }; or const int FOO = 1;

11 #if directive Allows to have conditional compilation #if defined(DEBUG) // compiled only when DEBUG exists printf(X = %d\n, X); #endif

12 #if – header safety Complex.h: struct Complex { … MyStuff.h: #include Complex.h Main.c #include MyStuff.h #include Complex.h Error: Complex.h:1: redefinition of `struct Complex'

13 #if – header safety Complex.h (revised): #if !defined(COMPLEX_H) #define COMPLEX_H struct Complex { … #endif Main.c: #include MyStuff.h #include Complex.h // no error this time

14 Preprocessor We can test what the preprocessor does > gcc –E hello.c will print the C code after running preprocess

15 Compilation Takes input C-code and produces machine code (object file) –gcc –c Main.c –Main.c Main.o The object file does not contain all external references –It leaves names, such as printf, addComplex, etc. as undefined references

16 Linking Combines several object files into an executable file –No unresolved references Main Preprocessor Compiler Complex.c Complex.o Main.c Main.o Linker libc.a

17 Link errors The following errors appear only at link time Missing implementation > gcc -o Main Main.o Main.o(.text+0x2c):Main.c: undefined reference to `foo' Duplicate implementation > gcc -o Main Main.o foo.o foo.o(.text+0x0):foo.c: multiple definition of `foo' Main.o(.text+0x38):Main.c: first defined here

18 assert.h #include // Sqrt(x) - compute square root of x // Assumption: x non-negative double Sqrt(double x ) { assert( x >= 0 ); // aborts if x < 0 … If the program violates the condition, then assertion "x >= 0" failed: file "Sqrt.c", line 7

19 assert.h Important coding practice Declare implicit assumptions Sanity checks in code Check for violations during debugging/testing Can we avoid overhead in production code?

20 assert.h #undef assert // procedure that actually prints error message void _assert(char* file, int line, char* test); #ifdef NDEBUG #define assert(e) ((void)0) #else #define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e)) #endif


Download ppt "Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)"

Similar presentations


Ads by Google