ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015...
-
Upload
samuel-leonard -
Category
Documents
-
view
213 -
download
1
Transcript of ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015...
ECE 103 Engineering ProgrammingChapter 9
gcc Compiler
Herbert G. Mayer, PSU CSStatus 6/19/2015
Initial content copied verbatim fromECE 103 material developed by
Professor Phillip Wong @ PSU ECE
Syllabus What’s This Blue Code? Compile C Source Code Execute Generated Object Code
3
What’s This Blue Code?#include <stdio.h> // for printf()#define Max 4
short zag = -1;short prime[ Max ] = { 3, 5, 7, 11 }; // initializationvoid print_prime(){ // print_prime short i; for( i = 0; i < Max; i++ ) { printf( " prime[%1d] = %2d\n", i, prime[ i ] ); } //end for printf( " zag = %d\n\n", zag );} //end print_prime
int one(){ // one zag++; // side-effect return 1;} //end one
main( ){ // main print_prime(); prime[ zag + one() ] = prime[ zag + one() ]; // left or right first? print_prime();} //end main
4
Compile Source Code
The GNU Project provides a freely available, open source C compiler called gcc
Syntax for compiling a program (command line):
gcc [options] file_list
where:gcc → compiler name (in lowercase)[options] → compilation options (optional)file_list → list of source and/or object files
5
Typical GCC compilation options:
Option Description
-std=xxx
–std=c90 to compile to ISO C90 standard–std=c99 to compile to ISO C99 standard–std=gnu90 to compile to GNU C90 standard–std=gnu99 to compile to GNU C99 standard
-ansi Same effect as –std=c90
-pedantic Issue warning if C source is not strictly ISO compliant
-Wall Issue warnings on all questionable C constructs
-lmLink to the standard math library(required for some UNIX systems)
-o exec_file
Determines the name of the executable file(can be different from the source file name)
If -o is omitted, the default name is a.out (or a.exe).
6
Example:Your source file is called prog1.c. Your compiler generates a default executable file named a.out
Type this at the command line (no extra checking):
gcc prog1.c
-or-
Type this at the command line (full checking):
gcc -ansi -pedantic -Wall prog1.c
7
Example:Your source file is called mathprog.c. But you prefer the executable file to be named mprog.
Then type this command:
gcc -ansi -pedantic -Wall mathprog.c –o mprog
8
Example:Your source files are ted.c and alice.c. You want the executable file to be named jupiter2.
Type this at the command line:
gcc -ansi -pedantic -Wall ted.c alice.c –o jupiter2
9
Execute Generated Object Code
If the source file is compiled successfully, an executable (binary) file is created
The executable file can be run from the command line in Unix-like environments
Syntax for running the executable file:
efilename
where efilename is the name of the actual file you want to run.
10
If the operating system complains that it cannot find the executable file in the current directory: Prefix the file's name with "./" Syntax: ./efilename
11
Example1:Suppose your executable file is called a.out and it is stored in the current directory
To run it, type the following: ./a.out
On Unix, this also works: a.out
Example2:Suppose your executable file is called prog1 and it is stored in subdirectory HW2
To run it, type the following: HW2/prog1