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
€8.99 €25.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Netherlands

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

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

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Netherlands

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 32.99
C++ Application Development with Code::Blocks
€32.99
Total 32.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela