SlideShare a Scribd company logo
GCC/G++
OPERATING SYSTEMS
What is GNU?
The GNU project was started in 1984 to create a complete Unix-like
operating system as free software, in order to promote freedom and
cooperation among computer users and programmers. Every Unix-like
operating system needs a C compiler, and as there were no free
compilers in existence at that time, the GNU Project had to develop one
from scratch. The work was funded by donations from individuals and
companies to the Free Software Foundation, a non-profit organization set
up to support the work of the GNU Project.
What is GCC?
GCC stands for “GNU Compiler Collection”. It is an integrated distribution of compilers
for several major programming languages: C, C++, Objective-C, Objective-
C++, Java, Fortran, Ada, and Go. The abbreviation GCC has multiple meanings in
common use. The current official meaning is “GNU Compiler Collection”, which refers
generically to the complete suite of tools. The name historically stood for “GNU C
Compiler”. The name is also used when speaking of the language-
independent component of GCC: code shared among the compilers for all supported
languages. The language-independent component of GCC includes the majority of the
optimizers, as well as the “back ends” that generate machine code for various
processors. The part of a compiler that is specific to a particular language is called the
“front end”.
What is G++?
Most of the compilers for languages other than C have their own names. The “G++” is
the compiler of C++. When compiling these languages, we might refer to that compiler
by its own name, or as “GCC”. Either is correct.
G++ is a compiler, not merely a preprocessor. G++ builds object code directly from C++
program source. There is no intermediate C version of the program. Avoiding an
intermediate C representation of the program means that it is possible to get better
object code, and better debugging information. The GNU debugger, GDB, works with
this information in the object code to give a comprehensive C++ source-level editing
capabilities.
GCC Command Options
When GCC invoked, it normally does preprocessing, compilation, assembly and linking.
The “overall options” allows to stop this process at an intermediate stage. For example,
the -c option says not to run the linker. Then the output consists of object files output by
the assembler. Other options are passed on to one stage of processing. Some options
control the preprocessor and others the compiler itself. Yet other options control the
assembler and linker.
Most of the command line options those used with GCC are useful for C programs; when
an option is only useful with another language (usually C++), the explanation says so
explicitly. If the description for a particular option does not mention a source language, it
is possible to use that option with all supported languages.
GCC Command Options (2)
The GCC compiler accepts both single-letter options, such as -o, and multi-letters, such
as -ansi. Because it accepts both types of options, it is not possible to group multiple
single-letter options together as it may be done in many GNU and Unix/Linux programs.
For example, the multi-letter option -pg is not the same as the two single-letter options -p
-g. The -pg option creates extra code in the final binary that outputs profile information
for the GNU code profiler, gprof. On the other hand, the -p -g options generate extra
code in the resulting binary that produces profiling information for use by the prof code
profiler (-p) and causes GCC to generate debugging information using the operating
system’s normal format (-g).
GCC Command Options (3)
Despite its sensitivity to the grouping of multiple single-letter options, it is possible to mix
the order of options and compiler arguments on the GCC command line. That is,
invoking GCC as
gcc -pg -fno-strength-reduce -g myprog.c -o myprog
has the same result as
gcc myprog.c -o myprog -g -fno-strength-reduce –pg
In some situations, order does matter if several options of the same kind been used. For
example, the -I option specifies the directory or directories to search for include files. So
if -I specified several times, GCC searches the listed directories in the order specified.
GCC Command Options (4)
To compile a single source file, invoke GCC passing the name of the source file as the
argument.
$ gcc myprog.c
$ ls –l
By default, the result on Linux and Unix systems is an executable file named a.out in the
current directory, which you execute by typing ./a.out. On Cygwin systems, you will wind
up with a file named a.exe that you can execute by typing either ./a or ./a.exe. To define
the name of the output file that GCC produces, use the -o option, as illustrated in the
following example:
$ gcc myprog.c -o runme
GCC Command Options (5)
For compiling multiple source files, they may simply specified on the GCC command
line, as in the following example, which leaves the compiled and linked executable in the
file named showdate:
$ gcc showdate.c helper.c –o showdate
To compile these files incrementally and eventually to link them into a binary, it is
possible to use the –c option to halt compilation after producing an object file, as in the
following example:
$ gcc -c showdate.c
$ gcc -c helper.c
$ gcc showdate.o helper.o –o showdate
G++ Command Options
The G++ compiler also accepts both single-letter options and multi-letter options as
GCC. And also it is not possible to group multiple single-letter options due to fact that it
accepts both types of options together.
Despite its sensitivity to the grouping of multiple single-letter options, it is free to mix the
order of options and compiler arguments on the command line. That is, invoking G++ as
g++ -pg -fno-strength-reduce -g myprog.c -o myprog
has the same result as
g++ myprog.c -o myprog -g -fno-strength-reduce -pg
G++ Command Options (2)
In some situations, order does matter if several options of the same kind been used. For
example, the -I option specifies the directory or directories to search for include files. So
if -I specified several times, it searches the listed directories in the order specified.
To compile a single source file, myprog.cc, just invoke G++, passing the name of the
source file as the argument.
$ g++ myprog.cc
$ ls -l
G++ Command Options (3)
By default, the result on Linux and Unix systems is an executable file named a.out in the
current directory, which may be executed by typing ./a.out. On Cygwin systems, result is
a file named a.exe that you can execute by typing either ./a or ./a.exe. To define the
name of the output file that G++ produces, use the -o option, as illustrated in the
following example:
$ g++ myprog.cc -o runme
$ ls –l
For compiling multiple source files using G++, and to get file named showdate:
$ g++ showdate.cc helper.cc –o showdate
G++ Command Options (4)
If you want to compile these files incrementally and eventually link them into a binary,
you can use the –c option to halt compilation after producing an object file, as in the
following example:
$ g++ -c showdate.cc
$ g++ -c helper.cc
$ g++ showdate.o helper.o –o showdate
$ ls –l
G++ Command Options (5)
All GCC compilers evaluate filename suffixes to identify the type of compilation that they will
perform.
Suffix Operation
.C C++ source code to preprocess.
.cc C++ source code to preprocess. (Standard extension for C++ source files.)
.cpp C++ source code to preprocess.
.cp C++ source code to preprocess.
.c++ C++ source code to preprocess
.cxx C++ source code to preprocess
.ii C++ source code not to preprocess.
Options Summary
Here is a summary of all the options, grouped by type.
Overall Options: Controlling the kind of output: an executable, object files, assembler
files, or preprocessed source.
C Dialect Options: Controlling the variant of C language compiled.
C++ Dialect Options: Variations on C++.
Objective-C Dialect Options: Variations on Objective-C.
Language Independent Options: Controlling how diagnostics should be formatted.
Warning Options: How picky should the compiler be?
Options Summary (2)
Debugging Options: Symbol tables, measurements, and debugging dumps.
Optimize Options: How much optimization?
Preprocessor Options: Controlling header files and macro definitions. Also, getting
dependency information for Make.
Assembler Options: Passing options to the assembler.
Link Options: Specifying libraries and so on.
Directory Options: Where to find header files and libraries. Where to find the compiler
executable files.
Spec Files: How to pass switches to sub-processes.
Target Options: Running a cross-compiler, or an old version of GCC.
Optimization of Code with GCC
These days, compilers are pretty smart. They can perform all sorts of code
transformations make compiled code run faster. In most situations, faster is better than
smaller, because disk space and memory are quite cheap for desktop users.
However, for embedded systems small is often at least as important as fast because of a
commonplace environment consisting of extreme memory constraints and no disk
space, making code optimization a very important task.
In the absence of optimization, GCC’s goal, besides compiling code that works, is to
keep compilation time to a minimum and generate code that runs predictably in a
debugging environment.
Optimization Theory
Optimization refers to analyzing a straightforward compilation’s resulting code to
determine how to transform it to run faster, consume fewer resources, or both. Compilers
that do this are known as optimizing compilers, and the resulting code is known as
optimized code.
To produce optimized code, an optimizing compiler performs one or more
transformations on the source code provided as input. The purpose is to replace less
efficient code with more efficient code while at the same time preserving the meaning
and ultimate results of the code.
Optimization Theory (2)
Optimizing compilers use several methods to determine where generated code can be
improved. One method is control flow analysis, which examines loops and other
control constructs, such as if-then and case statements, to identify the execution paths a
program might take and, based on this analysis, determine how the execution path can
be streamlined.
Another typical optimization technique examines how data is used in a program, a
procedure known as data flow analysis. Data flow analysis examines how and when
variables are used (or not) in a program and then applies various set equations to these
usage patterns to derive optimization opportunities.
Optimization Techniques – Code
Motion
It is an optimization technique to eliminate redundant code through common sub-
expression elimination. It does not entirely remove common sub-expressions, but
attempts to reduce the number of times they occur by relocating them to more optimal
locations in an intermediate representation of the code. For example, in nested loops or
other control constructs, intermediate value calculations may be performed more times
than necessary. In order to optimize, compilers can move these calculations to locations
where they will be executed less frequently, but will still produce the same result. Code
motion that specifically moves redundant calculations outside the scope of a loop is
referred to as loop-invariant code motion. It is also used in a technique that is related to
common sub-expression elimination known as partial redundancy elimination.
Optimization Techniques – Common
Sub-expression Elimination
Eliminating redundant computations is a standard optimization mechanism because it
reduces the number of instructions that a program has to execute in order to arrive at the
same result.
For example, given an expression whose value is computed only once, each subsequent
occurrence of that expression can be eliminated and its value used instead, if the values
of the variables in the expression have not changed since first computed. The
subsequent occurrences of the expression are called common sub-expressions.
Optimization Techniques – Common
Sub-expression Elimination (2)
Example:
#define STEP 3
#define SIZE 100
int i, j, k;
int p[SIZE];
for (i = 0; i < SIZE; ++i) {
j = 2 * STEP;
k = p[i] * j; }
The expression j = 2 * STEP in the for loop is a common sub-expression because its
value is computed before entering the loop and its constituent variable, STEP (actually a
predefined value), has not changed.
Optimization Techniques – Common
Sub-expression Elimination (3)
Common sub-expression elimination (CSE) removes the repeated computation of j in the
for loop. After CSE, the for loop might look like the following:
j = 2 * STEP;
for (i = 0; i < 100; ++i) {
k = p[i] * j; }
Admittedly, the example is contrived, but it makes the point: CSE has eliminated 100
redundant computations of j. CSE improves performance by eliminating unnecessary
computations and typically also reduces the size of the resulting binary by eliminating
unnecessary instructions.
Optimization Techniques – Constant
Folding
It is an optimization technique that eliminates expressions that calculate a value that can
already be determined when the program is compiled. These are typically calculations
that only reference constant values or expressions that reference variables whose
values are constant. For example:
n = 10 * 20 * 400;
i = 10;
j = 20;
ij = i * j;
In the latter case, the assignments to the variables i and j could also be eliminated if
these variables were not used elsewhere in the code.
Optimization Techniques – Copy
Propagation Transformations
It eliminate cases in which values are copied from one location or variable to another in
order to simply assign their value to another variable. Given an assignment of the form f
= g, subsequent uses of f, called copies, use g instead. Consider a code fragment such
as the following:
i = 10;
x[j] = i;
y[MIN] = b;
b = i; //After CPT it looks like “b=10”
Optimization Techniques – Dead
Code Elimination
Dead code elimination (DCE) is the term used for optimizations that remove code that
doesn’t actually do anything permanent or useful. Unreachable code elimination is a
related optimization that removes code that the compiler can identify as impossible to
reach. For example:
if ( i == 10 ) { . . . }
else { . . .
if ( i == 10) { . . . }
}
The nested test for whether i is equal to 10 can be eliminated because it can never be
reached.
Optimization Techniques – If-
Conversion
If-conversion is a technique where branch constructs, such as large if-then-else if-else
constructs, are broken into separate if statements to simplify generated code, provide
opportunities for further optimization, and eliminate jumps and branches wherever
possible.
Optimization Techniques – Inlining
It is an optimization technique where code performance can improve by replacing
complex constructs and even function calls with inline representations of the construct or
function call. Code inlining, or loop unrolling, are terms for replacing all or portions of a
loop with an explicit series of instructions. Function inlining is the term for replacing
function calls with the explicit set of instructions that are performed by the function. In
general, inlining can reduce code complexity and provide higher performance than would
be required by branching that would be done otherwise. It often also provides
opportunities for common sub-expression elimination and code motion optimization.
GCC Optimization Basics
GCC uses a sequence of different intermediate representations of code before
generating binaries. Converting language-specific code into simpler forms provides
several primary advantages:
- Breaking code into simpler, more low-level constructs exposes opportunities for
optimization that may not be directly visible in source code.
- Using intermediate representations enables the simpler form to more easily and
readably represent parse trees that are otherwise at varying levels of complexity.
- Using a simpler form enables the GCC compilers to share optimization mechanisms,
regardless of the language in which your code was originally written.
GCC Optimization Basics (2)
Traditionally, GCC has always internally used an intermediate form known as Register
Transfer Language (RTL), which is a very low-level language into which all code
compiled with a GCC compiler (in any high-level language) is transformed before object
code generation begins. Transforming high-level input languages into an intermediate,
internal form is a time-tested mechanism for exposing opportunities for optimization. As
a very low-level representation of code, GCC’s RTL lends itself well to optimizations that
are similarly low-level, such as register allocation and stack and data optimizations.
Because it is relatively low-level, RTL is not as good as one would like in terms of
supporting higher-level optimizations related to data types, array and variable
references, and overall data and control flow within code.
GCC Optimization Basics (3)
With GCC 4.0, the fine makers of the GCC compilers introduced a new intermediate
form, static single assignment (SSA), which operates on the parse trees that are
produced from code by GCC compilers, and is thus known as Tree SSA. It is interesting
to note that 4.0 and later GCC compilers use two intermediate forms before arriving at a
Tree SSA representation, known as GENERIC and GIMPLE. A GENERIC representation
is produced by eliminating language-specific constructs from the parse tree that is
generated from code, and a GIMPLE representation is then produced from the
GENERIC representation by simplifying address references within the code. It provides
several additional points at which optimizations can occur at as high a level as possible
before zooming into the depths of the RTL.
Architecture-Independent
Optimizations
GCC’s most well-known optimization switches are –O; its variant -On, where n is an
integer between 0 and 3; and -Os. -OO turns off optimization. -O and -O1 (which are
level 1) are equivalent, telling GCC to optimize code. With -O or -O1, the compiler
attempts to minimize both code size and execution time without dramatically increasing
compilation time. Using -O2 and -O3 will increase the level from that requested by -
O1, while still invoking the optimization options requested by -O1. To minimize code
size, use option -Os. To disable one of them while leaving the others enabled, negate the
option using no- between -f and the optimization name. For example, to disable deferred
stack pops, the command line might resemble this:
$ gcc myprog.c -o myprog -O1 -fno-defer-pop
Architecture-Independent Level 1
GCC Optimizations
Architecture-Independent Level 1
GCC Optimizations (2)
Architecture-Independent Level 1
GCC Optimizations (3)
Architecture-Independent Level 2
GCC Optimizations
-O2 optimization (level 2 optimization) includes all level 1 optimizations plus the
additional optimizations listed below. Applying these optimizations will lengthen the
compile time, but as a result a measurable increase should be seen in the resulting
code’s performance, or, rather, a measurable decrease in execution time.
Architecture-Independent Level 2
GCC Optimizations (2)
Architecture-Independent Level 2
GCC Optimizations (3)
Architecture-Independent Level 2
GCC Optimizations (4)
Architecture-Independent GCC
Optimizations for Code Size
The -Os option is becoming increasingly popular because it applies all of the level 2
optimizations except those known to increase the code size. -Os also applies additional
techniques to attempt to reduce the code size. Code size, in this context, refers to a
program’s memory footprint at runtime rather than its on-disk storage requirement. In
particular, -Os disables the following optimization flags (meaning they will be ignored if
specified in conjunction with -Os):
• -falign-functions
• -falign-jumps
• -falign-labels
• -falign-loops
Architecture-Independent GCC
Optimizations for Code Size (2)
• -fprefetch-loop-arrays
• -freorder-blocks
• -freorder-blocks-and-partition
• -ftree-ch
You might find it instructive to compile a program with -O2 and -Os and compare runtime
performance and memory footprint. For example, I have found that recent versions of the
Linux kernel have nearly the same runtime performance when compiled with -O2 and -
Os, but the runtime memory footprint is 15 percent smaller when the kernel is compiled
with -Os. Naturally, your mileage may vary and, as always, if it breaks, you get to keep all
of the pieces.
Architecture-Independent Level 3
GCC Optimizations
Specifying the –O3 optimization option enables all level 1 and level 2 optimizations plus
the following:
• -fgcse-after-reload: Performs an extra load elimination pass after reloading
• -finline-functions: Integrates all “simple” functions into their callers
• -funswitch-loops: Moves branches with loop invariant conditions out of loops
Note: If you specify multiple -O options, the last one encountered takes precedence.
Thus, given the command gcc -O3 foo.c bar.c -O0 -o baz, no optimization will be
performed because -O0 overrides the earlier -O3.
Architecture-Independent Manuel
GCC Optimization Flags
In addition to the optimizations enabled using one of the -O options, GCC has a number
of specialized optimizations that can only be enabled by specifically requesting them
using -f.
Architecture-Independent Manuel
GCC Optimization Flags (2)
Note: Not all of GCC’s optimizations can be controlled using a flag. GCC performs some
optimizations automatically and we cannot disable these optimizations when you request
optimization using -O.
Processor-Specific Optimizations
In reality, though, the target-specific switches are not properly optimizations in the traditional
sense. Rather, they are options that give GCC more information about the type of system
on which the code being compiled will run. GCC can use this additional information to
generate code that takes advantage of specific features or that works around known
misfeatures of the given processor.
Thanks for listening…
Ad

More Related Content

What's hot (20)

Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
Linux Synchronization Mechanism: RCU (Read Copy Update)
Linux Synchronization Mechanism: RCU (Read Copy Update)Linux Synchronization Mechanism: RCU (Read Copy Update)
Linux Synchronization Mechanism: RCU (Read Copy Update)
Adrian Huang
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
Brendan Gregg
 
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺
hydai
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
mukul bhardwaj
 
eBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux Kernel
Thomas Graf
 
Anatomy of the loadable kernel module (lkm)
Anatomy of the loadable kernel module (lkm)Anatomy of the loadable kernel module (lkm)
Anatomy of the loadable kernel module (lkm)
Adrian Huang
 
Poll mode driver integration into dpdk
Poll mode driver integration into dpdkPoll mode driver integration into dpdk
Poll mode driver integration into dpdk
Vipin Varghese
 
Advanced C
Advanced C Advanced C
Advanced C
Emertxe Information Technologies Pvt Ltd
 
IntelON 2021 Processor Benchmarking
IntelON 2021 Processor BenchmarkingIntelON 2021 Processor Benchmarking
IntelON 2021 Processor Benchmarking
Brendan Gregg
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
Linaro
 
How A Compiler Works: GNU Toolchain
How A Compiler Works: GNU ToolchainHow A Compiler Works: GNU Toolchain
How A Compiler Works: GNU Toolchain
National Cheng Kung University
 
Assembler
AssemblerAssembler
Assembler
Maha Lakshmi
 
Introduction to armv8 aarch64
Introduction to armv8 aarch64Introduction to armv8 aarch64
Introduction to armv8 aarch64
Yi-Hsiu Hsu
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
Kernel TLV
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
Houcheng Lin
 
Gcc porting
Gcc portingGcc porting
Gcc porting
Shiva Chen
 
Toolchain
ToolchainToolchain
Toolchain
Anil Kumar Pugalia
 
Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
Ray Jenkins
 
GCC RTL and Machine Description
GCC RTL and Machine DescriptionGCC RTL and Machine Description
GCC RTL and Machine Description
Priyatham Bollimpalli
 
Linux Synchronization Mechanism: RCU (Read Copy Update)
Linux Synchronization Mechanism: RCU (Read Copy Update)Linux Synchronization Mechanism: RCU (Read Copy Update)
Linux Synchronization Mechanism: RCU (Read Copy Update)
Adrian Huang
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
Brendan Gregg
 
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺
hydai
 
eBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux KerneleBPF - Rethinking the Linux Kernel
eBPF - Rethinking the Linux Kernel
Thomas Graf
 
Anatomy of the loadable kernel module (lkm)
Anatomy of the loadable kernel module (lkm)Anatomy of the loadable kernel module (lkm)
Anatomy of the loadable kernel module (lkm)
Adrian Huang
 
Poll mode driver integration into dpdk
Poll mode driver integration into dpdkPoll mode driver integration into dpdk
Poll mode driver integration into dpdk
Vipin Varghese
 
IntelON 2021 Processor Benchmarking
IntelON 2021 Processor BenchmarkingIntelON 2021 Processor Benchmarking
IntelON 2021 Processor Benchmarking
Brendan Gregg
 
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, InternalsLAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
Linaro
 
Introduction to armv8 aarch64
Introduction to armv8 aarch64Introduction to armv8 aarch64
Introduction to armv8 aarch64
Yi-Hsiu Hsu
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
Kernel TLV
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
Houcheng Lin
 
Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
Ray Jenkins
 

Viewers also liked (13)

Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
Janani Parthiban
 
Compiling Under Linux
Compiling Under LinuxCompiling Under Linux
Compiling Under Linux
PierreMASURE
 
NetBeans para Java, C, C++
NetBeans para Java, C, C++NetBeans para Java, C, C++
NetBeans para Java, C, C++
Manuel Antonio
 
Gcc opt
Gcc optGcc opt
Gcc opt
Mark Veltzer
 
C compilation process
C compilation processC compilation process
C compilation process
RajKumar Rampelli
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
Gccgdb
GccgdbGccgdb
Gccgdb
selva raj
 
GCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programsGCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
 
HRM - PM in GCC
HRM - PM in GCCHRM - PM in GCC
HRM - PM in GCC
Muhammad Danish Azad
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
MinGw Compiler
MinGw CompilerMinGw Compiler
MinGw Compiler
Avnish Patel
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
Saleem Ansari
 
Deep C
Deep CDeep C
Deep C
Olve Maudal
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
Janani Parthiban
 
Compiling Under Linux
Compiling Under LinuxCompiling Under Linux
Compiling Under Linux
PierreMASURE
 
NetBeans para Java, C, C++
NetBeans para Java, C, C++NetBeans para Java, C, C++
NetBeans para Java, C, C++
Manuel Antonio
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
GCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programsGCC Compiler as a Performance Testing tool for C programs
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
Saleem Ansari
 
Ad

Similar to G++ & GCC (20)

Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
Gizem Çetin
 
GNU Debugger
GNU DebuggerGNU Debugger
GNU Debugger
Gizem Çetin
 
GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?
Saket Pathak
 
Comparing C and Go
Comparing C and GoComparing C and Go
Comparing C and Go
Marcin Pasinski
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical training
Thierry Gayet
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
NEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
MayurWagh46
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
Danielle780357
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
bhargavi804095
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
bhargavi804095
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oS
Harrytoye2
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
corehard_by
 
02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)
Mohammed Ali
 
GCC
GCCGCC
GCC
Anshul Sharma
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
Keroles karam khalil
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
Keroles karam khalil
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
mpaproductions
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applications
Roman Podoliaka
 
GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?
Saket Pathak
 
Autotools pratical training
Autotools pratical trainingAutotools pratical training
Autotools pratical training
Thierry Gayet
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
NEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
MayurWagh46
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
bhargavi804095
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
bhargavi804095
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oS
Harrytoye2
 
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
The Hitchhiker's Guide to Faster Builds. Viktor Kirilov. CoreHard Spring 2019
corehard_by
 
Life of a Chromium Developer
Life of a Chromium DeveloperLife of a Chromium Developer
Life of a Chromium Developer
mpaproductions
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applications
Roman Podoliaka
 
Ad

Recently uploaded (20)

SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 

G++ & GCC

  • 2. What is GNU? The GNU project was started in 1984 to create a complete Unix-like operating system as free software, in order to promote freedom and cooperation among computer users and programmers. Every Unix-like operating system needs a C compiler, and as there were no free compilers in existence at that time, the GNU Project had to develop one from scratch. The work was funded by donations from individuals and companies to the Free Software Foundation, a non-profit organization set up to support the work of the GNU Project.
  • 3. What is GCC? GCC stands for “GNU Compiler Collection”. It is an integrated distribution of compilers for several major programming languages: C, C++, Objective-C, Objective- C++, Java, Fortran, Ada, and Go. The abbreviation GCC has multiple meanings in common use. The current official meaning is “GNU Compiler Collection”, which refers generically to the complete suite of tools. The name historically stood for “GNU C Compiler”. The name is also used when speaking of the language- independent component of GCC: code shared among the compilers for all supported languages. The language-independent component of GCC includes the majority of the optimizers, as well as the “back ends” that generate machine code for various processors. The part of a compiler that is specific to a particular language is called the “front end”.
  • 4. What is G++? Most of the compilers for languages other than C have their own names. The “G++” is the compiler of C++. When compiling these languages, we might refer to that compiler by its own name, or as “GCC”. Either is correct. G++ is a compiler, not merely a preprocessor. G++ builds object code directly from C++ program source. There is no intermediate C version of the program. Avoiding an intermediate C representation of the program means that it is possible to get better object code, and better debugging information. The GNU debugger, GDB, works with this information in the object code to give a comprehensive C++ source-level editing capabilities.
  • 5. GCC Command Options When GCC invoked, it normally does preprocessing, compilation, assembly and linking. The “overall options” allows to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler. Other options are passed on to one stage of processing. Some options control the preprocessor and others the compiler itself. Yet other options control the assembler and linker. Most of the command line options those used with GCC are useful for C programs; when an option is only useful with another language (usually C++), the explanation says so explicitly. If the description for a particular option does not mention a source language, it is possible to use that option with all supported languages.
  • 6. GCC Command Options (2) The GCC compiler accepts both single-letter options, such as -o, and multi-letters, such as -ansi. Because it accepts both types of options, it is not possible to group multiple single-letter options together as it may be done in many GNU and Unix/Linux programs. For example, the multi-letter option -pg is not the same as the two single-letter options -p -g. The -pg option creates extra code in the final binary that outputs profile information for the GNU code profiler, gprof. On the other hand, the -p -g options generate extra code in the resulting binary that produces profiling information for use by the prof code profiler (-p) and causes GCC to generate debugging information using the operating system’s normal format (-g).
  • 7. GCC Command Options (3) Despite its sensitivity to the grouping of multiple single-letter options, it is possible to mix the order of options and compiler arguments on the GCC command line. That is, invoking GCC as gcc -pg -fno-strength-reduce -g myprog.c -o myprog has the same result as gcc myprog.c -o myprog -g -fno-strength-reduce –pg In some situations, order does matter if several options of the same kind been used. For example, the -I option specifies the directory or directories to search for include files. So if -I specified several times, GCC searches the listed directories in the order specified.
  • 8. GCC Command Options (4) To compile a single source file, invoke GCC passing the name of the source file as the argument. $ gcc myprog.c $ ls –l By default, the result on Linux and Unix systems is an executable file named a.out in the current directory, which you execute by typing ./a.out. On Cygwin systems, you will wind up with a file named a.exe that you can execute by typing either ./a or ./a.exe. To define the name of the output file that GCC produces, use the -o option, as illustrated in the following example: $ gcc myprog.c -o runme
  • 9. GCC Command Options (5) For compiling multiple source files, they may simply specified on the GCC command line, as in the following example, which leaves the compiled and linked executable in the file named showdate: $ gcc showdate.c helper.c –o showdate To compile these files incrementally and eventually to link them into a binary, it is possible to use the –c option to halt compilation after producing an object file, as in the following example: $ gcc -c showdate.c $ gcc -c helper.c $ gcc showdate.o helper.o –o showdate
  • 10. G++ Command Options The G++ compiler also accepts both single-letter options and multi-letter options as GCC. And also it is not possible to group multiple single-letter options due to fact that it accepts both types of options together. Despite its sensitivity to the grouping of multiple single-letter options, it is free to mix the order of options and compiler arguments on the command line. That is, invoking G++ as g++ -pg -fno-strength-reduce -g myprog.c -o myprog has the same result as g++ myprog.c -o myprog -g -fno-strength-reduce -pg
  • 11. G++ Command Options (2) In some situations, order does matter if several options of the same kind been used. For example, the -I option specifies the directory or directories to search for include files. So if -I specified several times, it searches the listed directories in the order specified. To compile a single source file, myprog.cc, just invoke G++, passing the name of the source file as the argument. $ g++ myprog.cc $ ls -l
  • 12. G++ Command Options (3) By default, the result on Linux and Unix systems is an executable file named a.out in the current directory, which may be executed by typing ./a.out. On Cygwin systems, result is a file named a.exe that you can execute by typing either ./a or ./a.exe. To define the name of the output file that G++ produces, use the -o option, as illustrated in the following example: $ g++ myprog.cc -o runme $ ls –l For compiling multiple source files using G++, and to get file named showdate: $ g++ showdate.cc helper.cc –o showdate
  • 13. G++ Command Options (4) If you want to compile these files incrementally and eventually link them into a binary, you can use the –c option to halt compilation after producing an object file, as in the following example: $ g++ -c showdate.cc $ g++ -c helper.cc $ g++ showdate.o helper.o –o showdate $ ls –l
  • 14. G++ Command Options (5) All GCC compilers evaluate filename suffixes to identify the type of compilation that they will perform. Suffix Operation .C C++ source code to preprocess. .cc C++ source code to preprocess. (Standard extension for C++ source files.) .cpp C++ source code to preprocess. .cp C++ source code to preprocess. .c++ C++ source code to preprocess .cxx C++ source code to preprocess .ii C++ source code not to preprocess.
  • 15. Options Summary Here is a summary of all the options, grouped by type. Overall Options: Controlling the kind of output: an executable, object files, assembler files, or preprocessed source. C Dialect Options: Controlling the variant of C language compiled. C++ Dialect Options: Variations on C++. Objective-C Dialect Options: Variations on Objective-C. Language Independent Options: Controlling how diagnostics should be formatted. Warning Options: How picky should the compiler be?
  • 16. Options Summary (2) Debugging Options: Symbol tables, measurements, and debugging dumps. Optimize Options: How much optimization? Preprocessor Options: Controlling header files and macro definitions. Also, getting dependency information for Make. Assembler Options: Passing options to the assembler. Link Options: Specifying libraries and so on. Directory Options: Where to find header files and libraries. Where to find the compiler executable files. Spec Files: How to pass switches to sub-processes. Target Options: Running a cross-compiler, or an old version of GCC.
  • 17. Optimization of Code with GCC These days, compilers are pretty smart. They can perform all sorts of code transformations make compiled code run faster. In most situations, faster is better than smaller, because disk space and memory are quite cheap for desktop users. However, for embedded systems small is often at least as important as fast because of a commonplace environment consisting of extreme memory constraints and no disk space, making code optimization a very important task. In the absence of optimization, GCC’s goal, besides compiling code that works, is to keep compilation time to a minimum and generate code that runs predictably in a debugging environment.
  • 18. Optimization Theory Optimization refers to analyzing a straightforward compilation’s resulting code to determine how to transform it to run faster, consume fewer resources, or both. Compilers that do this are known as optimizing compilers, and the resulting code is known as optimized code. To produce optimized code, an optimizing compiler performs one or more transformations on the source code provided as input. The purpose is to replace less efficient code with more efficient code while at the same time preserving the meaning and ultimate results of the code.
  • 19. Optimization Theory (2) Optimizing compilers use several methods to determine where generated code can be improved. One method is control flow analysis, which examines loops and other control constructs, such as if-then and case statements, to identify the execution paths a program might take and, based on this analysis, determine how the execution path can be streamlined. Another typical optimization technique examines how data is used in a program, a procedure known as data flow analysis. Data flow analysis examines how and when variables are used (or not) in a program and then applies various set equations to these usage patterns to derive optimization opportunities.
  • 20. Optimization Techniques – Code Motion It is an optimization technique to eliminate redundant code through common sub- expression elimination. It does not entirely remove common sub-expressions, but attempts to reduce the number of times they occur by relocating them to more optimal locations in an intermediate representation of the code. For example, in nested loops or other control constructs, intermediate value calculations may be performed more times than necessary. In order to optimize, compilers can move these calculations to locations where they will be executed less frequently, but will still produce the same result. Code motion that specifically moves redundant calculations outside the scope of a loop is referred to as loop-invariant code motion. It is also used in a technique that is related to common sub-expression elimination known as partial redundancy elimination.
  • 21. Optimization Techniques – Common Sub-expression Elimination Eliminating redundant computations is a standard optimization mechanism because it reduces the number of instructions that a program has to execute in order to arrive at the same result. For example, given an expression whose value is computed only once, each subsequent occurrence of that expression can be eliminated and its value used instead, if the values of the variables in the expression have not changed since first computed. The subsequent occurrences of the expression are called common sub-expressions.
  • 22. Optimization Techniques – Common Sub-expression Elimination (2) Example: #define STEP 3 #define SIZE 100 int i, j, k; int p[SIZE]; for (i = 0; i < SIZE; ++i) { j = 2 * STEP; k = p[i] * j; } The expression j = 2 * STEP in the for loop is a common sub-expression because its value is computed before entering the loop and its constituent variable, STEP (actually a predefined value), has not changed.
  • 23. Optimization Techniques – Common Sub-expression Elimination (3) Common sub-expression elimination (CSE) removes the repeated computation of j in the for loop. After CSE, the for loop might look like the following: j = 2 * STEP; for (i = 0; i < 100; ++i) { k = p[i] * j; } Admittedly, the example is contrived, but it makes the point: CSE has eliminated 100 redundant computations of j. CSE improves performance by eliminating unnecessary computations and typically also reduces the size of the resulting binary by eliminating unnecessary instructions.
  • 24. Optimization Techniques – Constant Folding It is an optimization technique that eliminates expressions that calculate a value that can already be determined when the program is compiled. These are typically calculations that only reference constant values or expressions that reference variables whose values are constant. For example: n = 10 * 20 * 400; i = 10; j = 20; ij = i * j; In the latter case, the assignments to the variables i and j could also be eliminated if these variables were not used elsewhere in the code.
  • 25. Optimization Techniques – Copy Propagation Transformations It eliminate cases in which values are copied from one location or variable to another in order to simply assign their value to another variable. Given an assignment of the form f = g, subsequent uses of f, called copies, use g instead. Consider a code fragment such as the following: i = 10; x[j] = i; y[MIN] = b; b = i; //After CPT it looks like “b=10”
  • 26. Optimization Techniques – Dead Code Elimination Dead code elimination (DCE) is the term used for optimizations that remove code that doesn’t actually do anything permanent or useful. Unreachable code elimination is a related optimization that removes code that the compiler can identify as impossible to reach. For example: if ( i == 10 ) { . . . } else { . . . if ( i == 10) { . . . } } The nested test for whether i is equal to 10 can be eliminated because it can never be reached.
  • 27. Optimization Techniques – If- Conversion If-conversion is a technique where branch constructs, such as large if-then-else if-else constructs, are broken into separate if statements to simplify generated code, provide opportunities for further optimization, and eliminate jumps and branches wherever possible.
  • 28. Optimization Techniques – Inlining It is an optimization technique where code performance can improve by replacing complex constructs and even function calls with inline representations of the construct or function call. Code inlining, or loop unrolling, are terms for replacing all or portions of a loop with an explicit series of instructions. Function inlining is the term for replacing function calls with the explicit set of instructions that are performed by the function. In general, inlining can reduce code complexity and provide higher performance than would be required by branching that would be done otherwise. It often also provides opportunities for common sub-expression elimination and code motion optimization.
  • 29. GCC Optimization Basics GCC uses a sequence of different intermediate representations of code before generating binaries. Converting language-specific code into simpler forms provides several primary advantages: - Breaking code into simpler, more low-level constructs exposes opportunities for optimization that may not be directly visible in source code. - Using intermediate representations enables the simpler form to more easily and readably represent parse trees that are otherwise at varying levels of complexity. - Using a simpler form enables the GCC compilers to share optimization mechanisms, regardless of the language in which your code was originally written.
  • 30. GCC Optimization Basics (2) Traditionally, GCC has always internally used an intermediate form known as Register Transfer Language (RTL), which is a very low-level language into which all code compiled with a GCC compiler (in any high-level language) is transformed before object code generation begins. Transforming high-level input languages into an intermediate, internal form is a time-tested mechanism for exposing opportunities for optimization. As a very low-level representation of code, GCC’s RTL lends itself well to optimizations that are similarly low-level, such as register allocation and stack and data optimizations. Because it is relatively low-level, RTL is not as good as one would like in terms of supporting higher-level optimizations related to data types, array and variable references, and overall data and control flow within code.
  • 31. GCC Optimization Basics (3) With GCC 4.0, the fine makers of the GCC compilers introduced a new intermediate form, static single assignment (SSA), which operates on the parse trees that are produced from code by GCC compilers, and is thus known as Tree SSA. It is interesting to note that 4.0 and later GCC compilers use two intermediate forms before arriving at a Tree SSA representation, known as GENERIC and GIMPLE. A GENERIC representation is produced by eliminating language-specific constructs from the parse tree that is generated from code, and a GIMPLE representation is then produced from the GENERIC representation by simplifying address references within the code. It provides several additional points at which optimizations can occur at as high a level as possible before zooming into the depths of the RTL.
  • 32. Architecture-Independent Optimizations GCC’s most well-known optimization switches are –O; its variant -On, where n is an integer between 0 and 3; and -Os. -OO turns off optimization. -O and -O1 (which are level 1) are equivalent, telling GCC to optimize code. With -O or -O1, the compiler attempts to minimize both code size and execution time without dramatically increasing compilation time. Using -O2 and -O3 will increase the level from that requested by - O1, while still invoking the optimization options requested by -O1. To minimize code size, use option -Os. To disable one of them while leaving the others enabled, negate the option using no- between -f and the optimization name. For example, to disable deferred stack pops, the command line might resemble this: $ gcc myprog.c -o myprog -O1 -fno-defer-pop
  • 36. Architecture-Independent Level 2 GCC Optimizations -O2 optimization (level 2 optimization) includes all level 1 optimizations plus the additional optimizations listed below. Applying these optimizations will lengthen the compile time, but as a result a measurable increase should be seen in the resulting code’s performance, or, rather, a measurable decrease in execution time.
  • 40. Architecture-Independent GCC Optimizations for Code Size The -Os option is becoming increasingly popular because it applies all of the level 2 optimizations except those known to increase the code size. -Os also applies additional techniques to attempt to reduce the code size. Code size, in this context, refers to a program’s memory footprint at runtime rather than its on-disk storage requirement. In particular, -Os disables the following optimization flags (meaning they will be ignored if specified in conjunction with -Os): • -falign-functions • -falign-jumps • -falign-labels • -falign-loops
  • 41. Architecture-Independent GCC Optimizations for Code Size (2) • -fprefetch-loop-arrays • -freorder-blocks • -freorder-blocks-and-partition • -ftree-ch You might find it instructive to compile a program with -O2 and -Os and compare runtime performance and memory footprint. For example, I have found that recent versions of the Linux kernel have nearly the same runtime performance when compiled with -O2 and - Os, but the runtime memory footprint is 15 percent smaller when the kernel is compiled with -Os. Naturally, your mileage may vary and, as always, if it breaks, you get to keep all of the pieces.
  • 42. Architecture-Independent Level 3 GCC Optimizations Specifying the –O3 optimization option enables all level 1 and level 2 optimizations plus the following: • -fgcse-after-reload: Performs an extra load elimination pass after reloading • -finline-functions: Integrates all “simple” functions into their callers • -funswitch-loops: Moves branches with loop invariant conditions out of loops Note: If you specify multiple -O options, the last one encountered takes precedence. Thus, given the command gcc -O3 foo.c bar.c -O0 -o baz, no optimization will be performed because -O0 overrides the earlier -O3.
  • 43. Architecture-Independent Manuel GCC Optimization Flags In addition to the optimizations enabled using one of the -O options, GCC has a number of specialized optimizations that can only be enabled by specifically requesting them using -f.
  • 44. Architecture-Independent Manuel GCC Optimization Flags (2) Note: Not all of GCC’s optimizations can be controlled using a flag. GCC performs some optimizations automatically and we cannot disable these optimizations when you request optimization using -O.
  • 45. Processor-Specific Optimizations In reality, though, the target-specific switches are not properly optimizations in the traditional sense. Rather, they are options that give GCC more information about the type of system on which the code being compiled will run. GCC can use this additional information to generate code that takes advantage of specific features or that works around known misfeatures of the given processor.