GCC(GNU Compiler Collection) Tool Kit
Total Page:16
File Type:pdf, Size:1020Kb
GCC(GNU Compiler Collection) Tool Kit Linux Users Group, JMI An overview of GNU Compiler Collection and its use for compiling C, C++ By: Saleem A. Ansari The Free Software Compiler An Introduction to GCC Virtually all other open software is based on it at some level or another. Even other languages, such as Perl and Python, are written in C, which is compiled by the GNU compiler. This piece of software is more fundamental to the entire free software movement than any other. In fact, without it or something like it, there would be no free software movement. Lin- ux is possible because of GCC. GCC is a product of the GNU Project. The fundamental language of GCC is C. The en- tire compiler system began as a C compiler and, over time, the other languages were added to it. C++ Was the First Addition. Now can compile C++, Objective-C, Java, Ada, Fortran ... The GCC set of compilers runs on many plat- forms. We can do multi-platform compilation us- ing the same machine. (Alpha, HPPA, Intel x86, MIPS, PowerPC, Sparc) GCC Components cc1: The actual C compiler. cc : A version of gcc that sets the default lan- guage to C and automatically includes the standard C libraries when linking. cc1plus : The actual C++ compiler. g++ / c++ : A version of gcc that sets the default language to C++. jc1: The actual Java compiler. gcj The driver program used to compile Java. gcc: The driver program. GCC Components contd. as : The GNU assembler. It is really a family of assemblers because it can be compiled to work with one of several different platforms. This pro- gram is part of the binutils package. gdb : The GNU debugger, which can be used to examine the values and actions inside a pro- gram while it is running. Other tools : gprof (profiler), ld(linker), ar(archive), make, nm, objcopy, objdump, ranlib, strip ... Developing Software using GCC You need a text editor: gedit, kedit, vi emacs, joe, nedit etc. You need to learn atleast one of the lan- guages supported by GCC: C, C++, Java, Fortran etc. You need the GCC Toolkit Installed on the system itself get-set-go... The famous C program /*hello.c*/ #include<stdio.h> int main() { printf(ªHello GCC\nº); return 0; } Compilation: cc -c hello.c cc -o hello hello.o ./hello The famous program in C++ /*hello.cpp*/ #include<iostream> using namespace std; int main(void) { cout << ªHello GCCº << endl; return 0; } Compilation: c++ -c hello.cpp c++ -o hello hello.o ./hello Command Line Options -c compile and produce object code -o name of translated code file -l specify library -I specify include directory -Wall show all errors -std=__ assume the specified standard -v give verbose output -s, -S result in assembly code production -O1, O2, -O3 Optimization Levels Yet another simple example. Illegal memory access!! #include<stdio.h> int main() { char *str=”abc”; str[0]=’d’; str[1]=’e’; str[2]=’f’; puts(str); return 0; } Here comes the debugger Use the GCC command line switch -g or -ggdb to incorporate debugging information into the object code Invoke the gdb and fire!! Multiple Files: A simple example /*mystring.c*/ #include<string.h> /*mystring.h*/ int palindrome(char s[]) int palindrome(char s[]); { int l=strlen(s)-1; int i=0; while(i<l) if(s[i++]!=s[l--]) return 0; return 1; } /*mystringtest.c*/ continued... #include<stdio.h> #include "mystring.h" int main() { char str[50]; puts("Enter a string:"); gets(str); if(palindrome(str)) printf("Its a palindrome"); else printf("Its not a palindrome"); return 0; } MAKE indeed is a boon MAKEFILE -------------------------------------------------------------------------- CC=gcc CFLAGS=-Wall -g all: mystring.a test test: mystring.a $(CC) $(CFLAGS) -c mystringtest.c $(CC) $(CFLAGS) -o test mystringtest.o mystring.a clean: rm -f test mystringtest.o mystring.o mystring.a mystring.a: mystring.o ar cvr mystring.a mystring.o ranlib mystring.a mystring.o: $(CC) $(CFLAGS) -c mystring.c Compiling a complete software MPlayer as an example demonstration For further information Manpages of gcc, make, gdb, nm, obj- dump, objcopy... Info pages of binutils Thats all for now! Thanx.