Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Introduction to GCC Department of Radio Physics and Electronics, Calcutta University.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
CSE 303 Lecture 16 Multi-file (larger) programs
Linking & Loading CS-502 Operating Systems
Chapter 7: User-Defined Functions II
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Building User Libraries.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Computer Science 210 Computer Organization Introduction to C.
Chapter 3 Getting Started with C++
chap13 Chapter 13 Programming in the Large.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
UNIT 13 Separate Compilation.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
First Compilation Rudra Dutta CSC Spring 2007, Section 001.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
KYC - Know your compiler Introduction to GCC
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
Introduction to C Topics Compilation Using the gcc Compiler
C++ Lesson 1.
Week 3-4 Control flow (review) Function definition Program Structures
Computer Science 210 Computer Organization
Executable program (p1)
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Linking & Loading.
Functions Separate Compilation
Day 01 Introduction to Linux and C
Introduction to C Topics Compilation Using the gcc Compiler
CS-3013 Operating Systems C-term 2008
CS1010 Programming Methodology
Introduction to C Topics Compilation Using the gcc Compiler
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
CS1010 Programming Methodology
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
C Preprocessor(CPP).
Comments, Prototypes, Headers & Multiple Source Files
Govt. Polytechnic,Dhangar
Linking & Loading CS-502 Operating Systems
C++ Compilation Model C++ is a compiled language
Your first C and C++ programs
CSE 303 Concepts and Tools for Software Development
C Preprocessor Seema Chandak.
C Programming Language
Linking & Loading CS-502 Operating Systems
Compiler vs linker The compiler translates one .c file into a .o file
Introduction to C Topics Compilation Using the gcc Compiler
Executable program (p1)
SPL – PS1 Introduction to C++.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Introduction to GCC Department of Radio Physics and Electronics, Calcutta University And Indian GNU/Linux Users Group, Kolkata Chapter

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal What is GCC? GCC is the GNU Compiler Collection Provides Compilers for: ● C ● C++ ● Objective C ● Fortran ● ADA ● Java

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal 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

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Possible Outputs for C/C++ Programs ● Complete processing: – Executable files – Dynamically linked 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

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 1: Compiling a simple C Program ● Syntax: gcc gcc HelloWorld.c ● Output: An executable called a.out ● To run:./a.out Hello World Hello World

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 2: Compiling a simple C++ Program ● Syntax: g++ g++ HelloWorld.cpp ● Output: An executable called a.out ● To run:./a.out Hello World Hello World

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 3: Providing the executable name ● Extra option: -o ● Syntax: gcc -o gcc HelloWorld.c -o myapp ● Output: An executable called outputname ● To run:./outputname Hello World Hello World

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Multi-file Programs Why create multi-file programs? ● Manageability ● Modularity ● Re-usability ● Abstraction

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal General abstraction used in Multi-file Programs ● Components: – Header files – Implementation Source files – Application source file ( contains the main function)

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Header Files Contents: ● 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 #ifndef _HELLOWORLD_H_ #define _HELLOWORLD_H_ typedef unsigned int my_uint_t; extern void printHelloWorld(); extern int iMyGlobalVar;... #endif

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Implementation Source Files Contents: ● Function body for functions declared in corresponding header files ● Statically defined and inlined functions ● Global variable definitions Example: HelloWorld.c #include #include “HelloWorld.h” int iMyGlobalVar; void printHelloWorld() { iMyGlobalVar = 20; printf(“Hello World\n”); return; } #include #include “HelloWorld.h” int iMyGlobalVar; void printHelloWorld() { iMyGlobalVar = 20; printf(“Hello World\n”); return; }

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Application Source File Contents: ● Function body for the main function ● Acts as client for the different modules Example: app.c #include #include “HelloWorld.h” int main() { iMyGlobalVar = 10; printf(“%d\n”, iMyGlobalVar); printHelloWorld(); printf(“%d\n”, iMyGlobalVar); return 0; } #include #include “HelloWorld.h” int main() { iMyGlobalVar = 10; printf(“%d\n”, iMyGlobalVar); printHelloWorld(); printf(“%d\n”, iMyGlobalVar); return 0; }

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Correlation between components Module Header File Module Implementation File Application File #include Object File Object File Link Executable File Compile Other includes Other libraries / object files

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 4: Compiling a simple multi-file program ● Syntax: gcc... -o filename ● Example: gcc HelloWorld.c app.c -o my_app 10 Hello World 20 gcc HelloWorld.c app.c -o my_app 10 Hello World 20

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal 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 ● Link multiple files into the final executables – Syntax: gcc [-o output]

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 5: Continued gcc -c HelloWorld.c gcc -c app.c gcc HelloWorld.o app.o -o my_app 10 Hello World 20 gcc -c HelloWorld.c gcc -c app.c gcc HelloWorld.o app.o -o my_app 10 Hello World 20 ● Example:

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 6: Including files from other directories ● Special Option: -I ● Syntax: gcc -I -I ● Example: cd HelloWorld/src gcc -c -I../../HelloWorld/include HelloWorld.c cd../../app/src gcc -c -I../../HelloWorld/include main.c cd../../ gcc HelloWorld/src/HelloWorld.o app/src/main.o -o my_app cd HelloWorld/src gcc -c -I../../HelloWorld/include HelloWorld.c cd../../app/src gcc -c -I../../HelloWorld/include main.c cd../../ gcc HelloWorld/src/HelloWorld.o app/src/main.o -o my_app

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal 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.a – Dynamically Linked: Object code is loaded dynamically into memory at runtime. ● Name format: lib.so

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal 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 the executable ● Library format is dependent on linkers ● Increases executable size

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal 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 the loader ● Increases reuse ● Decreases program size and execution time

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 7: Linking with external libraries ● Static linking: Link to libname.a – Special option: -static and -l – Syntax: gcc -static -lname ● Dynamic linking: Link to libname.so – Special option: -l – Syntax: gcc -lname gcc -static math_test.c -lm gcc math_test.c -lm

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 8: Linking to a library at non-standard path ● Special option: -L ● Syntax: gcc -l -L gcc math_test.c -lm -L/usr/lib

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 9: Continued ● Example gcc -c HelloWorld.c ar rcs libHW.a HelloWorld.o ranlib libHW.a gcc app.c -lHW -L. -o my_app 10 Hello World 20 gcc -c HelloWorld.c ar rcs libHW.a HelloWorld.o ranlib libHW.a gcc app.c -lHW -L. -o my_app 10 Hello World 20

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal 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.so.

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Step 10: Continued ● Example gcc -c -fPIC HelloWorld.c gcc -shared -W1,-soname,libHW.so.1 -o libHW.so HelloWorld.o gcc app.c -lHW -L. -o my_app export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH Hello World 20 gcc -c -fPIC HelloWorld.c gcc -shared -W1,-soname,libHW.so.1 -o libHW.so HelloWorld.o gcc app.c -lHW -L. -o my_app export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH Hello World 20 ● LD_LIBRARY_PATH – Set to the directory containing the.so file

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Some more details about linking ● If -static is specified, linking is always static – if lib.a is not found gcc errors out ● Otherwise – If lib.so is found linking is dynamic – otherwise linking is static ● In case of dynamic linking, lib.so should be placed in a standard location, otherwise LD_LIBRARY_PATH needs to be set

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Recommended organization of Multi-file Projects Header Files Project Name Module1 Include Src Implementation Source files App Src Application Source file

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Compiler Warnings ● Special option: -Wall ● Syntax : gcc -Wall.... gcc warning.c gcc warning.c -Wall warning.c: In function ‘main’: warning.c:8: warning: suggest explicit braces to avoid ambiguous ‘else’ gcc warning.c gcc warning.c -Wall warning.c: In function ‘main’: warning.c:8: warning: suggest explicit braces to avoid ambiguous ‘else’

Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Wrapping up Questions?