1 / 26

CPS120: Introduction to Computer Science

CPS120: Introduction to Computer Science. Compiling a C++ Program From The Command Line. Compilers. A compiler is a program that reads source code, which is the C++ code written by a programmer, and produces an executable or binary file

Download Presentation

CPS120: Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPS120: Introduction to Computer Science Compiling a C++ Program From The Command Line

  2. Compilers • A compiler is a program that reads source code, which is the C++ code written by a programmer, and produces an executable or binary file • A Compiler is an important tool that can check your syntax, issue warnings about questionable usage, indicate errors, and allow you to create binaries or executables that can run to test your program's logic

  3. Command Line Compiles • You'll eventually use a more sophisticated interface (an IDE - Integrated Development Environment) such as Visual C++ • The common denominator you'll always find is the plain command line interface. • Even if you use an IDE, it could help you understand how things work "behind the scenes"

  4. Editors • He first step in creating a C++ program is writing the source code. • This, you will need access to a text editor • Unix • VI • EMACS • PICO • Windows • NotePad • WordPad

  5. Using Unix at WCC • START • PROGRAMS • NET TOOLS • TERRATERM PRO

  6. Using Unix at WCC Host: cidermill.wccnet.org • TCP/IP TCP PORT #23 OTHER * • OK • Login: pmillis • PW: ************ • PICO (or EMACS or VI)

  7. Entering Your Program • Type it into your editor and save the file as myprog.cpp #include <iostream.h> int main() { cout << "Hello World\n" ; return 0; }

  8. Compiling • The source file is a plain text file containing your code. • The executable file, consist of machine codes, 1's and 0's, that are not meant to be understood or read by people, but only by computers

  9. The Compiling Process • The compilation is split in to many different phases • Driver - The "engine", that drives the whole set of tools the compiler is made of. We invoke it, and it begins to invoke the other tools one by one, passing the output of each tool as an input to the next tool. • C++ Pre-Processor - It takes a C source file, and handles all the pre-processor definitions (#include files, #define macros, conditional source code inclusion with #ifdef, etc.)

  10. The Compiling Process • The C++ Compiler - This is the actual compiler, that translates the input file into assembly language. • Optimizer - This one handles the optimization on a representation of the code that is language-neutral. • Assembler - This takes the assembly code generated by the compiler, and translates it into machine language code kept in object files. • Linker-Loader - This is the tool that takes all the object files (and C++ libraries), and links them together, to form one executable file, in a format the operating system supports.

  11. Why the Complexity? • The basic idea is quite simple - split the compiler into many different parts to give the programmer more flexibility and to allow the compiler developers to re-use as many modules as possible

  12. C++ Compilers • easiest case of compilation is when you have all your source code set in a single file • C++ compiler is rally named c++ or g++ "g++ source file" • You may see some errors or warning messages if you typed something incorrectly in your source file. • If so, correct your errors in the source file, using the text editor and recompile. • In all cases, you'll get a file 'a.out' as a result, if the compilation completed successfully

  13. C++ Compilers • To compile, you type, at the prompt "g++ source file -o executable_file " • Every compiler recognizes the '-o' flag as specifying the name the resulting executable file.

  14. Running the Program • Simply type myprog at the command line • This requires that the current directory be in our PATH. In many cases, this directory is not placed in our PATH. We specify it explicitly: ./myprog • This time we explicitly told our Unix shell that we want to run the program from the current directory. • One more obstacle could block our path - file permission flags.

  15. File Permissions • Normally you'll have no problem running the file, but if you copy it to a different directory, or transfer it to a different computer over the network, it might loose its original permissions, and thus you'll need to set them properly, as shown: chmod u+rwx myprog

  16. Debuggable Code • Normally, when we write a program, we want to be able to debug it. • We need to tell the compiler to insert information to the resulting executable program that'll help the debugger g++ -g myprog.cpp -o myprog • The '-g' flag tells the compiler to use debug info

  17. Removing the Debugging • Note that the resulting file is much larger then that created without usage of the '-g' flag. • The difference in size is due to the debug information. We may still remove this debug information using the strip command, like this: strip myprog • You'll note that the size of the file now is even smaller then if we didn't use the '-g' flag in the first place

  18. Creating Optimized Code • The basic way to create an optimized program would be like this: g++ -O myprog.cpp -o myprog The '-O' flag tells the compiler to optimize the code.

  19. Getting Extra Compiler Warnings • To get the compiler to use all types of warnings it is familiar with, we'll use a command line like this: g++ -Wall myprog.cpp -o myprog

More Related