Cps120 introduction to computer science
Download
1 / 26

CPS120: Introduction to Computer Science - PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CPS120: Introduction to Computer Science' - candace-hanson


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Cps120 introduction to computer science

CPS120: Introduction to Computer Science

Compiling a C++ Program

From The Command Line


Compilers
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


Command line compiles
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"


Editors
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


Using unix at wcc
Using Unix at WCC

  • START

  • PROGRAMS

  • NET TOOLS

  • TERRATERM PRO


Using unix at wcc1
Using Unix at WCC

Host: cidermill.wccnet.org

  • TCP/IP

    TCP PORT #23

    OTHER *

  • OK

  • Login: pmillis

  • PW: ************

  • PICO (or EMACS or VI)


Entering your program
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;

    }


Compiling
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


The compiling process
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.)


The compiling process1
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.


Why the complexity
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


C compilers
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


C compilers1
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.


Running the program
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.


File permissions
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


Debuggable code
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


Removing the debugging
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


Creating optimized code
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.


Getting extra compiler warnings
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