CPSC 427: Object-Oriented Programming
Michael J. Fischer
Tasks for this week
C++ Overview
Why did C need a ++?
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
What is a project?
A project is a collection of source files (or implementation files or
.cpp files) that can be processed by the system to produce a runnable
piece of code called an application (or program or executable or
command).
An application is assembled from several compilation modules (or
object files or .o files).
Some modules are produced as part of the project. Others come from
libraries (.a or .so files) that contain compiled code written by others
and provided by the system for your use.
Whatever the origin of the modules, they must be joined together during final assembly to produce the runnable application. This step of the process is called linking.
C++ compilation model
The C++ compiler takes as input an implementation (.cpp) file and
some number of header (.hpp) files. It compiles the .cpp file to
produce the corresponding .o object file.
A project generally has several .cpp files. In the traditional separate
compilation model, each is compiled separately to produce a
corresponding .o file. Then the .o files and necessary libaries are linked
together to produce the executable.
The C++ programmer must clearly distinguish between compilation and linking, especially when interpreting error comments from the build process.
Header files
Modules generally refer to classes, data and functions provided by other
modules. To compile such a module, the compiler needs some knowledge of
those other entities. While one might assume the compiler could figure
out on its own what is in its own libraries, that is not the case. The
mechanism for supplying that information is the header file (or .hpp or .h
file).1
Header files for system modules are often found in the /usr/include directory, but they can be put anywhere as long as the compiler is told where to look for them.
What’s in a header file?
Header (.hpp) files contain declarations that are needed in order to
compile both the corresponding .cpp file and also any other .cpp files
that refer to this module.
Because the same declarations are needed by several different .cpp files,
they are placed in a separate header file and included during
compilation as needed.
This avoids unnecessary duplication of the declarations and makes the code more maintainable.
What’s in an implementation file?
Implementation (.cpp) files contain definitions of functions and
constants that comprise the actual runnable code.
Each compiled definition must appear in exactly one object file. If it
appears in more than one, the linker will generate a multiply-defined
error.
For this reason, definitions are never put in header files.2
Compiling in linux
The command for compiling 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, g++ compiles a .cpp file to produce a single .o file.
Linking
When used without the -c switch, g++ calls the linker ld to build an executable.
In both cases, the linker completes the linking task by searching libraries for any missing (unresolved) functions and variables and linking them into the final output.
System libraries
System libraries are often found in directories /lib, /lib64, /usr/lib,
or /usr/lib64, but they can be placed anywhere as long as the linker is
told where to find them.
The linker knows where to find the standard system libraries, and it searches the basic libraries automatically. Many other libraries are not searched unless specifically requested by the -L and -l linker flags.
One-line compilation
Often all that is required to compile your code is the single command
g++ -o myapp -O1 -g -Wall -std=c++14 *.cpp
The switches have the following meanings:
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 can be a major chore.
Project maintenance tools such as make and Integrated Development Environments (IDEs) are used to aid in this task.
Command line development tools
At the very least, you should become familiar with the basic tools for maintaining and building projects:
clang++ is a newer alternative to g++. There are indications that it produces slightly better error messages and slightly better code than g++, but both compilers are very good and are suitable for use in this course. (The MacIntosh Xcode development system now defaults to clang++.)
Parts of a simple project
Object files are built from implementation files and header files.
The executable is built from object files.
Dependencies
Whenever a source file is changed, the object files and executables that
are directly or indirectly produced from it become out of date and must
be rebuilt. Those files are called dependencies of the source
file.
make uses dependency information stored in Makefile to avoid
rebuilding files that have not changed since the last build. It only
recompiles and/or relinks those files that are older than a file that they
depend on.
make uses file modification dates for this purpose, so if those dates are off, make might fail to rebuild a file that is actually out of date.
A sample Makefile
Parts of a Makefile
A Makefile has three parts:
Syntax peculiarities:
Macros
Macros are named strings.
Rules
Rules tell how to build product files.
Rules
Notes:
Dependencies
Dependencies are a kind of degenerate rule.
But those dependencies are source files, so there is nothing to build. And where is the rule to build main.o?
What make does is compare the file modification dates on the target and on the dependencies in order to know if the target needs to be rebuilt.
Implicit rules
To build a target such as main.o for which there is no explicit rule, make uses an implicit rule that knows how to build any .o file from the corresponding .cpp file. In this case, the implicit rule invokes the $(CXX) compiler to produce output main.o. The compiler is called with the switches listed in $(CXXFLAGS).
Integrated Development Environments
Graphical development tools: IDEs Integrated Development Environments provide graphical tools to aid the programmer in many common tasks:
Recommended IDE’s
Eclipse/CDT is a powerful, well-supported IDE that runs on many different platforms. Xcode is an Apple-proprietary IDE that only runs on Macs. Mac users may prefer it for its greater stability and even more features. I recommend either of these for serious C++ code development.
Geany is a lightweight IDE. It starts up much faster and is much more transparent in what it does. It should be more than adequate for this course.
Both Eclipse and Geany are installed on the Zoo, ready for your use.
The early part of this course can be perfectly well done in Emacs, so you don’t have to learn Eclipse or Geany in order to get started.
Integrated Development Environment (e.g., Eclipse)
Advantages
Integrated Development Environment (e.g., Eclipse)
Disadvantages
Integrated Development Environment
If you use an IDE, before submitting your assignment, you should:
Submission Instructions
Submitting your assignments Regardless of how you prepared your code, you should follow these instructions when you submit your assignment.