CPSC 427: Object-Oriented Programming
Michael J. Fischer
Tasks for this week
C++ Overview
Why did C need a ++?
Chapter 2 of Exploring C++
Today, we demand much more from a language.
C++ was Designed for Modeling Design goals for C++ (Bjarne Stroustrup)
General properties of C++
C++ Extends C
Some Extensions in C++
Building a Project
Compilation modules
An application (or executable or command file) is built from a
number compilation modules, also called object files or .o files.
Often, .o files are packed together into library files, which have
extensions .a or .so. Think of these files as components of the finished
application.
Modules are joined together during final assembly of the application. This step of the process is called linking.
Building
compilation
modules
Modules are built from implementation files, also called code files, or
.cpp files. These are the files that contain executable C++ code. A .cpp
source file can be compiled to produce a corresponding .o object
file.
Object files can be produced by different programmers at different times.
Many of the modules you will be using come pre-compiled and
pre-installed on your machine. Only during linking do all of the required
object files and libraries need to be collected together.
System libraries are often found in directories /lib, /usr/lib, or /usr/lib64, but they can be placed anywhere as long as the linker is informed about where to find them.
Header files
Header files contain declarations about the functions and objects
contained in the module, but they are not compiled alone and they do
not produce object code.
Modules will generally need to refer to data and functions provided by
other modules. In order to do this, they need a blueprint of those
entities which describes their properties.
The blueprint takes the form of a header file, also called a .h or a
.hpp file. In this course, we will use the .hpp extension to denote
C++ header files, reserving the older .h extension for C header
files.
Header files for system modules are often found in the /usr/include directory, but they can be placed anywhere as long as the compiler is informed about where to find them.
Compiling and linking in linux
The command for compiling and linking in linux is g++, the GNU
implementation of C++. g++ is a very powerful tool and requires many
man pages to describe.
When used with the -c switch, it build an object file from a source code
file.
Otherwise, it builds an executable from one or more object modules. It
invokes the GNU linker ld to accomplish this task.
If called with one or more source code files and object files but no -c switch, it first compiles all of the source code files and then links the resulting object files with any object files from the command line and the libraries.
One-line compilation
Often all that is required to compile your code is the single command
g++ -o mycommand <switches> *.cpp
We will generally be using the following switches:
The job of the project manager
As we’ve seen, a project consists of many different files. Keeping track of
them and remembering which files and switches to put on the command
line is a major chore.
To aid in this task, one uses one of a number of project development tools such as make or Integrated Development Environments.
Command line development tools
The default g++ compiler installed on the Zoo is version 4.8.6-4. The newer
version 5.3.0 and associated files is installed in /usr/local/gcc-5.3.0.
The principal difference is that the newer version provides fuller support
for the C++11 and C++14 language standards.
Both versions will work perfectly well for many of the assignments, but for some of the later assignments, you will need to configure your environment to use the newer compiler.