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
₹799 ₹2621.99
Paperback
₹3276.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with a Packt Subscription?

Free for first 7 days. ₹800 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781783283415
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. ₹800 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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
₹800 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
₹4500 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 ₹400 each
Feature tick icon Exclusive print discounts
₹5000 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 ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 3,276.99
C++ Application Development with Code::Blocks
₹3276.99
Total 3,276.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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.