Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
C++ Application Development with Code::Blocks
C++ Application Development with Code::Blocks

C++ Application Development with Code::Blocks: Using Code::Blocks it's possible for C++ developers to create application consistency across multiple platforms. This book takes you through the process from installation to implementing advanced features, all with a user-friendly approach.

eBook
$9.99 $25.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

C++ Application Development with Code::Blocks

Chapter 2. App Development with Code::Blocks

In this chapter, we'll learn C++ app development with Code::Blocks. We'll begin with a simple Hello World app. Subsequently concept of project and workspace will be introduced.

Creating your first app with Code::Blocks


Let's write a simple Hello World app, which essentially prints out "Hello World" to console. Launch Code::Blocks to begin and as shown in the following screenshot click on the new button in main toolbar and then click on the File menu option. The following screenshot represents the same:

Click on the C/C++ source option in the next window and then on the Go button. A wizard will be presented. Click on the Next button on the first page of the wizard. Choose the C++ option and click on the Next button. Choose file path and name in the next window and click on the Finish button to complete wizard.

Then type the following code in the editor:

#include <iostream>

int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Code::Blocks will automatically add an empty line at the end of the file if there isn't any, this is a Code::Blocks feature. GCC expects an empty line at the end of source code, and it will throw warning if an...

Project in Code::Blocks


The project is an important concept in Code::Blocks. A project can be described as a collection of source files and build targets.

A build target can be defined as a label or a tag for each source file, which contains separate set of build (compiler, linker and resource compiler) options. Each build target contains a set of build options and during compilation of a project Code::Blocks selects currently active target. All files of that target is then compiled using that build target's build options.

A project requires a minimum of one target and one source file to compile. A source file may be part of all or none of the targets. Build targets can be dependent upon other targets, which in turn helps to maintain a relationship between different source files. We'll explain a bit more on importance of build targets in the next section.

But before doing that let's create a project and develop an app. Perform the following steps for the same:

  1. Click on the new button in the...

Project with multiple files


In this section we'll learn about C++ app development comprising of multiple files. We'll develop a class, called Vector, which implements a dynamic array. This class is similar to the std::vector class offered by Standard Template Library (STL) and has a very limited set of features compared to STL class.

Create a new project and name it App2. Navigate to File | New | File… menu option and then choose C/C++ header option and follow the wizard to add a new file to App2 project. Add the following code in a new file under App2 and name it vector.h file:

#ifndef __VECTOR_H__
#define __VECTOR_H__

#ifndef DATA_TYPE
#define DATA_TYPE double
#endif

class Vector {
public:
    Vector(size_t size = 2);
    virtual ~Vector();

    size_t GetCount() const;

    bool Set(size_t id, DATA_TYPE data);
    DATA_TYPE operator[] (size_t id);

private:
    DATA_TYPE* m_data;
    size_t     m_size;
};

#endif //__VECTOR_H__

Header file vector.h declares the Vector class structure....

Debug versus release target


We noticed that in App1 and App2, there are two build targets in each project—namely debug and release. In this section we'll learn more about it.

Code::Blocks defines two default build targets—debug and release at the time of a project creation.

As the name suggests a debug target is suitable for app debugging. Appropriate compiler options are added to generate debugging symbols in the compiled app. It also disables all program optimizations.

We can find in the following screenshot (navigate to Project | Build options… menu option) a Debug target has a compiler option Produce debugging symbols. This instructs compiler to generate debugging symbols, which allows app debugging:

A Release target disables generation of debugging symbols. It also defines appropriate compiler options to optimize the program. Thus this is suitable for code to be used in production. The following screenshot shows typical compiler flags in a release target.

These two targets are quite important...

Project with external library


In this section we'll develop an app with an external library. External libraries are used in almost every project written in any language. They allow code reuse resulting faster project cycle. We'll learn how to configure an external library with a Code::Blocks project.

We have printed Hello World! text to console. How about printing text in color? We can use a library called conio2 (http://conio.sourceforge.net/) to print text in color and do other text manipulations. A compiled copy of conio2 library is provided together with the book. Consider the following example code:

#include <cstring>
#include "conio2.h"

int main() {
    int screenWidth = 0;
    const char* msg = "Hello World!\n\n";
    struct text_info textInfo;
    inittextinfo();
    gettextinfo(&textInfo);
    screenWidth  = textInfo.screenwidth;
    textcolor(YELLOW);
    textbackground(RED);
    cputsxy( (screenWidth - strlen(msg))/2 , textInfo.cury, const_cast<char*>(msg) );
 ...

Workspace


Workspace in Code::Blocks is a collection of projects. Workspace acts as a container of projects and also maintains project dependencies. So if project 2 is dependent upon project 1, then project 2 will be compiled before compilation of project 1.

Consider the following snippets. Create a static library project named libcalc by navigating to File | New | Project… and select Static library wizard.

Then replace code of project's main.c file with the following code:

int mult(int a, int b)
{
    return (a * b);
}

Next create a console project named App6 and then replace its main.cpp file with the following code:

#include <iostream>

extern "C" int mult(int a, int b);

int main() {
    std::cout << "2 * 3 = " << mult(2, 3);
    return 0;
}

The Management window now shows two projects in one workspace. Workspace has been renamed to App6 in the following screenshot:

This workspace can be saved by navigating to File | Save workspace as… menu option. Right-click on the App6 project...

Summary


In this chapter we have learned to create project in Code::Blocks. We understood the importance of build targets. We also learned to use external libraries in our project. Finally, we learned to create and use a workspace.

With this we conclude our introduction to projects in Code::Blocks. We'll discuss debugging in the next chapter.

Left arrow icon Right arrow icon

What you will learn

  • Install and configure Code::Blocks
  • Develop console-based C++ applications
  • Learn about Windows application development
  • Understand the role of GUI toolkits
  • Implement advanced Code::Block features

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 25, 2013
Length: 128 pages
Edition :
Language : English
ISBN-13 : 9781783283422
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 25, 2013
Length: 128 pages
Edition :
Language : English
ISBN-13 : 9781783283422
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 43.99
C++ Application Development with Code::Blocks
$43.99
Total $ 43.99 Stars icon
Banner background image

Table of Contents

5 Chapters
Getting Started with Code::Blocks Chevron down icon Chevron up icon
App Development with Code::Blocks Chevron down icon Chevron up icon
App Debugging with Code::Blocks Chevron down icon Chevron up icon
Windows App Development with Code::Blocks Chevron down icon Chevron up icon
Programming Assignment Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.3
(4 Ratings)
5 star 0%
4 star 0%
3 star 50%
2 star 25%
1 star 25%
Montana May 17, 2021
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
... marginal compiler setup and debug info... for an older version of Code Blocks.
Amazon Verified review Amazon
P. Hendrick Jan 02, 2017
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
UPDATE (Jan 2, 2017)For now, I am leaving the rating as 3 stars. (That might change later as I get more qualified to judge a book such as this.)Since yesterday's review, I have finished the Goetz introduction to Code::Blocks, mentioned below. I recommend that pdf file to anyone who is new to C:B. It got me going to where I feel comfortable working with basic C programs -- i.e., compiling them and running them (and doing a little debugging :>). There are only the most minor discrepancies between what is presented there and what you will see in actually running C::B. I think some of that are due to (a) continuing minor updates on the C::B web site not yet reflected in the update of the pdf online; and (b)I am suspicious that a slight variation in button pushes can skip over some of the common dialogs shown in screenshots, depending on the context.As to this book being reviewed on Amazon, I have finished up to a third of chapter 2 (not much, I admit), going through compiling and running a simple C++ program with a single file. But I don't know C++, and am in the process of trying to learn C first. I followed the logic of the first program OK (and it ran OK). But on the next program, involving multiple files, I quickly got passed by. I just don't know enough C++ at this point. I set it up and it ran OK, but I didn't get much out of that. And that is on me. Though there were explanations of what was happening, they were very concise.In my short read into this book, it has definitely gone past what is in the Goetz pdf, and there are about another 70 pages to go. (The pdf did cover some on debugging with C::B, and I haven't gotten into that with this Packt book, yet.)I believe the appropriate reader of this book is someone who has somewhat of an understanding of C++, including putting together several source files involving multiple classes. If you think this book will actually teach you the C++ language, I think you are misinterpreting the title. It is intended to teach you how to do what you already know of C++, but in a Code::Blocks environment.ORIGINAL REVIEW (Jan 1, 2017):Despite the earlier (and only) review which rated it one star, I am going to give this book a try. From the "Look inside" feature, it looked like it might be helpful, at least to me. I bought it as an e-book directly from the publisher's site, where it was fairly cheap (at least for now).I am giving this book a neutral (3-star) rating for now, and will update after I have finished it.I tried the documentation on the code::blocks dev site, but it was not helpful to me. I like to know what all the menu options and toolbars are for (and there are A LOT of these) and why I might want to select them, but that documentation seemed to be geared to someone who has a lot of experience with C/C++ compilers and project management, not me.Before I read this, I will finish reading Code::Blocks Student Manual (a free pdf) by goetz, et al., from CUNY; and come back and update to tell what this has that that doesn't. That pdf looks like it is kept very up-to-date as new builds of Code:Blocks are released. A partial reading has been so-far helpful to me. It does a good job of describing installation and some initial configuration.
Amazon Verified review Amazon
Peter Edwards Jan 17, 2014
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Modak has several books to his credit, so I expected a well structured teaching/reference book. This is not it. Modak is certainly no teacher. The book is a jumble of sketchy information seemly thrown at the reader. At £25 for 109 pages, it is expensive. I have been unable to find any good introduction to Code Blocks, so this book represents a wasted opportunity. Pity!
Amazon Verified review Amazon
Carol and Norm Dec 27, 2013
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I have walls full of reference books and tutorials. This is probably the worst. If I could have given it a lower rating I would have.I consider myself an intermediate programmer although new to Code::Blocks. I'm not a professional programmer but more of a hobbyist. I've used IDE's in the past and was looking for a decent introduction to Code::Blocks to get me up and running. There are no other books out on using Code::Blocks so I jumped on this one, perhaps a bit impulsively.On its cover, this book promises that it will help you "develop advanced applications with Code::Blocks quickly and efficiently with this concise, hands-on guide." In the preface, it states that the target audience is C/C++ developers and that prior knowledge of C/C++ compiler is required. One would assume from this that the target audience is reasonably intelligent and experienced. Anyone in that category would likely feel insulted and ripped off by this book.The short and brief connotation of "concise" is certainly fulfilled. Including the appendix, it's only 109 pages. About two thirds of it is filled with mostly useless screen shots of the program's dialog boxes that only a moron or a high school student would find useful. The clincher for me was the screen shot of a blank Notepad window that takes up a quarter of page 61.The author attempts a tutorial style throughout the book. There are numerous pages of C++ code that he uses as examples. Although he claims to not be trying to teach C/C++, he felt the need to explain in significant depth what various lines of code were trying to accomplish. What's left of the book, the text in fairly large font, could probably fill out a fairly long magazine article that highlights some of the things you can do with Code::Blocks.The author is clearly not a native English speaker. That's ok as long as you have an editor that can back you up. The editor(s) clearly failed here. There are numerous grammatically incorrect sentences, although you can usually figure out what he is trying to say.Chapter 1 is about getting and installing the program. Go to [...] and download the program. Run the installation program and follow all the instructions accepting all the default values suggested. I think you would know to accept the license agreement and to click on the install button on the screen. Hover over the toolbar icons to find out what they do. There, I've just saved you 16 pages of useless reading.Chapter 2 starts off with the obligatory "Hello World" console program. If you're an experienced programmer, you can likely figure out what button to press to compile and run. He next develops a project with a couple of files in it, again as a console program. Again, if you're an experienced programmer, you can likely figure this out on your own too. He spends a few pages here on an introductory example of how to use the debugger. Next is an example of how to include an external library (conio2) in a project. The only part of this chapter I didn't already know was the page and a half discussion of workspaces.Chapter 3 is how to use the debugger in more depth. Pretty standard stuff again. If you've used any debugger before and know how to set breakpoints, there's nothing new here.Chapter 4 gives a couple examples of how to write a Win32GUI project and later a wxWidgets project with the wxSmith plugin. These are merely example programs. There is little or no talk about how to install wxWidgets and configure it to run in Code::Blocks. If you've worked in a RAD environment before, what he talks about is all pretty standard stuff.Chapter 5 is a tutorial on how to develop an image viewer. After my experience with the first four chapters, I decided to save myself from reading it.The appendix talks about a few other features of Code::Blocks: a short paragraph that you can use the Squirrel language to script changes to the program; a few pages on using Doxyblocks to document your code; a page on saving and reusing code snippets; a page saying you can configure other external tools to work with Code::Blocks.Although I consider myself to be a Code::Blocks novice, this book did not teach me anything that I hadn't learned by playing with it and Googling things I didn't know. If you expect any depth at all, or are hoping to learn how to set up the various compiler and linker options, you'll be sorely disappointed.If you want to really learn how to configure and use Code::Blocks, download the user manual from [...] or read the articles on the wiki at [...] If you want to learn about wxWidgets, go to [...] The rest you can get by Googling. Don't waste your time and money on this book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.