Introducing the Dev-C Compiler


Introducing the Dev-C++ Compiler

Compilers today are more than just programs that translate source code into object code. They are complete integrated development environments (IDEs). With compilers like Dev-C++, you can write, compile, link, and debug your programs all in one development environment. So before we get into how to write C++ programs, let's take a brief look at Dev-C++.

At this point, I assume that you've installed Dev-C++ on your computer. If you haven't, please do it now. I'll wait.

Okay, now that you've installed Dev-C++, start it up. When you do, the program's main window resembles Figure 2.1.

Figure 2.1. The Dev-C++ program's main window.


We'll see what each part of the Dev-C++ main window is used for in the next few sections.

Warning

I think it's worthwhile to take a moment to repeat a warning that I gave in chapter 1. All of the C++ source code you will encounter in this book is designed to work with Dev-C++. It may or may not compile with other compilers, such as Visual C++. Ideally, C++ source code should work with all compilers, but in reality, subtle differences exist between compilers produced by different companies. Therefore, I strongly recommend that you use Dev-C++ for all the programs in this book. After you become an experienced C++ programmer, you should have no problem compiling this book's programs with Visual C++ with only minimal changes.


Can I Use Other Compilers?

You do not have to use the Dev-C++ compiler if you already have another one. For example, many programmers today use Microsoft's Visual Studio.

Commercial compilers such as Visual Studio are great, but they also cost a lot of money. Dev-C++ gives you the tools you need to get started, but it doesn't have all of the fancy Web programming tools, database access tools, and so on that you get with Visual C++. Most of these tools are not needed for game development. By itself, Dev-C++ does pretty much everything you need for game development in one IDE.

I recommend you start with Dev-C++. If you find that you need a bigger, more powerful tool set, you can always spend the money for a commercial compiler later.


Creating a Project

The first step in using Dev-C++ to write a C++ program is to create a new project. Here's how it's done.

Creating a Project File

1.

Start by choosing File from the main menu. Now select New and then Project. The dialog box in Figure 2.2 appears.

Figure 2.2. The New Project dialog box.


There are several options in the dialog box shown in Figure 2.2. Let's keep things simple and ignore most of them for now.

2.

Click on the Console Application icon.

3.

In the Name box, type hello and then click the Ok button.

4.

In the dialog box that appears, choose a directory to save your project file into. The main window now resembles Figure 2.3

Figure 2.3. The Dev-C++ main window after creating a new project.


The tabbed box down the left side of the Dev-C++ window in Figure 2.3 contains information about the current project. If you click the + sign next to the folder icon by the word hello, Dev-C++ shows a list of all files in your project. Right now, the only file in the project is called main.cpp.

The larger pane on the right side of the window has a tab with the filename in it. This is the file editing pane. In this pane, you'll see your first C++ programDev-C++ generated it for you. This program doesn't actually do anything. However, it contains all of the fundamental parts of a C++ program. We'll look at each one of them shortly.

Writing Programs

Let's go through the program in Figure 2.3 line by line. It's presented here in Listing 2.1 for your convenience.

Listing 2.1. A very basic C++ program

 1   #include <cstdlib> 2   #include <iostream> 3 4   using namespace std; 5 6   int main(int argc, char *argv[]) 7   { 8       system("PAUSE"); 9       return EXIT_SUCCESS; 10  } 

The first two lines in the file main.cpp contain the following text:

 #include <cstdlib> #include <iostream> 


These statements are called include statements. They help you access code in the C++ Standard Libraries. Every C++ compiler comes with this standard set of libraries. The libraries contain object code that performs many common tasks that almost all C++ programs do. The main job of your linker is to link your program to the C++ Standard Libraries. You can also use it to link to other libraries. When you use other libraries, you will need to put special include statements in your program, similar to the ones shown in Figure 2.3.

On line 4 of the file main.cpp, you'll find this statement:

 using namespace std; 


At this point, I'm not going to explain what this does. However, I will say that you need to put it at the beginning of most of your .cpp files. We'll revisit this statement, and others like it, in later chapters.

Lines 610 of main.cpp contain a block of code called the main() function. The main() function is the heart of every C++ program. I'll explain it in detail soon. Right now, we'll take a look at how to add C++ statements to the main() function.

Warning

I added line numbers to Listing 2.1 to make it easy to discuss the code. These line numbers are not allowed in C++ programs. Do not type them into your program. It will not compile. They're just shown in the listing for convenience.


Modifying the Main() Function

1.

Delete the text on line 8 of main.cpp. Do this by using your mouse cursor to highlight both lines and then pressing the Delete key on your keyboard.

2.

Change the main() function so that it matches the code in Listing 2.2. The changes are shown in bold text. Of course, they won't be bold when you type them into Dev-C++; they're just shown in bold here to make it easier for you to see what to add.

Listing 2.2. The new version of main()

 1   #include <cstdlib> 2   #include <iostream> 3 4   using namespace std; 5 6   int main(int argc, char *argv[]) 7   { 8       string yourName; 9 10      cout << "Please type your name and press Enter: " 11      cin >> yourName; 12 13      cout << endl << "Hello, " << yourName << endl << endl; 14 15      cout << "Press c and then Enter to continue..."; 16 17      char justWait; 18      cin >> justWait; 19 20      return (EXIT_SUCCESS); 21   } 

3.

Save main.cpp by selecting File and then Save from the main menu. The dialog box that appears enables you to enter a filename. In this case, you can just accept the name main.cpp by clicking the Save button.

Note

I've compiled all of the code listings in this chapter into executable programs. You'll find them on the CD in the folder\Source\Chapter02\Bin.


Note

When the length of a line of code exceeds the character limit for this book, you'll see a little continuation arrow on the next line. Treat this as a single line of code: don't use a hard return.


Compiling and Linking Programs

After you make the changes in Listing 2.2, press the F9 key on your keyboard to compile, link, and run the program. If you typed it correctly, you'll see a window that looks like Figure 2.4

Figure 2.4. The output from your first program.


Congratulations! You've compiled, linked, and run your first C++ program. This is a console program. If you've been using computers a while, you know that it is essentially no different than old DOS programs. Figure 2.4 shows that this program is waiting for you to type your name and press Enter. After you do, it greets you with a friendly hello. It then asks you to press the C key followed by Enter. When you do, the program ends.

Tip

If you prefer, you can just compile and link your program without running it. Do this by pressing Ctrl+F9. You can accomplish the same thing by selecting Execute from the main menu and then choosing Compile.




Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net