110 likes | 237 Views
ECE 103 Engineering Programming Chapter 9 gcc Compiler. Herbert G. Mayer, PSU CS Status 6/30/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE. Syllabus. What ’ s This Blue Code? Compile C Source Code Execute Generated Object Code.
E N D
ECE 103 Engineering ProgrammingChapter 9gcc Compiler Herbert G. Mayer, PSU CS Status 6/30/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE
Syllabus • What’s This Blue Code? • Compile C Source Code • Execute Generated Object Code
What’s This Blue Code? #include <stdio.h> // for printf() #define Max 4 short zag = -1; short prime[ Max ] = { 3, 5, 7, 11 }; // initialization void 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
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 3
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 5
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 6
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 7
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. 8
If the operating system complains that it cannot find the executable file in the current directory: Prefix the file's name with "./" Syntax: ./efilename 9
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 10