Chapter 2
In This Chapter
Reviewing the compilation process
Installing the Code::Blocks development environment
Testing your installation with a default program
Reviewing the common installation errors
In this chapter, you review what it takes to use C++ source code to create executable programs that you can run on Windows, Linux, or Macintosh computers. Then you install the Code::Blocks integrated development environment used in the remainder of the book — and build a default test program to check out your installation. If all is working, then by the time you reach the end of this chapter, you’ll be ready to start writing and building C++ programs of your own — with a little help, of course!
You need two programs to create your own C++ programs. First, you need a text editor that you can use to enter your C++ instructions. Any editor capable of generating straight ASCII text letters will work; I’ve written programs using the Notepad editor that comes with Windows. However, an editor that knows something about the syntax of C++ is preferable; it can save you a lot of typing, and sometimes highlight any mistakes you might make as you type, in much the same way that a spell checker highlights misspelled words in a word processor.
The second program you need is a compiler that converts your C++ source statements into machine language that the computer can understand and interpret. This process of converting from source-code C++ statements to machine code is called building. Graphically, the process looks something like Figure 2-1.
The process of building a program actually has two steps: The C++ compiler first converts your C++ source code statements into a machine executable format in a step known as compiling. It then combines the machine instructions from your program with instructions from a set of libraries that come standard with C++ in a second step known as linking to create a complete executable program.
Most C++ compilers these days come in a software package known as an Integrated Development Environment or IDE. IDEs include the editor, the compiler, and several other useful development programs together in a common bundle. Not only does this save you from having to purchase the programs separately, but also offers productivity benefits by combining them into a single package:
As nice as that sounds, the automatically generated code only displays the windows. A programmer still has to generate the real code that gets executed whenever the operator selects buttons within those windows.
Invariably, these visual IDEs are tightly coupled into one particular operating system. For example, the popular Visual Studio is strongly tied into the .NET environment in Windows. It’s not possible to use Visual Studio without learning the .NET environment — and something about Windows — along with C++ (or one of the other .NET languages). In addition, the resulting programs only run in a .NET environment.
In this book, you use a public-domain C++ IDE known as Code::Blocks. Versions of Code::Blocks exist for Windows, Linux, and Mac OS. Versions of Code::Blocks for these three operating systems (as well as a few others) are available for free download at www.codeblocks.org. The specific version used in the generation of the book is version 13.12. You can also download the Windows 13.12 binary at www.dummies.com/extras/beginningprogrammingcplusplus.
You’ll use Code::Blocks to generate the programs in this book. These programs are known as console applications because they take input from — and display text back to — a console window. Okay, this isn’t as sexy as developing programs in onscreen windows, but staying with console applications lets you focus on C++ without being distracted by the requirements of a windowed environment. In addition, using console applications will allow the programs in the book to run the same on all environments that support Code::Blocks.
The following instructions take you through installing Code::Blocks on a Windows, Mac OS, or Linux computer. Jump to the section that applies to your operating system.
This section provides detailed installation instructions for installing Code:: Blocks for Windows Version 13.12 available at www.dummies.com/extras/beginningprogrammingcplusplus.
That’s straightforward enough.
Depending on which version of Windows you’re using, you may a warning pop-up message like this one:
Do you want to allow the following program from an unknown publisher to make changes to your computer?
Setup unpacks the files it needs to start and run the Code::Blocks Setup Wizard. This process may take a minute. When it finishes, the startup window shown in Figure 2-2 appears.
The Setup Wizard displays the generic End User License Agreement (EULA). There’s nothing much here to get excited about.
The Setup Wizard then displays a list of the components that you may choose to install. See Figure 2-3. The defaults are okay.
It’s absolutely critical that the MinGW Compiler Suite option appear onscreen and have a check mark next to it. This is the option that installs the C++ compiler.
The next window asks you to choose the installation’s location. This window also tells you how much space Code::Blocks requires on your hard drive (about 250MB, depending on the options you’ve selected) and how much space is available. If you don’t have enough free disk space, you’ll have to delete some of those captured YouTube videos to make room before you continue.
At this point, the Code::Blocks Setup Wizard really goes to work. It extracts umpteen dozen files that it installs in a myriad of subdirectories in a flurry too complicated for mere mortals to follow. This process may take several minutes.
If all has gone well so far, the Installation Complete window shown in Figure 2-4 appears.
Finally, the Completing the Code::Blocks Setup Wizard window appears. This final step creates the icons necessary to start the application.
You’ve installed Code::Blocks but you still need to make sure that it’s set up properly for the programs in this book. Jump to the “Setting up Code::Blocks” section farther along in this chapter.
Code::Blocks does not include the gcc compiler in the Linux installation, so installation is a two-step process. First you’ll need to install gcc. Then you can install Code::Blocks.
The gcc compiler is readily available for Linux. Follow these steps to install it:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install g++
The standard Ubuntu Linux distribution includes a GNU C compiler, but it does not include the C++ extensions; in particular, it doesn’t have the C++ 2011 standard extensions. The first two commands update and upgrade the tools you already have. The third command installs C++.
gcc --version
My Ubuntu 13.04 downloaded GNU C++ version 4.7.3. You’ll be fine with version 4.7.1 or later. If you have an earlier version, some of the C++ 2011 features may not work properly, but otherwise it should be okay.
sudo yum install g++
Fortunately for all concerned, an Ubuntu-ready version of Code::Blocks is available in the Ubuntu Software Center. Many other versions of Linux include something similar to the Software Center. Follow these steps to install Code::Blocks:
The installation process starts.
Code::Blocks searches your hard drive, in quest of your C++ compiler. If Code::Blocks finds your C++ compiler,, you’re done.
If Code::Blocks doesn’t find your C++ compiler, then execute the following steps.
Jump to the “Setting Up Code::Blocks” section of this chapter to make sure that Code::Blocks is set up properly for the programs in this book.
The Macintosh version of Code::Blocks relies on the Xcode distribution from Apple for its compiler.
Xcode is a free development package offered by Apple; you’ll need it. Follow these steps to install it first:
This will open the Xcode download dialog box shown in Figure 2-5.
The icon changes to Installing.
The download and installation takes quite some time, as Xcode takes up a little over 2GB as of this writing.
As big as Xcode is, you might think that it has everything you need, but you’d be wrong. You need one more package from Apple to make your joy complete and to get a working gcc compiler on your Macintosh. Follow these steps to install the Command Line Tools for Xcode:
You may be asked to sign up for an Apple Developer ID. Go ahead and do so — it’s free. Provide your existing developer ID if you already have one.
The installation should finish with Installation Was Successful.
Now, at last, you can finish your Mac OS installation by downloading the Code::Blocks package:
At the time of this writing, CodeBlocks-13.12-mac.zip was the most recent.
If you’ve never installed an application from a third-party site, you may need to execute these extra steps before you can do so:
When you’ve completed the installation of Code::Blocks, you may choose to return to this window and restore the settings to Mac App Store.
The first time you do this, the Mac OS asks, “Are you sure you want to open it?”
Code::Blocks should start and find the gcc compiler installed with the Command Line Tools.
Code::Blocks opens with a banner page, followed by a menu across the top of the dialog box.
Continue to the next section, “Setting Up Code::Blocks.”
The GNU C++ compiler that comes with Code::Blocks supports a myriad of options; all of them affect the way it reads your programs. For this book, you’ll need to make sure that a few of these options are set. The instructions in this section are the same whether you’re using Windows, Linux, or Mac OS.
In Windows, you can use the icon that Code::Blocks installed on the desktop. A window similar to Figure 2-9 appears. (The details of this opening screen vary by operating system and by version. This is the opening screen from the Version 13.12.)
A window similar to Figure 2-10 appears.
This displays the compiler options.
This will cause the GNU C++ compiler to complain about anything that it sees that might be a problem. Turning on all warnings is a good idea when you’re first learning a language — or when you’ve been programming for over 30 years (like me).
The most recent upgrade to the C++ standard was adopted in 2011. This upgrade implements several new features, many of which are beyond the scope of this book. Some of these features actually make life easier, however, so you’ll want to use them. Selecting this check box gives you access to these useful extensions.
The Settings window closes.
In this section, you’ll build a default program that comes with Code::Blocks. This program does nothing more than display Hello, world! on the display, but building and running this program successfully will verify that you’ve installed Code::Blocks properly.
Start Code::Blocks by double-clicking the Code::Blocks icon created on the Desktop or selecting Programs⇒Code::Blocks⇒Code::Blocks. This should open a window like the one shown in Figure 2-9.
Across the top of the window are the usual menu options, starting with File, Edit, View, and so on. The window at the upper right, the one that says “Start here,” is where the source code will go when you get that far. The window at the lower right is where Code::Blocks displays messages to the user. Compiler error messages appear in this space. The window on the left labeled Management is where Code::Blocks keeps track of the files that make up the programs. It should be empty now since you have yet to create a program. The first thing to do is create a project.
Okay, what’s a project?
You want Code::Blocks to create only Console Applications, but it can create a lot of different types of programs. For Windows programmers, Code::Blocks can create Dynamic Link Libraries (also known simply as DLLs). It can create Windows applications. It can create both static and dynamically linked libraries for Linux and Mac OS.
In addition, Code::Blocks allows the programmer to set different options on the ways each of these targets is built. (Later chapters show you how to adjust a few of these settings.) And finally, Code::Blocks remembers how you have your windows configured for each project. When you return to the project, Code::Blocks restores the windows to their last configuration to save you time.
Code::Blocks stores the information it needs about the type of program that you’re building, the optional settings, and the window layout in two project files. The settings are stored in a file with the same name as the program but carrying the extension .cbp. The window configuration is stored in a file with the same name but with the extension .layout.
This is a list of all of the types of applications that Code::Blocks knows how to build.
Fortunately, you’ll be concentrating on just one, the Console Application.
Code::Blocks responds with the display shown in Figure 2-12. Here Code::Blocks is offering you the option to create either a C or a C++ program.
Code::Blocks opens a dialog box where you’ll enter the name and optional subfolder for your project. First, click the little ... button to create a folder to hold your projects, and navigate to the root of your working disk (on a Windows machine, it’ll be either C or D, most likely C). Select the Make New Folder button at the bottom left of the window. Name the new folder Beginning_Programming-CPP.
The folder that you create to hold your project must not contain any spaces in the name. In addition, none of the folders in the path should contain spaces. That automatically eliminates placing your projects on the Desktop since the path to the Desktop contains spaces. You should also avoid spaces in the name of the project. You can use underscores to separate words instead. The Code::Blocks compiler gets confused with spaces in the filenames and generates obscure and largely meaningless errors.
Now enter the name of the Project as HelloWorld. Notice that Code:: Blocks automatically creates a subfolder of the same name to contain the files that make up the project.
Code::Blocks creates a console application project — and even populates it with a working program — when you select Finish on the Project Wizard. To see that program, click the plus (+) sign next to Sources in the Management window on the left side of the display. The drop-down list reveals one file, main.cpp. Double-click main.cpp to display the following simple program in the source code entry window on the right:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
It’s okay to skip over what some of this stuff means for now, but the crux of the program starts after the open brace following main(). This is where execution begins. The line
cout << "Hello world!" << endl;
says to output the line "Hello, world!" to the cout, which by default is the command line. The next line
return 0;
causes control to return to the operating system, which effectively terminates the program.
(You can press Ctrl+F9 or click the Build icon if you prefer.) Immediately, you should see the Build Log tab appear in the lower-right screen followed by a series of lengthy commands, as shown in Figure 2-16. This is Code::Blocks telling the C++ compiler how to build the test program using the settings stored in the project file. The details aren’t important. What is important, however, is that the final two lines of the Build Log window should be
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
The terminated status of 0 means that the build process worked properly. The “0 errors, 0 warnings” means that the program compiled without errors or warnings. (The build time of 1 second is not important.)
If you don’t get a status of 0 with 0 errors and 0 warnings, then something is wrong with your installation or with the project. The most common sources of error are
Immediately a window should pop open with the message Hello, world! followed by the return code of zero and the message Press any key to continue (as shown in Figure 2-17).
The window disappears and control returns to the Code::Blocks text editor.
If you were able to see the Hello, world! message by executing the program, then congratulations! You’ve installed your development environment and built and executed your first C++ program successfully. If you did not, then delete the Beginning_Programming_CPP folder, uninstall Code::Blocks, and try again, carefully comparing your display to the figures shown in this chapter. If you’re still having problems, refer to www.stephendavis.com for pointers as to what might be wrong, as well as a link to my email where you can send me questions and comments. I cannot do your programming homework for you, but I can answer some questions to get you started.
3.145.179.225