Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu...
Transcript of Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu...
![Page 1: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/1.jpg)
KYC - Know your compiler
Introduction to GCC
![Page 2: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/2.jpg)
The Operating System
Computer Hardware
Operating System Kernel
General Purpose or Application Specific Software
User User 1
User 2
![Page 3: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/3.jpg)
What is GCC?GCC is the GNU Compiler CollectionProvides Compilers for:• C• C++• Objective C• Fortran• ADA• Java
![Page 4: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/4.jpg)
Basic steps in compilation
• Pre-Process directives like #include• Compilation of the C Code to generate the
assembly• Assembly Code to object file generation• Link the object code to generate the
executable
![Page 5: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/5.jpg)
Possible Outputs for C/C++ Programs
• Complete processing:– Executable files– Libraries
• Partial processing:– C/C++ Source files after preprocessing only– Assembly code corresponding to a source file– Object code corresponding to a C/C++ file
![Page 6: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/6.jpg)
Step 1: Compiling a simple C Program
• Syntax: gcc <filename.c>
user@ws$ gcc HelloWorld.c
Output: An executable called a.out To run: ./a.out
user@ws$ ./a.outHello World user@ws$
![Page 7: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/7.jpg)
Step 2: Compiling a simple C++ Program
• Syntax: g++ <filename.cpp>
user@ws$ g++ HelloWorld.cpp
Output: An executable called a.out To run: ./a.out
user@ws$ ./a.outHello World user@ws$
![Page 8: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/8.jpg)
Step 3: Providing the executable name• Extra option: -o• Syntax: gcc <filename.c> -o <outputname>
user@ws$ gcc HelloWorld.c -o myapp
Output: An executable called outputname
To run: ./outputname
user@ws$ ./myappHello World user@ws$
![Page 9: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/9.jpg)
Multi-file Programs
Why create multi-file programs?• Manageability• Modularity• Re-usability• Abstraction
![Page 10: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/10.jpg)
General abstraction used in Multi-file Programs
• Components:– Header files– Implementation Source files– Application source file ( contains the main
function)
![Page 11: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/11.jpg)
Header FilesContents:• Pre-processor directives and macros• Constant declarations• Type declarations (enum, typedef, struct, union etc)• Function prototype declarations• Global variable declarations• May also contain static function definitions
Example: HelloWorld.h
#ifndef _HELLOWORLD_H_#define _HELLOWORLD_H_
typedef unsigned int my_uint_t;extern void printHelloWorld();extern int iMyGlobalVar;...#endif
![Page 12: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/12.jpg)
Implementation Source FilesContents:• Function body for functions declared in corresponding header files• Statically defined and inlined functions• Global variable definitions
Example: HelloWorld.c
#include <stdio.h>#include “HelloWorld.h”
int iMyGlobalVar;void printHelloWorld(){
iMyGlobalVar = 20;printf(“Hello World\n”);return;
}
![Page 13: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/13.jpg)
Application Source FileContents:• Function body for the main function• Acts as client for the different modules
Example: app.c#include <stdio.h>#include “HelloWorld.h”
int main(){
iMyGlobalVar = 10;printf(“%d\n”, iMyGlobalVar);printHelloWorld();
printf(“%d\n”, iMyGlobalVar);return 0;
}
![Page 14: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/14.jpg)
Correlation between componentsModule
Header File
Module Implementation
FileApplication
File
#include#include
ObjectFile
ObjectFile
Link
ExecutableFile
Compile Compile
Other includesOther includes
Other libraries / object files
![Page 15: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/15.jpg)
Step 4: Compiling a simple multi-file program
• Syntax: gcc <file1.c> <file2.c> ... -o filename
Example:
user@ws$ gcc HelloWorld.c app.c -o my_appuser@sw$ ./my_app10Hello World20user@ws$
![Page 16: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/16.jpg)
Step 5: Multi-step compilation and linking
• Steps:– Compile source files to object files– Link multiple object files into executables
• Compilation to object files– Special Option: -c – Syntax: gcc -c <filename(s).c>
• Link multiple files into the final executables– Syntax: gcc <filename1.o> <filename2.o> [-o output]
![Page 17: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/17.jpg)
Step 5: Continued• Example:
user@ws$ gcc -c HelloWorld.c user@ws$ gcc -c app.cuser@ws$ gcc HelloWorld.o app.o -o my_appuser@sw$ ./my_app10Hello World20user@ws$
![Page 18: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/18.jpg)
Step 6: Including files from other directories
• Special Option: -I<directory_name>• Syntax:
gcc -I<directory1> -I<directory2> <filename(s).c>
• Example:
user@ws$ cd HelloWorld/srcuser@ws$ gcc -c -I../../HelloWorld/include HelloWorld.cuser@ws$ cd ../../app/srcuser@ws$ gcc -c -I../../HelloWorld/include main.c user@ws$ cd ../../user@ws$ gcc HelloWorld/src/HelloWorld.o app/src/main.o -o my_appuser@ws$ ./my_app
![Page 19: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/19.jpg)
Object Libraries• Libraries contain pre-compiled object codes• Are of 2 types:
– Statically Linked: Object codes are linked into and placed inside the executable during compilation.
• Name format: lib<name>.a
– Dynamically Linked: Object code is loaded dynamically into memory at runtime.
• Name format: lib<name>.so
![Page 20: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/20.jpg)
Statically Linked Libraries
• Consists of a set of routines which are copied into a target application
• An archive of object files• Object code corresponding to the required
functions are copied directly into theexecutable
• Library format is dependent on linkers• Increases executable size
![Page 21: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/21.jpg)
Dynamically Linked Libraries• Contains position independent code for
different functions• Executable code is loaded by the loader at
runtime• The symbol table in the library contains blank
addresses which are filled up later by theloader
• Increases reuse• Decreases program size and execution time
![Page 22: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/22.jpg)
Step 7: Linking with external libraries
• Static linking: Link to libname.a– Special option: -static and -l– Syntax: gcc -static <filename(s)> -lname
Dynamic linking: Link to libname.so− Special option: -l− Syntax: gcc <filename(s)> -lname
user@ws$ gcc -static math_test.c -lm
user@ws$ gcc math_test.c -lm
![Page 23: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/23.jpg)
Step 8: Linking to a library at non-standard path
• Special option: -L<path>• Syntax:
gcc <filename> -l<name> -L<path>
user@ws$ gcc math_test.c -lm -L/usr/lib
![Page 24: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/24.jpg)
Step 9: Building Static libraries
Required external tools: ar, ranlib1.Create object files for the source codes2.User ar to create the archives with names of
the form: lib<libraryname>.a3.Use ranlib to generate the index within the
library
![Page 25: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/25.jpg)
Step 9: Continued• Example
user@ws$ gcc -c HelloWorld.cuser@ws$ ar rcs libHW.a HelloWorld.ouser@ws$ ranlib libHW.auser@ws$ gcc app.c -lHW -L. -o my_appuser@ws$ ./my_app10Hello World20user@ws$
![Page 26: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/26.jpg)
Step 10: Building Dynamically linked libraries
• Requirements: Object code needs to be position independent• Steps:
1.Compile sources in a Position Independent manner• Option: -fPIC
2. Combine objects to create shared library:• Option:
-shared -W1,-soname,lib<name>.so.<version>
![Page 27: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/27.jpg)
Step 10: Continued• Example
user@ws$ gcc -c -fPIC HelloWorld.cuser@ws$ gcc -shared -W1,-soname,libHW.so.1 -o libHW.so HelloWorld.ouser@ws$ gcc app.c -lHW -L. -o my_appuser@ws$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATHuser@ws$ ./my_appHello World20user@ws$
LD_LIBRARY_PATH− Set to the directory containing the .so file
![Page 28: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/28.jpg)
Some more details about linking
• If -static is specified, linking is always static– if lib<name>.a is not found gcc errors out
• Otherwise– If lib<name>.so is found linking is dynamic– otherwise linking is static
• In case of dynamic linking, lib<name>.so should be placed in a standard location, otherwise LD_LIBRARY_PATH needs to be set
![Page 29: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/29.jpg)
Recommended organization of Multi-file Projects
Header Files
Project Name
Module1
Include
SrcImplementation Source files
App
Src
Application Source file
.
.
.
.
.
.
![Page 30: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/30.jpg)
Compiler Warnings• Special option: -Wall• Syntax : gcc -Wall ....
user@ws$ gcc warning.cuser@ws$ gcc warning.c -Wallwarning.c: In function ‘main’:warning.c:8: warning: suggest explicit braces to avoid ambiguous ‘else’user@ws$
![Page 31: Introduction to GCC - Indian Statistical Institutepdslab/2014/slides/01IntroToGCC.pdf · Santanu Sinha Created Date: 7/20/2013 2:08:37 PM ...](https://reader031.fdocuments.net/reader031/viewer/2022020120/5b4c4b167f8b9a5c278d4667/html5/thumbnails/31.jpg)
Wrapping up
Questions?