Choosing a C++ Editor

A lot of people have asked why I chose Code::Blocks as the editor for C++ All-In-One for Dummies, 3rd Edition. There are a number of reason that I chose this particular editor including:

  • Ease of use
  • Free download
  • Runs on all the platforms the book supports

However, the fact that Code::Blocks works well for the book and for most beginning projects doesn’t mean it’s the best solution for your particular needs. There are many different C++ Integrated Development Environments (IDEs) out there and I’m constantly trying new products. When the new version of Visual Studio comes out, you can be sure I’ll obtain a copy and check it out because some of my readers use Visual Studio. The problem with Visual Studio is that it tends to use Microsoft additions to the C++ language and it also doesn’t run on all the platforms that the book must support. However, if you’re working with Microsoft systems and need to create a relatively large project, Visual Studio might be a good choice for you—only you can make that determination.

The Eclipse IDE is another good selection. In fact, I used Eclipse when writing Java eLearning Kit for Dummies because it runs on every platform I needed to test. When I wrote this book, I chose to focus on the language though and not really mention the IDE at all. I didn’t want readers to have preconceived ideas of how Java should look inside an editor. I had thought about using Eclipse for C++ All-in-One for Dummies, 3rd Edition, but after reviewing the IDE and comparing it to Code::Blocks, I felt Code::Blocks was a lot simpler. Even so, if you need great multiplatform support for your C++ projects, Eclipse is a great choice.

I recently tried another IDE, Intel Composer. This is most definitely not a IDE for the faint of heart or the light of pocketbook (the asking price is $1,199.00). Of course, many of you are going to question my sanity for even downloading such a product when there are so many less expensive solutions out there. The main reason to obtain a product like this one is that it provides phenomenal parallelism support for multiprocessor applications. In other words, you use this sort of IDE for high end projects. You can read all the other features this product offers on the vendor site. One of the other items that grabbed my attention is that it provides both multiplatform (Windows, Mac, Linux, and Android) and multiformat (Phone, Tablet, PC, Ultrabook, and Server) support. Whether this particular IDE makes sense for your needs depends on the kind of applications you create.

Although the information in Wikipedia is often suspect, you can find a comparison of various IDEs at http://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments. The best way to choose a C++ IDE is to look for a product that meets your needs and then try it out on a subset of the problem you’re trying to solve. Researching the IDE you use is essential because a mistake can cost you a lot of time later. Not every IDE support every C++ feature, every platform, or every team need. Tell me why you think I should move to something other than Code::Blocks for the next edition of the book at [email protected].

 

Entering Data in a Code::Blocks Window

Sometimes it isn’t very obvious how to enter data into a Code::Blocks window. One of the windows that seems to be causing problems for a number of readers is the Watches window. You open this window by choosing Debug | Debugging Windows | Watches. The purpose of this window is to let you view the content of variables in your application, which is an essential part of the debugging process. In order to view the variable (or other expression) content, you must enter it in the Watches window. Book III of C++ All-In-One Desk Reference For Dummies tells you all about debugging.

One technique for entering the variable is to select it in the editing window and the drag it to the Watches window. The variable will appear in the Watches window along with its value. However, this approach only works for variables and expressions that actually appear in your code. You might want to enter some other expression (or manually enter the variable, rather than drag and drop it). The Watches window consists of rows and columns as shown here.

WatchEntry01

The name of the variable or the expression you want to view appears in the first column. To enter a new value into the Watches window, click directly in the first empty left column cell. The row will turn blue and you’ll see a red insertion point appear in the cell as shown in the screenshot. Now you can type the variable name or expression you want to work with and press Enter. Let’s say your variable is named i. It might look like this:

WatchEntry02

Notice that the row is now white because it isn’t selected. However, you can see the name of the variable, i, it’s value 1983844706, and it’s type int. The row is in red because the value of i has just changed (unchanged values appear in black so you can see them easier). As you debug your application, you can now watch the value of i for changes.

Sometimes it isn’t obvious how to enter information into Code::Blocks (or any other application for that matter). When that happens, the focus turns to the application, rather than the work you need to do, and the experience becomes frustrating. Let me know about your book-related Code::Blocks questions at [email protected] and I’ll do my best to answer them. Because I don’t have a direct connection to the vendor, my ability to answer other sorts of questions is limited.

 

Pausing the C++ Example Output

A number of readers have written to ask about running the example code in C++ All-In-One Desk Reference For Dummies. The book shows that the examples pause so you can see the output, yet a few people experience problems getting the example to pause as shown in the book. Let’s take the first book example. When you run the example, you should see a command window similar to the one shown here open.

ExamplePause01

This is how the command window looks in Windows, but if you use some other operating system, you should see something similar. Notice that the output is paused. Pressing any key will cause the window to disappear and the example to end. The purpose of pausing the output is so that you can see the result.

There are two common reasons that people aren’t seeing the output pause. The most common reason is that the example is run in debug mode. Make sure you click Run or choose Build | Run to execute the example. If you click Debug/Continue or choose Debug | Start/Continue instead, the example will execute without pausing and you won’t see the output. Of course, you can always set a breakpoint to get the example to pause, but most people simply want to see the example output which means running the example, rather than debugging the example.

A less common cause is that the project environment is configured incorrectly. Normally the Code::Blocks environment automatically pauses when you run the example. However, it’s possible to set Code::Blocks not to pause. Some readers inadvertently change this setting while exploring the environment. In order to check this setting, choose Project | Properties to display the Project Targets/Options dialog box. Select the Build Targets tab and you see the dialog box shown here.

ExamplePause02

Notice the Pause When Execution Ends option. If this option is cleared, the example won’t pause when you run it. Make sure this option is checked and click OK. The projects that come with the downloadable code all have this setting set up correctly.

Of course, there could always be other reasons why the examples aren’t pausing. Please let me know if you have any problems setting the example output. It’s essential that you be able to see the example output as you follow along in the book to understand how C++ works. Send your queries about this (or any other book-specific) topic to [email protected]. I always want to ensure you have the best possible experience when using my books.

 

Resetting Your CodeBlocks Configuration

Quite a few people have written to me about issues they have with C++ All-In-One Desk Reference For Dummies that involve getting CodeBlocks up and running. The posts in the C++ All-in-One for Dummies archive normally provide everything needed to get the compiler up and running. I even provide posts on using the 10.05 version of the product, should you wish to upgrade. However, there are rare times when no matter how much you try, you simply can’t get the compiler to work.

One technique I haven’t really covered until now is to reset the CodeBlocks configuration. The problem with this approach is that it resets all of your settings, not just those that could be in error. This is the reason that I’ve taken a more measured approach to helping readers through problems until now. My concern is that resetting everything will actually cause more problems and end up confusing some readers, so you really do want to try those other posts first. That said, there are situations where resetting CodeBlocks is the only course of action that will work.

To reset your settings, open your copy of CodeBlocks. Choose Settings | Compiler and Debugger. You see the Compiler and Debugger Settings dialog box shown here.

ResettingYourCodeBlocksConfiguration01

Click Reset Defaults. This action will reset all of the defaults so that they match the initial installation configuration unless you have created a default of your own. Make absolutely certain that the Selected Compiler field shows GNU GCC Compiler as shown in the figure and then click OK. Close and then reopen CodeBlocks before you test your configuration.

Let me know if you have any questions about this procedure at [email protected]. It’s always my goal to make my books as useful to you as possible.

An Update on Microsoft’s New Casablanca Release

A little over a month ago I wrote a post entitled, “Microsoft’s New Casablanca Release” about Microsoft’s newest Casablanca product. Niklas Gustafsson, a member of the Microsoft Visual C++ Team was kind enough to contact me and answer a few questions about this release. I decided that you also need to know the answers to these questions so that you can make an intelligent decision about Casablanca. As a quick recap, Casablanca is a new product that lets C++ developers interact with the cloud using REST.

The first thing Niklas pointed out is that Casablanca isn’t precisely a product—it’s what is termed as an incubation effort, something to see what is possible and will work. Casablanca is early in its life cycle and doesn’t provide either the quality or maturity that a released product would provide. to me, this means that you need to be careful using Casablanca. For the time being, it’s probably an interesting technology to play with, but you probably shouldn’t employ it in your production application because it will change quite a lot.

Even though I use C++ for utilities and low level program (as described in C++ All-In-One Desk Reference For Dummies), Niklas pointed out that many organizations use C++ for larger, line of business applications. In many cases, the reason for using a language like C++ for this purpose is that the organization has already made an investment in C++, so the language is familiar and the organization already has the required resources. I still can’t imagine creating a large scale user application using C++, but I’m also not the one trying to forge ahead in a large organization. It seems to me that using other languages would be simpler and less error prone, but I’m well-versed in using a number of languages, so I have the option of using the best tool for a specific task. In fact, Niklas summarized C++ usage for larger applications in the following points:

 

  • Raw performance
  • Portability
  • It’s what they know

To make his point clearer, Niklas provided me with a link to a whitepaper entitled “C++ and Cloud Computing” that makes a number of points clear. I encourage you to download this whitepaper and give it a read before you make any decisions regarding C++ and the cloud. It certainly helped me envision how someone might use Casablanca a bit better. For example, even a low-level application could need access to an online storage provider in order to access the information it needs. I also hadn’t considered some special areas of program, such as gaming, when I wrote my original post—I was thinking more along the lines of what a business developer would need.

With regard to my question about using REST, rather than SOAP, Niklas pointed out that REST currently enjoys far wider support than SOAP and that it’s simpler to implement. If Casablanca becomes a success, SOAP support could follow. So, at least the team is thinking about SOAP as a future addition.

It’s also important to remember that many organizations are only starting to think about cloud computing, so technologies such as Casablanca are still well ahead of the curve. Sometimes in reading the technical articles online, you get the idea that cloud computing is already well entrenched in the enterprise. The truth is that many enterprises are only now experimenting with the cloud and some will never use the cloud due to regulatory or other concerns.

I was really happy that Niklas took time out to contact me regarding Casablanca. I’ll be taking another look at this technology as the Visual C++ Team works on it and will likely provide you with an update sometime in the future. In the meantime, let me know how your organization is working in the cloud today at [email protected].

 

Microsoft’s New Casablanca Release

When I wrote C++ All-In-One Desk Reference For Dummies, I provided the reader with a view of C++ as a low-level language. It’s true that most developers use C++ to create command line utilities, drivers, embedded systems, libraries, and even operating systems. While I might use C++ to create a database engine, I probably wouldn’t use it to create a database application. I’d probably lean toward some combination of a procedural language such as C# or Visual Basic and a declarative language such as SQL or LINQ for the purpose. I’ve written database applications using PHP, Java, and a host of other languages, but never in C++ because C++ isn’t the optimal tool for the job. If you have a lot of apps to develop, you are probably better off using an online database software like Kohezion. Build your own online database application today without using outside vendors to cut your online database app development time.

Many developers have written about the strength of C++ being the flexibility it provides to perform amazing tasks. So, I was a bit surprised to learn that Microsoft has delivered a new product codenamed Casablanca that lets C++ developers interact with the cloud using REST.

In reading the blog post announcing Casablanca, I detect a lack of direction. I understand that C++ currently lacks library support for any sort of Web service access without buying a separate third party product. However, that’s all that the blog post tells me. It doesn’t provide me with any ideas of how Microsoft sees the developer using this library. Given that some people do write C++ applications, I imagine that Microsoft envisions developers creating full-fledged applications with their product, but the intent is a mystery (and will remain so until someone at Microsoft speaks up). The last paragraph of the blog post says it all, ‘We would love to know whether you’re interested in using C++ to consume and implement cloud services, and if so, what kind of support you want in order to do so, whether “Casablanca” is on the right track, and how you’d like to see it evolve.’ Apparently, Microsoft is hoping that the development community will come up with some ideas on using this product.

Casablanca also comes with some significant restrictions. The most important of these restrictions is the platforms that support it:

 

  • Windows Vista
  • Windows 7
  • Windows 8


This means you can’t use Casablanca to create a library for all of those Windows XP users on your network. It doesn’t surprise me that Microsoft would place these platform limits on the product, but I’m wondering just how many developers will be able to use Casablanca in today’s enterprise environment for a product application. The fact that Microsoft’s Casablanca site heavily promotes its use with Azure leaves no doubt that this product is designed for the enterprise (or at least, a larger business).

Another strange limitation is that the product only supports REST. At one time, Microsoft was promoting SOAP and many Web services still rely on this protocol. In fact, it’s actually easier to create a connection to a SOAP Web service in Visual Studio than it is to create a REST connection. I’m sure that Microsoft will address this limitation at some point, but for now, this remains a problem for developers.

Casablanca does come with the usual Microsoft bells and whistles. If you buy the latest version of Visual Studio, you’ll obtain a complete set of templates that will make coding access to a REST Web service easier. I’m sure that there are developers who are working with just the supported platforms, work with Azure, and have the most recent version of Visual Studio who will absolutely love this product, but I have to wonder how many developers outside this small core group will be able to use Casablanca to do something productive.

Normally, I try to find something positive to say about new product releases, but this one has me scratching my head. I’ve downloaded Casablanca and plan to play with it some more. If there are some truly dazzling features, I’ll post an updated blog entry later. In the meantime, I’d like to hear your input. Is Casablanca an amazing new product that C++ developers must have? If so, how do you plan to use it? Let me know at [email protected].

 

Getting CodeBlocks to Work – An Update

Quite a while ago, at least in computer technology terms, I wrote the Getting CodeBlocks to Work post to make it easier for readers to obtain a good copy of CodeBlocks to use with C++ All-In-One Desk Reference For Dummies. It seems that some of the information I provided at that time has become outdated.

While the http://www.codeblocks.org/downloads/5 link still works fine, some of the other links may not. A reader recently pointed out that the automated installer location has changed. When you go to the original automated installer site, you now see a number of links, including one listed as Download mingw-get-inst-20111118.exe (591.9 kb ).

Clicking the new link will automatically start a download of the latest automated installer. When the download is complete, you can install your copy of CodeBlocks. Another link to the same download is http://sourceforge.net/projects/mingw/. The point is that there is a download that will work with your system, no matter what system you’re using.

If anyone encounters a problem with these latest downloads, please let me know at [email protected]. I want to be certain that you have a great learning experience when working with my books.

 

Passing By Value Versus By Reference

A number of readers have written me about the examples that use pointers in the book. One of the most common questions comes from the example on page 149 of C++ All-In-One Desk Reference For Dummies. In this example, the application contains a function that accepts an argument by reference. The use of a reference seems to have many people confused and this post will hopefully clear the issue up.

The first thing you need to understand is that there isn’t any magic going on here. The process for working with functions remains constant in C++. When you call any function with an argument, the application creates memory to hold the argument. The memory is local to that function. When the function ends, the memory is released. What the memory holds, now that changes.

Think of the memory as a box. This box has an address that points to it. When you work with a pointer, you’re working with the address. On the other hand, when you work with the value, you’re working with the content pointed to by the pointer, or the contents of the box. Whether the box the function receives contains a pointer or the value depends on how you write the function and what you pass to it from the caller.

Now, let’s look at an example so that you can better understand what’s going on.  This example will do the same thing to the number each time, but using different techniques: by value, using a pointer, and by reference.

#include <iostream>
 
using namespace std;
 
void MessMeUpA(int myparam)
{
    myparam = myparam * 2 + 10;
}
 
void MessMeUpB(int* myparam)
{
    // Obtain the current value.
    int currentValue = *myparam;
 
    // Perform the required math.
    currentValue = currentValue * 2 + 10;
 
    // Save the result.
    *myparam = currentValue;
}
 
void MessMeUpC(int &myparam)
{
    myparam = myparam * 2 + 10;
}
 
int main()
{
    // Call by value.
    int mynumber = 30;
    MessMeUpA(mynumber);
    cout << "By Value: " << mynumber << endl;
 
    // Call using a pointer.
    mynumber = 30;
    MessMeUpB(&mynumber);
    cout << "By Pointer: " << mynumber << endl;
 
    // Call using a reference.
    mynumber = 30;
    MessMeUpC(mynumber);
    cout << "By Reference: " << mynumber << endl;
 
    return 0;
}

You may notice that part of this example comes directly from page 149. If you run this example, you’ll see this output:

PassingData01

When you pass the data by value, you’re passing the information, not a pointer to the information. As a result, MessMeUpA() receives a value of 30. It doesn’t receive a pointer to the initial variable, nor does it obtain a reference to the initial variable. As a result, when the application performs the calculation, the result is thrown away.

When the application calls MessMeUpB(), it provides a pointer to the variable. However, the pointer isn’t a value. As a result, you move the value pointed to by the pointer into a local variable, perform the required math, and then move the value back into the original pointer. As a consequence, the original value is altered to 70.

Finally, when the application calls MessMeUpC(), the function obtains a reference to the original memory location. When the function performs the math, it’s actually using the original value as pointed to by the reference, which means that this is a kind of pointer, just not passed from the caller. The changes made in MessMeUpC() are reflected in the original value because you’re using a pointer to that value and not a copy of the value in local memory.

I highly recommend that anyone trying to understand how code works use the debugger to trace through that code. It’s instructive to look at each of the functions to see how they look. Place a breakpoint in each of the functions and then start the debugger. Open the Watches window, if necessary, by choosing Debug | Debugging Windows | Watches. Here is the view of the Watches window when calling MessMeUpA().

PassingData02

What you see here is a function argument—an integer that contains the value of 30. Now, let’s look at MessMeUpB().

PassingData03

In this case, you see a local variable, currentValue, that contains the updated value and a pointer to the variable used by the caller in myparam. The pointer gives you access to the value held by the caller and allows you to change it. Finally, let’s look at MessMeUpC().

PassingData04

Look at the function argument, myparam. The debugger is giving you a valuable insight here. See the address provided with the variable? Match that address to the pointer provided to MessMeUpB() and you’ll see that they’re the same. The reference is simply another sort of pointer, but one that gives you direct access to the memory without having to go through the extra steps required by pointers. The debugger even shows you that the current value of that memory is 30 (the math hasn’t been performed as of yet).

A function argument is simply a box that holds either a value or a pointer. Whether that pointer is an actual pointer or a reference depends on how you write you code. That’s the point of the example. Pointers don’t have to be mysterious or hardthey’re simply an address for an area of memory (a box if you will) that holds a specific value. Please let me know if you have any questions about this issue at [email protected].

 

Debugging a CodeBlocks Application with Command Line Arguments

Most application environments provide a means of setting command line arguments and CodeBlocks is no exception. The example shown in Listing 4-12 on page 107 of C++ All-In-One Desk Reference For Dummies requires that you set command line arguments in order to see anything but the barest output from the debugger. This post discusses the requirements for setting command line arguments for debugging purposes.

Let’s begin with the example without any configuration. Every application has one command line argument—the path and application executable name. So, when you run the example shown in Listing 4-12 you’ll see the path and executable name as a minimum, as shown here.

CommandLineArguments01

If you run this example, you may see a different path, but the command line executable should be the same. The point is that you see at least one argument as output. However, most people will want to test their applications using more than one argument. In order to do this, you must pass command line arguments to the application. The following steps tell how to perform this task.

 

  1. Choose Project | Set Program’s Arguments. You’ll see the Select Target dialog box shown here.
    CommandLineArguments02
  2. Select Debug as the target, as shown in the figure.
  3. Type the arguments you want to use, such as Hello Goodbye, in the Program Arguments field and click OK. The IDE is now set to provide command line arguments to the application when you’re using the specified target, which is Debug in this case.

To see how this works, set a breakpoint at the line that reads (you’ll find a discussion of how to set breakpoints on page 375 of the book):

for (int index=0; index < argc; index++)

You also want to create a watch that shows the command line arguments in action. In this case, you want to see the particular argument that the application is processing. Use these steps to set the watch.

 

  1. Choose Debug | Edit Watches. You’ll see the Edit Debugger Watches dialog box shown here.
    CommandLineArguments03
  2. Click Add. You’ll see the Add Watch dialog box shown here.
    CommandLineArguments04
  3. Type argv[index] and click OK. You’ll see the new watch added to the Edit Debugger Watches dialog box.
  4. Click OK. The new watch is ready for use.

You’re ready to test out the new setup. Choose Debug | Start or press F8. The debugger will start. If you don’t see the Watches window, choose Debug | Debugging Windows | Watches. Click Next Line and you’ll see the first command line argument as shown here.

CommandLineArguments05

Notice that the value of index is in red because it has changed to 0. In this case, argc contains 3 arguments (including the first one that you’re seeing now) and that argument is in argv, which is pointed to at the memory address shown. The argument at that first index value is the path and command line executable name, just as expected.

Click Next Line twice. Now you’ll see the second argument, as shown here.

CommandLineArguments06

If you followed the instructions earlier, you should be seeing Hello as the second argument as well. Click Run and you’ll see the output shown here.

CommandLineArguments07

Testing for command line arguments in a CodeBlocks application consists of telling the IDE what to pass in the Select Target dialog box and then setting the correct watches so that you can see how the command line arguments work. Let me know if you have any questions about this process at [email protected]JohnMuellerBooks.com.