GCC: the GNU Compiler Collection

Slides:



Advertisements
Similar presentations
CSE 303 Lecture 16 Multi-file (larger) programs
Advertisements

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
August 7, 2003Serguei A. Mokhov, 1 gcc Tutorial COMP 444/5201 Revision 1.1 Date: January 25, 2004.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Programming With C.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
UNIT 13 Separate Compilation.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
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.
CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
Programs – Preprocessing, Compilation and Linking
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Introduction to C Topics Compilation Using the gcc Compiler
The First Real Bug.
UMBC CMSC 104 – Section 01, Fall 2016
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
User-Written Functions
The Machine Model Memory
Executable program (p1)
A bit of C programming Lecture 3 Uli Raich.
FUNCTIONS In C++.
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.
Introduction to C Topics Compilation Using the gcc Compiler
GCC: the GNU Compiler Collection
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 Systems and Networks
Programmazione I a.a. 2017/2018.
Choice of Programming Language
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
C Programming Lecture Series
Functions Inputs Output
Today’s Topic Breakdown of CC script.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
A First Book of ANSI C Fourth Edition
C++ Compilation Model C++ is a compiled language
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 11 Introduction to Programming in C
Introduction to C Topics Compilation Using the gcc Compiler
Appendix F C Programming Environment on UNIX Systems
Running a Java Program using Blue Jay.
Introduction to C Topics Compilation Using the gcc Compiler
Debugging.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Executable program (p1)
Chapter 2 part #1 C++ Program Structure
SPL – PS1 Introduction to C++.
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Presentation transcript:

GCC: the GNU Compiler Collection We will be primarily concerned with the C compiler, gcc. The program gcc is actually a front-end for a suite of programming tools. For the purposes of CS 2505, the underlying tools include: cpp the GNU C preprocessor cc the GNU C language compiler as the GNU assembler ld the GNU linker We will begin by considering only the use of gcc itself.

Getting Started with gcc Download the example caesar.c from the course website if you want to follow along with the following examples. Execute the following command: gcc caesar.c You should not get any messages; list the files in your directory and you'll find a new file named a.out – that's the executable file produced by gcc. Execute the command a.out; you should see a message from the program showing how to invoke it correctly. Execute the command a.out with valid parameters, say: a.out 3 AMansAManForAThat.txt and you should see the modified file contents, echoed to the console window.

Fundamental gcc options: -o First of all, the default name for the executable file is a.out, which is both strange and unsatisfying. Use the -o option to specify the name you want to be given to the executable: gcc -o caesar caesar.c 1030 wmcquain in caesar> ls -l total 8 -rw-rw-r--. 1 wmcquain comporg 1426 Sep 13 21:34 AMansAManForAThat.txt -rw-rw-r--. 1 wmcquain comporg 2222 Sep 13 21:50 caesar.c 1031 wmcquain in caesar> gcc -o caesar caesar.c 1032 wmcquain in caesar> ls -l total 20 -rwxrw-r--. 1 wmcquain comporg 9119 Sep 13 21:51 caesar 1033 wmcquain in caesar> Side note: as is often the case, the space after the -o option is optional.

Fundamental gcc options: -Wall Use the -Wall option to direct gcc to display all relevant warning messages: gcc -o caesar -Wall caesar.c 1035 wmcquain in caesar> gcc -o caesar -Wall caesar.c caesar.c: In function ‘processFile’: caesar.c:43:7: warning: implicit declaration of function ‘isalpha’ [-Wimplicit-function-declaration] if ( isalpha(nextIn) ) { ^ caesar.c: In function ‘checkShiftAmt’: caesar.c:86:8: warning: unused variable ‘result’ [-Wunused-variable] int result = (int) strtol(src, &p, 10); 1036 wmcquain in caesar> So, the supplied C code compiles, but does not compile cleanly. The first message tells us that at line 43 of the file caesar.c, in the function processFile, we have called a function isalpha that has not been declared explicitly. All too true. The Standard Library function isalpha is declared in the header file ctype.h and the supplied code doesn't have an include directive for that header; in this case, we got away with that, but the issue should be fixed.

Fundamental gcc options: -Wall 1035 wmcquain in caesar> gcc -o caesar -Wall caesar.c . . . caesar.c: In function ‘checkShiftAmt’: caesar.c:86:8: warning: unused variable ‘result’ [-Wunused-variable] int result = (int) strtol(src, &p, 10); ^ 1036 wmcquain in caesar> The next message tells us that at line 86 of the file caesar.c, in the function checkShiftAmt, we have declared a variable result whose value is never used. Again, this is true. However, in this case the variable was used in order to capture the return value from the library function strtol, which we do not make any further use of. This is deliberate, and fits with the design of the function checkShiftAmt, and so we'll leave it unaltered.

More gcc warning options You may also turn on certain specific categories of warnings if -Wall is too intrusive: -Wformat warnings regarding mismatches between format specifiers and arguments -Wunused warnings regarding unused variables -Wimplicit warnings regarding functions that are called without being declared; usually results from missing include directives or misspellings -Wconversion warnings regarding implicit conversions that could result in errors -Wshadow warnings regarding name hiding -W a variety of additional warnings not included in -Wall

Fundamental gcc options: -W 1039 wmcquain in caesar> gcc -o caesar -Wall -W caesar.c caesar.c: In function ‘applyShift’: caesar.c:57:36: warning: unused parameter ‘shiftAmt’ [-Wunused-parameter] char applyShift(char Original, int shiftAmt) { . . . 1040 wmcquain@centosvm in ~/2505/notes/T05/caesar> Now, we see an additional warning, and this one is somewhat alarming... Why would a function receive a parameter and not use it? Sounds like a possible design error... (In this case, the function applyShift() has not been completely implemented.

A Not So Benign Warning Message // Adapted from An Introduction to GCC, Gough, p. 8 #include <stdio.h> int main() { int a = 5, b = 10; printf("The sum of %d and %d is %f.\n", a, b, a + b); return 0; } 1049 wmcquain in gough> gcc -o badformat -Wall badformat.c badformat.c: In function ‘main’: badformat.c:7:4: warning: format ‘%f’ expects argument of type ‘double’, but argument 4 has type ‘int’ [-Wformat=] printf("The sum of %d and %d is %f.\n", a, b, a + b); ^ 1050 wmcquain in gough> badformat The sum of 5 and 10 is 0.000000.

Compliance-related gcc options: -std Use the -std option to direct gcc to require code to comply with a particular C langauge standard: gcc -o caesar –Wall –std=c11 caesar.c This is necessary in order to use some features added in C11 (such as declaring for-loop counters within the loop header). It is also necessary with some legacy code in order to sidestep requirements imposed by newer standards. Unless explicitly stated otherwise, in CS 2505 we will always specify compliance with the C99 standard (as shown above).

GCC Reference Manual The official reference manual for GCC is available at: http://gcc.gnu.org/onlinedocs/ This is a very useful, if somewhat verbose, resource.

Behind the Scenes Executing gcc with the -save-temps option results in the preservation of some temporary intermediate files created by/for the underlying tools. For example if you use the file caesar.c: caesar.i written by the preprocessor; input to the compiler caesar.s written by the compiler; input to the assembler caesar.o written by the assembler; input to the linker caesar written by the linker By default, only the final, executable file is preserved once the process is complete. We will gradually see that the intermediate files are occasionally of use, if for no reason than that they shed light on the actual process of program translation from a high-level language to the machine level.

cpp caesar.c > caesar.i The Preprocessor: cpp Try executing the command cpp caesar.c > caesar.i cpp writes its output to standard output; this redirects it into a (new) file named caesar.i. If you examine this (text) file, the first 2000 or so lines indicate the processing of the include directives in the source file; so declarations from those files are available to the compiler. At the end of the file, you will find a modified copy of the original source: - all the comments have been stripped out - the values that were defined in the source file have been substituted into the source code

The Compiler: cc Now, try executing the command cc –S caesar.i With the -S option (case-sensitive!), the compiler writes its output to a file; the name is generated from the name of the input file; in this case, the output is written to caesar.s. This file contains the assembly code generated by the compiler from the pre-processed C source code.

The Assembler: as Now, try executing the command as –o caesar.o caesar.s The assembler writes its output to a.out by default; the name can be specified using the option -o, as with gcc. This file contains a partial translation of the assembly code into native machine language code; calls to library functions haven't been resolved completely since the code for those is not in the local source file.

The Linker: ld Now, try executing the command ld caesar.o The linker will complain about a large number of undefined references because it doesn't know where to find the system files that contain the implementations of the relevant features in the Standard Library. Fortunately, gcc takes care of these settings for us and invokes the linker as needed: gcc –o caesar caesar.o

More useful gcc options You may also turn on certain specific categories of warnings if -Wall is too intrusive: -lm required in order to use floating-point functions in the Library -std=c11 required to use features specific to the C11 Standard other options include: c89, c99 -On choose optimization level n (0, 1, 2 or 3); use –O0 for debugging -m32, -m64 build 32-bit or 64-bit executable (we use only 64-bit code here) -g, -ggdb, -ggdb3 generate debugging information to aid gdb