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 John@JohnMuellerBooks.com.

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 99 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/. When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.