the gnu c compiler

17
The GNU C Compiler José Dapena Paz <[email protected]>

Upload: andres-maneiro

Post on 19-May-2015

1.276 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: The Gnu C Compiler

The GNU C Compiler

José Dapena Paz <[email protected]>

Page 2: The Gnu C Compiler

The GNU C Compiler

Tutorial based on this book:

An introduction to GCCfor the GNU Compilers gcc and g++

Brian Gough

This is available on network. More information on:

http://www.network-theory.co.uk/gcc/intro/

Page 3: The Gnu C Compiler

Goals of this tutorial

● Understand how to compile a program.● Use basic compiler options for optimisation and

debugging.

Page 4: The Gnu C Compiler

Compiling a C program

Compiling a C program:

$ gcc -Wall hello.c -o hello

Recommendation: use always -Wall:● Nested comments● Incorrect printf/scanf formats● Unused variables● Usage of implicit (non-declared) functions● Function return types.● Other checks... (see GCC reference manual).

Page 5: The Gnu C Compiler

Other warning options

● -W: common programming errors● Used frequently with -Wall.● Missing return values● Comparison between signed and unsigned.

● -Wconversion: type conversions with unexpected results.

● -Wshadow: variables in a scope hidden variables in a wider scope.

● -Wcast-qual: casts removing pointer qualifiers.

Page 6: The Gnu C Compiler

Other warning options

● -Wwrite-strings● Warns for attempts to modify constant strings

● -Wtraditional● Parts of code that can be interpreted differently

depending on standard.● Standard used can be established with parameters:

– -ansi (ANSI/ISO)– -ansi -pedantic (strict ANSI)– -std=STANDARDNAME for other standards

Page 7: The Gnu C Compiler

Reading errors

Example of error:

File, function, and then.File, number of line, criticity, error description.

$ gcc -Wall bad.c -o badbad.c: In function ‘main’:bad.c:6: warning: double format, different type arg (arg 2)

Page 8: The Gnu C Compiler

Compiling source files

● All source files in one step:– gcc -Wall -o output file1.c file2.c

● One source file per steop:– gcc -Wall -c file1.c # creates file1.o

– gcc -Wall -c file2.c # creates file2.o

– gcc -o output file1.o file2.o

● -c does not require a final output. -o sets the output executable.

Page 9: The Gnu C Compiler

Linking

● Object file defining a function should appear after all files using it.

● Linking external libraries:– gcc -Wall calc.c /usr/lib/libm.a -o calc

– gcc -Wall calc.c -lm -o calc

● -l searches libraries in default library paths. Linking order is the same.

Page 10: The Gnu C Compiler

Headers

● If you forget to declare methods before a call, be careful. It will use a “default declaration”.

Page 11: The Gnu C Compiler

Search paths

● Library search path:● gcc ... -L/opt/lib● Default path: /usr/lib, /usr/local/lib● Environment:

– LIBRARY_PATH

● Include search path:● gcc ... -I/opt/include● Default path: /usr/include, /usr/local/include● Environment:

– INCLUDE_PATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH

Page 12: The Gnu C Compiler

Shared libraries and static libraries

● Shared libraries: they're not included in the executable. Static libraries: they're attached to the executable.

● Using shared libraries● Variable LD_LIBRARY_PATH● With -llibrary, it tries first for shared libraries (.so instead

of .a).

● Forcing static linking:● gcc -static -ll1 -ll2 file.c -o output will link l1.a, and l2.a

and attach them to the output file.

Page 13: The Gnu C Compiler

Preprocessor

● Defining macros:● gcc -DFOO=1 or gcc -DFOO (equivalents as an empty

define defaults to 1).● Can be used in code as a constant, in ifdefs, etc (just as

if you did #define FOO 1 in the start of your .c, before includes).

● Test preprocessor output using gcc -E

Page 14: The Gnu C Compiler

Debugging

● Debug information can be included in output executables:

● gcc -g file.c -o output● With this gdb will give debug output.● -g3 also adds preprocessor symbols● Remove optimisations (-O0).

Page 15: The Gnu C Compiler

Optimisation

● There are different levels:– Safe: -O2

– No optimisation (should use for debug) -O0

– -Os optimises for size of code

Page 16: The Gnu C Compiler

Other features

● Coverage (gcov): adds information for statistics of code usage (gcc -fprofile-arcs -ftest-coverage)

● Profiling (gprof): adds information for profile information. (gcc -pg)

Page 17: The Gnu C Compiler

How to create a static library

● Compile .o files ● gcc -Wall -c file.c

● Join them with ar:● ar cr libtest.a test1.o test2.o