Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 - Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS.

Similar presentations


Presentation on theme: "CS 311 - Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS."— Presentation transcript:

1 CS 311 - Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS 311 - Operating Systems I

2 Compiling programs using gcc GCC stands for GNU Compiler Collection. Used to compile different programming languages like C, C++, Java, Fortran Contains a large set of options. Frequently used options to compile C programs – -c, -g, -lm, -o, -Wall, -ansi – -c – compiles the file to create an object file (.o) but does not perform linking. – -o – generates executable file – Ex: gcc –c file.c – gcc file.o –o exec_file Lecture 112CS 311 - Operating Systems I

3 Compiling using gcc More options – -lm – compile programs that uses mathematical functions – -g – Generate symbolic info to be used with gdb – -Wall – generate all warnings – -ansi – use strict ANSI coding standards for C To link multiple files together – Ex. gcc –c file.c-g –Wall #creating.o for file1 – gcc –c file2.c –g –Wall#creating.o for file2 – gcc file1.o file2.o –o exec_file#linking both object files Lecture 11CS 311 - Operating Systems I3

4 Archiving modules Useful when a project has several hundred object modules. Archiving modules helps us to categorize the modules. Archived file names must have “.a” extension. Archives can be used with gcc. Only the needed object modules are linked automatically as necessary, Archiving utility in UNIX – ar Lecture 11CS 311 - Operating Systems I4

5 Archive utility - ar “ar” allows us to create and manipulate archives. Syntax: ar key archiveName.a {filename}* – Key d – delete a file from archive q – appends a file to the end of archive even if its present r – adds/replaces a file to the archive s – builds an index for archive for faster access t – displays archive’s contents x – copies a list of files from archive to current directory v – generates verbose output – Ex: ar r myArchive.a file1.o file2.o #add 2 files to myArchive.a gcc myArchive.a –o file_exec Lecture 11CS 311 - Operating Systems I5

6 Indexing archives If a module A requires a function from module B then B should be present before A in the archive. To workaround this out-of-order problem, index the archives using ‘s’ key – Ex: ar s myArchive ‘s’ key creates an index to the archive which makes access faster. Lecture 11CS 311 - Operating Systems I6

7 Using Makefiles Makefiles are helpful in managing dependencies. Generally such files are named as “GNUMakefile”, “makefile” or “Makefile” To execute makefiles use the utility “make”. “make” will automatically look for “GNUMakefile”, “makefile” or “Makefile”. To use your own filename for makefiles use ‘–f’ option to execute it. – Ex: make –f myFile Lecture 11CS 311 - Operating Systems I7

8 Makefile rules Use of makefile – Ex: Assume we have these files heap.h, heap.c, mainHeap.c – Without using makefiles we compile the code like this gcc –c heap.c gcc –c mainHeap.c gcc mainHeap.o heap.o -o heap_exec – Changing any of the files will result in compiling the program all over again. (tedious when there are large number of files) – Using “make” the task is simplified. Rules for creating makefile – Simplest form of makefile Targetlist: dependencylist commandlist(commandlist should have a tab-space before it) Lecture 11CS 311 - Operating Systems I8

9 Creating makefiles Makefile for the previous example will be heap.o: heap.c heap.h gcc –c heap.c -g –Wall mainHeap.o: mainHeap.c heap.h gcc –c mainHeap.c -g –Wall heap_exec: heap.o mainHeap.o gcc heap.o mainHeap.o -o heap_exec We can execute this by simply typing “make”. Makefile compiles only the file that has been changed. Lecture 11CS 311 - Operating Systems I9

10 More on makefiles Using macros CFLAGS = -g –Wall#defining a macro OBJECT = file1.o file2.o file3.o file4.o SOURCE = file1.c file2.c file3.c file4.c heap.o: heap.c heap.h gcc –c heap.c $(CFLAGS)#using macro Maintaining archive myArchive.a: $(OBJECT)#creating an archive ar –r myArchive.a $(OBJECT) heap: heap.o myArchive.a(file2.o file1.o) #using files from archive Lecture 11CS 311 - Operating Systems I10

11 Debugging using gdb To debug a program gdb utility provides – Running and listing program – Setting breakpoints – Examining variable values – Tracing execution To run gdb use the command ‘gdb’ from terminal. Useful commands with gdb – help – list all commands – list – display the program – break #linenum – setup breakpoints – data – to watch the contents of variables, arrays – run – to execute the program Lecture 11CS 311 - Operating Systems I11


Download ppt "CS 311 - Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS."

Similar presentations


Ads by Google