the gnu c compiler
TRANSCRIPT
The GNU C Compiler
José Dapena Paz <[email protected]>
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/
Goals of this tutorial
● Understand how to compile a program.● Use basic compiler options for optimisation and
debugging.
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).
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.
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
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)
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.
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.
Headers
● If you forget to declare methods before a call, be careful. It will use a “default declaration”.
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
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.
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
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).
Optimisation
● There are different levels:– Safe: -O2
– No optimisation (should use for debug) -O0
– -Os optimises for size of code
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)
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