KYC - Know your compiler. Introduction to GCC

Similar documents
Your first C and C++ programs

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Miscellaneous C-programming Issues

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CS240: Programming in C

EL2310 Scientific Programming

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

EL2310 Scientific Programming

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

PRINCIPLES OF OPERATING SYSTEMS

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Lectures 5-6: Introduction to C

C - Basics, Bitwise Operator. Zhaoguo Wang

The Compilation Process

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

CMPE-013/L. Introduction to C Programming

2 Compiling a C program

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture Notes on Generic Data Structures

Lectures 5-6: Introduction to C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Topic 6: A Quick Intro To C

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Command Line Build Your Own C/C++ Files

CS240: Programming in C. Lecture 2: Overview

Compiler Drivers = GCC

UNIX Makefile. C Project Library Distribution and Installation.

COSC350 System Software

A Fast Review of C Essentials Part I

COMP322 - Introduction to C++

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

Advanced Programming & C++ Language

make and makefile CS 211

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Errors During Compilation and Execution Background Information

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

A Fast Review of C Essentials Part II

Programming in C week 1 meeting Tiina Niklander

Conduite de Projet Cours 4 The C build process

BLM2031 Structured Programming. Zeyneb KURT

Command Line Build Your Own C/C++ Files

C introduction: part 1

Embedded Systems Programming

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

The C Preprocessor Compiling and Linking Using MAKE

Have examined process Creating program Have developed program Written in C Source code

Programming refresher and intro to C programming

Page 1. Agenda. Programming Languages. C Compilation Process

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

Programming in C First meeting

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Generic Programming in C

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Computers and Computation. The Modern Computer. The Operating System. The Operating System

CSE 351. Introduction & Course Tools

C Programming Language Training. This instruction relies on the C language described in C++: The Complete Reference Third Edition By Herbert Schildt

Introduction to C An overview of the programming language C, syntax, data types and input/output

CS 61C: Great Ideas in Computer Architecture Introduction to C

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

Follow us on Twitter for important news and Compiling Programs

Introduction to the C Programming Language

CS2141 Software Development using C/C++ Libraries

Computer Systems Lecture 9

Introduction to C Programming

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Final CSE 131B Spring 2004

1 You seek performance 3

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

The C standard library

C Overview Fall 2014 Jinkyu Jeong

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Memory Allocation in C

COMP26912: Algorithms and Imperative Programming

C and C++ I. Spring 2014 Carola Wenk

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CS2141 Software Development using C/C++ Compiling a C++ Program

Intermediate Programming, Spring 2017*

CS Basics 15) Compiling a C prog.

Executables and Linking. CS449 Spring 2016

Creating, Compiling and Executing

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Lecture 2: C Programm

Main differences with Java

Transcription:

KYC - Know your compiler Introduction to GCC

The Operating System User User 1 General Purpose or Application Specific Software Operating System Kernel Computer Hardware User 2

What is GCC? GCC is the GNU Compiler Collection Provides Compilers for: C C++ Objective C Fortran ADA Java

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

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

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.out Hello World user@ws$

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.out Hello World user@ws$

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$./myapp Hello World user@ws$

Multi-file Programs Why create multi-file programs? Manageability Modularity Re-usability Abstraction

General abstraction used in Multi-file Programs Components: Header files Implementation Source files Application source file ( contains the main function)

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

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 <stdio.h> #include HelloWorld.h int imyglobalvar; void printhelloworld() { imyglobalvar = 20; printf( Hello World\n ); return; }

Application Source File Contents: 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; }

Correlation between components Other includes #include Module Header File #include Other includes Application File Other libraries / object files Module Implementation File Compile Compile Object File Link Object File Executable File

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_app user@sw$./my_app 10 Hello World 20 user@ws$

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]

Step 5: Continued Example: user@ws$ gcc -c HelloWorld.c user@ws$ gcc -c app.c user@ws$ gcc HelloWorld.o app.o -o my_app user@sw$./my_app 10 Hello World 20 user@ws$

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/src user@ws$ gcc -c -I../../HelloWorld/include HelloWorld.c user@ws$ cd../../app/src user@ws$ gcc -c -I../../HelloWorld/include main.c user@ws$ cd../../ user@ws$ gcc HelloWorld/src/HelloWorld.o app/src/main.o -o my_app user@ws$./my_app

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

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

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

Step 7: Linking with external libraries Static linking: Link to libname.a Special option: -static and -l Syntax: gcc -static <filename(s)> -lname user@ws$ gcc -static math_test.c -lm Dynamic linking: Link to libname.so Special option: -l Syntax: gcc <filename(s)> -lname user@ws$ gcc math_test.c -lm

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

Step 9: Building Static libraries Required external tools: ar, ranlib 1.Create object files for the source codes 2.User ar to create the archives with names of the form: lib<libraryname>.a 3.Use ranlib to generate the index within the library

Step 9: Continued Example user@ws$ gcc -c HelloWorld.c user@ws$ ar rcs libhw.a HelloWorld.o user@ws$ ranlib libhw.a user@ws$ gcc app.c -lhw -L. -o my_app user@ws$./my_app 10 Hello World 20 user@ws$

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>

Example Step 10: Continued user@ws$ gcc -c -fpic HelloWorld.c user@ws$ gcc -shared -W1,-soname,libHW.so.1 -o libhw.so HelloWorld.o user@ws$ gcc app.c -lhw -L. -o my_app user@ws$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH user@ws$./my_app Hello World 20 user@ws$ LD_LIBRARY_PATH Set to the directory containing the.so file

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

Recommended organization of Multi-file Projects Project Name...... Module1 Include Src Header Files Implementation Source files App Src Application Source file

Compiler Warnings Special option: -Wall Syntax : gcc -Wall... user@ws$ gcc warning.c user@ws$ gcc warning.c -Wall warning.c: In function main : warning.c:8: warning: suggest explicit braces to avoid ambiguous else user@ws$

Wrapping up Questions?