Davis Stephen R.

Beginning Programming with C++ For Dummies


Скачать книгу

Was Successful.

      Figure 2-6: You must install both Xcode and the Command Line Tools for Xcode to get the gcc compiler for Macintosh.

       Installing Code::Blocks

      Now, at last, you can finish your Mac OS installation by downloading the Code::Blocks package:

      1. Open the Safari browser and go to www.codeblocks.org/downloads.

      2. Click Downloads⇒Binaries.

      3. Click Mac OS X.

      4. Select either the BerliOS or the Sourceforge.net mirror site for the most recent version.

      At the time of this writing, CodeBlocks-13.12-mac.zip was the most recent.

      5. Install the downloaded Zip file into the Applications folder.

      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:

      a. Click System Preferences.

      b. Click Security and Privacy.

      c. Click the padlock in the lower-left corner of the window to allow changes.

      d. Click Allow Applications Downloaded from: Anywhere, as shown in Figure 2-7.

      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.

      Figure 2-7: You’ll need to allow third-party applications to be installed before you can install Code:: Blocks on your Macintosh.

      6. Double-click the Code::Blocks icon.

      The first time you do this, the Mac OS asks, “Are you sure you want to open it?”

      7. Select Dont Warn Me When Opening Applications on This Disk Image and click Open.

      Code::Blocks should start and find the gcc compiler installed with the Command Line Tools.

      8. Select the gcc compiler, as shown in Figure 2-8. Click Set as Default and then click OK to continue starting Code::Blocks.

      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.”

      Figure 2-8: Code:: Blocks automatically finds the gcc compiler the first time you execute it.

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.

      1. Start Code::Blocks if it isnt started already.

      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.)

      2. Choose Settings⇒Compiler from the menu options.

      A window similar to Figure 2-10 appears.

      3. Select Global Compiler Settings from the options on the left side of the window.

      Figure 2-9: The opening screen of the Code:: Blocks environment.

      Figure 2-10: The compiler settings window allows the user to adjust the details of the way the C++ compiler works.

      4. Select the Compiler Flags tab.

      This displays the compiler options.

      5. Select the Enable All Compiler Warnings check box.

      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).

      6. Select the Have g++ Follow the C++11 ISO C++ Language Standard check box.

      

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.

      7. Select OK.

      The Settings window closes.

Testing the Code::Blocks Installation

      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.

Creating the project

      1. Select File⇒New⇒Projects to open the window shown in Figure 2-11.

      This is a list of all of the types of applications that Code::Blocks knows how to build.

      Fortunately,