Programming Tools Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 1
Session Objectives To understand the process of compilation To be aware of provisions for data structuring provided by a C programming language To understand scope, memory organization and data representation of C data types To understand the tools used to implement the algorithms and data structures: Editor VIM Compiler GCC Debugger GDB 2
Session Objectives To understand how to create a library To understand the how to use a library 3
Compilation Process Step 1: Pre-processing All statements starting with '#' are evaluated #define #include #ifdef #undef #else Pre-processor also adds notes to the file Step 2: Lexical and Semantic analysis Here is where we get all the errors and wanrning Step 3: Intermidiate language conversion C code is converted to equivalent assembly code 4
Compilation Process Step 4: Assembly to an object file that has the equivalent achitecture specific operational codes Step 5: Static linking with libraries and bootstrap code Here is where executable is generated Demo of this process using gcc (GNU C Compiler) 5
Process Segments and Variable Scope What we already know: Type Scope Lifetime Default Value Global/Extern Full Program As long as process executes 0 Local Within Function Function Execution Time Garbage Static Within Function As long as process executes 0/initial Why Register and how exactly Within are these Function restrictions Execution imposed? Garbage Function Time 6
Segments The program, when it is loaded in to memory for execution along with data that the operating system stores in the memory is known as process. The process is divided in to segments each with a purpose:.text or Code segment Stores all the operational codes for all functions.global or Data Segment Stores global variables.bss (Bit Set Start) part of Data Segment Stores uninitialized global variables In a program(the.out file), only its size is given Initialized to zero when the memory is allocated 7
Segments.rodata (Read only Data) part of Data Segment Stores constants suct as strings etc For example: Char a[]= Hello, world! A stores address and is located in its corrosponding segment Hello, world!\0 is stored in.rodata Stack Function local variables are stored here A Frame corrosponding to memory requirements of a function's local variables (including parameters) and its return address is created on function call This Frame is pushed in to stack It is removed when the function returns after execution No initialization happens here 8
Segments Heap Dynamically allocated memory is here When you call malloc, calloc or realloc, memory is created here and the address of this memory is returned When you call free, this memory is 'freed' i.e. It is released to the operating system Stack Free Memory Stack/Heap Whatever value was there(garbage) Heap Data Code Data Initialized to Zero Code Only opcodes here 9
Primitives and Data Representation C primitives Numeric Integers char 1 byte signed int 4 byte (in gcc 32), signed (always 1 word) short 2 bytes (in gcc 32), signed long 4 bytes (in gcc 32) long long 8 bytes (in gcc 32) 10
C Primitives C primitives Numeric floating point float 4 byte signed double 8 byte (in gcc 32), signed (always 1 word) long double 12 bytes (in gcc 32), signed Pointer Size of address bus 8 bytes in 64 bit computer» Even if compiler is 32 bit 4 bytes in 32 bit computer 11
Data Representation Integer types Signed MSB is sign bit 0 means +'ve and 1 means -'ve +'ve integers are in direct binary forms -'ve integers are in 2's complement notation Unsigned All bits are used for representation of data Directly represented in binary forms Pointer types Unisgned direct binary representation Floating point numeric types Split as exponent and Mantissa Are signed Layout is based on compiler used 12
Derived Data Types Arrays The variable of an array points to starting address in memory of the array Print %p of variable to get what it represents sizeof(array variable) returns total size of array Structures The variable points to starting address of structure instance in memory sizeof(structure variable) returns word aligned size of the total structure variable in memory Unions The variable points to starting address of union instance in memory sizeof(union variable) is maximum variable size in it 13
GNU Tool Chain and Linux Bash Bourne Again Shell Linux Kernel Man Help(Manual Pages) Debian Operating System VIM Visual Editor Improved (Wheezy) GCC GNU C Compiler GLibC GNU C Library BinUtils Assembler, Linker,Archiver,etc. GDB GNU Debugger Make Automation of building 14
Shell - bash To get to bash press Windows button or click Activities Type terminal Select Terminal 15
binutils ar Similar to DLL files in Windows Creates a lib archive ar cr lib<name>.a <file1>.o <file2>.o... objdump Shows contents of object/executable files Display segments objdump -h <file> Notice.text,.data,.rodata and.bss Try nm -a <file> 16
bash Commands Create your directory mkdir <directory name> Change directory cd <directory>.. Delete directory with contents rm -rf <directory name> See contents of directory ls -l Find a file locate <filename> Update available files (as root) updatedb Open/create a file for editing vim <filename> Remember Unix/Linux uses '/' not '\' Case sensitive Root directory Not c:\ / If you want to install software Call us 17
Getting Help Man command To get help on commands man <command> To get help on library functions man 3 <function> Press q to quit Use arrow kets to navigate Press / and type a string to search first occurance Press n to go to next occurance 18
Visual Editor - Improved Checking if vim is installed vim --version Opening or Creating a new file vim <filename> Three modes Normal Mode Vi opens in this mode Press Esc key in any mode to get here Insert mode Press i (note the lower case) to get here from normal mode Command line mode Press : from normal mode to get here Visual Mode Press v in normal mode to get here In Insert mode, vi works just as any other text editor 19
gcc Checking if gcc is installed gcc --version Compiling a C file gcc <file>.c -o <file> Options -I <Folder> Specify folder having header files -L <Folder> Specify folder having library files -l<library> Compile and link with library Define a flag during compilation gcc -D <flag> <file>.c -o<file> Stop compilation after: Pre processing gcc -E <file>.c -o<file>1.c Assembly gcc -S <file>.c -o<file>.s Object file (OP Codes) gcc -c <file>.c -o<file>.o Linking (executable) gcc <file>.c -o<file> 20
glibc It is the library that provides implementation code (opcode) all standard functions like printf, scanf, malloc, etc. To check if it is installed locate libc.so.6 Type the full path of the file libc.so.6 to execute it and get the version Header files for stdio, stdlib that declare the functions are available in /usr/include/ 21
gdb When compiling, always compile with flag -ggdb gcc -ggdb <file>.c -o <file> To execute gdb./<file> You will see a Reading symbols from<path of file>...done. (gdb) This is GDB prompt where you can type commands To list contents of file (gdb) list <file>.c:<line number> For example:(gdb) list hello.c:11 22
gdb To set a break point (gdb) break <file>.c:<linenumber> For example: (gdb) break hello.c:13 To start execution (gdb) start To print a variable (gdb) print <variable name> For example: (gdb) print a To move up in stack (gdb) up To move down (gdb) down To continue execution (gdb) continue To quit (gdb) quit Press 'y' For help (gdb) help 23
Visual Editor - Improved Normal Mode Used to navigate between modes Can scroll text Can delete lines with delete Press y twice to copy a line (Yanking) Press p to paste at cursor Press d twice to delete a line Press i to edit text Press v for Visual mode Press : for commandline Press Esc to get here Command line mode Press : from normal mode to get here Press w<enter> to write to file Press wq<enter> to write and exit Press q!<enter> to exit without saving Type help to get help Visual Mode Move arrow keys to select Press <enter> to copy 24
Creating a Library Create a file add.c with: int add (int a, int b) { return a+b; } Create a file sub.c with: int subtract(int a, int b) { return a b; } Create a file mpy.c with: int multiply (int a, int b) { return a*b; } Create a file div.c with: int divide(int a, int b) { return a / b; } 25
Creating a Library Create a file calculator.h with: int add (int, int); int subtract(int, int); int multiply(int, int); Int divide (int, int); Compile as follows: gcc -c add.c -o add.o gcc -c sub.c -o sub.o gcc -c mpy.c -o mpy.o gcc -c div.c -o div.o Creating the library: ar cr libcalculator.a add.o sub.o mpy.o div.o 26
Using a Custom Library Create a file testcalculator.c with: #include <stdio.h> #include calculator.h int main (int argc, char *argv[]) { int a = 14, b= 22; printf ( %d %d\nsum: %d\n,a,b,add(a,b)); printf( Difference: %d\n,subtract(a,b)); printf( Product: %d\n,multiply(a,b)); printf( Ratio: %d\n,divide(a,b)); } 27
Using a Custom Library Move libcalculator.a and calculator.h to current directory Compile as : gcc -I./ -L./ testcalculator.c -o testcalculator -lcalculator -I tells compiler all custom header files are in current directory -L tells compiler all custom libraries are in current directory -lcalculator tells compiler to link with libcalculator.a file -o tells the compiler to create the output file as testcalculator.out and not a.out Now, we can run the executable as:./testcalculator<enter> Demo including GDB debugging of this library 28
Self Study For further understanding, answer the following questions What are memory holes? What are bit fields? What are memory leaks? How do you find and fix them? (Need to understand how malloc and free work for this) May need valgrind What is typedef? How is it used? What is #define What are #if, #ifdef, #else, #elif and #endif * Do not just gather information, experiment and understand 29
Summary Process of compiling code involves multiple steps Programming languages arrange memory as per definition of primitives and provide constructs to derive new data types Data in process is stored in different segments and the programmer must be aware of this while writing code GNU Tool chain provides all tools needed to develop a complete application. 30
Summary Process of compiling code involves multiple steps Programming languages arrange memory as per definition of primitives and provide constructs to derive new data types Data in process is stored in different segments and the programmer must be aware of this while writing code GNU Tool chain provides all tools needed to develop a complete application. 31
Summary C is written with functional programming in mind C programs are not always single file programs C is developed for modular programming such that different modules work as libraries for the main application user interface Libraries can be created and used in projects 32