1. Trang chủ
  2. » Công Nghệ Thông Tin

C++ application development with codeblocks

128 39 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 128
Dung lượng 6,41 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Table of ContentsPreface 1 Chapter 1: Getting Started with Code::Blocks 5 Chapter 2: App Development with Code::Blocks 17 Creating your first app with Code::Blocks 17 Project with multip

Trang 2

C++ Application Development with Code::Blocks

Develop advanced applications with Code::Blocks

quickly and efficiently with this concise, hands-on guide

Biplab Kumar Modak

BIRMINGHAM - MUMBAI

Trang 3

C++ Application Development with Code::Blocks

Copyright © 2013 Packt Publishing

All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews

Every effort has been made in the preparation of this book to ensure the accuracy

of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information.First published: October 2013

Trang 4

Production Coordinator

Aparna Bhagat

Cover Work

Aparna Bhagat

Trang 5

About the Author

Biplab Kumar Modak is a passionate, open-source software developer He started learning programming at school in the year 1996 with a BBC Micro computer system The sheer limitation of computing resources in a BBC Micro computer system forced him to think out-of-the-box and sharpened his programming skills He participated in several software development competitions at school level and won several awards before joining college

By then he started learning and developing in C and C++ language He used his C/C++ development skills for his academic and extracurricular projects He started contributing to the Code::Blocks project in the year 2006 and since then has remained

as one of the developers of Code::Blocks He has also contributed to a few other open source projects Overall he has about 17 years of freelance software development experience and out of which 14 years in C/C++ development experience

He is basically an Civil and Structural Engineer by profession His job involves analyzing and designing tall buildings and other structures He was one of the key engineers behind recent upgrade of Terminal 1, Singapore Changi Airport, and several public and private housing projects He spends his free time in learning new technologies and in developing Code::Blocks and other such projects

I want to thank Mr Rabindra Prakash Sarkar for introducing me to

the world of programming

I also want to thank my parents for their support, encouragement,

and blessings I thank my wife, Ishita, for her support all these years

I would also like to thank Pampa, Pradip, Bubai, Shyamali Das and

her family, Jibananda Mukherjee, all other family members, Atish,

and my in-laws for their support and encouragement

Trang 6

About the Reviewers

Zhia Chong is a young tech entrepreneur based in Seattle, WA His background

in technology ranges from C/C++ to Python He recently graduated Magna Cum Laude from Gonzaga University with a Bachelor's degree in Computer Science and currently works for PaperG, a young startup in Seattle He dedicates most of his time for writing beautiful, elegant code, and understanding business dynamics in the startup industry He dreams of building his own successful startup

He can be easily contacted via e-mail at zhiachong@gmail.com or his through website zhiachong.com

Lee Zhi Eng is a 3D artist-turned-programmer He has worked as a game artist and game programmer in several local game studios in his country; before becoming a contractor and a part-time lecturer at a local university, teaching game development subjects, in particularly related to Unity Engine, and Unreal Development Kit You can find more information about him at http://www.zhieng.com

Abhishek Gupta is a software engineer who has worked in the area of

Automotive In-Vehicle Infotainment (IVI) since last two years He has worked

on a software-based HD video conferencing system as part of his MTech in Visual Information and Embedded Systems at IIT Kharagpur, India, in 2011

He is passionate about video processing and loves to work on embedded

multimedia systems

Trang 7

currently working on data warehouse technology and business intelligence He is a blogger at www.codeincodeblock.com He writes writes articles related to software development using Code::Blocks IDE and has four years of experience in it.

He has completed a Bachelor's degree in Computer Engineering at Pulchowk Campus IOE Kathmandu, Nepal

I would like to thank my friend Bibek Subedi and my brother Bharat Subedi who helped me while reviewing this book

Trang 8

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related

to your book

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details

At www.PacktPub.com, you can also read a collection of free technical articles, sign

up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library Here, you can access, read and search across Packt's entire library of books

Why Subscribe?

• Fully searchable across every book published by Packt

• Copy and paste, print and bookmark content

• On demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books Simply use your login credentials for immediate access

Trang 10

Table of Contents

Preface 1 Chapter 1: Getting Started with Code::Blocks 5

Chapter 2: App Development with Code::Blocks 17

Creating your first app with Code::Blocks 17

Project with multiple files 23 Debug versus release target 28 Project with external library 31

Chapter 3: App Debugging with Code::Blocks 39

Introduction to debugging in Code::Blocks 39

Summary 59

Trang 11

Chapter 4: Windows App Development with Code::Blocks 61

Rapid app development with wxSmith 78

Exercise 88

Summary 89

Chapter 5: Programming Assignment 91

Developing MyPaint – an image viewer 91

Solution of exercise problem 94 Summary 101

Index 111

Trang 12

C++ Development with Code::Blocks is a concise and practical guide for application

development using C++ and Code::Blocks This book gives you several examples and step-by-step guides to begin with and then gradually progress to complex application development with C++ It also cleverly uses tutorials to elaborate

Code::Blocks features for the readers This book covers Code::Blocks Version 12.11 However, tutorials will be applicable to newer releases

What this book covers

Chapter 1, Getting Started with Code::Blocks, will help us to install Code::Blocks on

Windows and Linux

Chapter 2, App Development with Code::Blocks, will help us to develop a simple app,

to develop an app as a project, using external libraries with project, and concept

of the workspace

Chapter 3, App Debugging with Code::Blocks, explains the debugger related features

offered by Code::Blocks and also to debug single and multiple apps

Chapter 4, Windows App Development with Code::Blocks, describes how to develop apps

using Code::Blocks for the Windows operating system We will also learn to use wxWidgets and the way to use it for developing cross-platform apps

Chapter 5, Programming Assignment, explains how to develop an app from scratch

using Code::Blocks We will look at a completed app, dissect it, and then develop it using Code::Blocks

Appendix, discusses some advanced features of Code::Blocks We will also learn about

documentation generation, exporting source file, and so on in this chapter

Trang 13

What you need for this book

The following software is required to learn and follow examples demonstrated in the book:

Who this book is for

The target audience of this book is C/C++ developers Prior knowledge of C/C++ compiler is required This book is suitable for developers who want to learn about Code::Blocks and app development in C++ using it

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information Here are some examples of these styles, and an explanation of their meaning

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

Any command-line input or output is written as follows:

g++ -o app4.exe –g –O2 main.cpp

Trang 14

New terms and important words are shown in bold Words that you see on the

screen, in menus or dialog boxes for example, appear in the text like this: "clicking

the Next button moves you to the next screen".

Warnings or important notes appear in a box like this

Tips and tricks appear like this

Reader feedback

Feedback from our readers is always welcome Let us know what you think about this book—what you liked or may have disliked Reader feedback is important for us

to develop titles that you really get the most out of

To send us general feedback, simply send an e-mail to feedback@packtpub.com, and mention the book title via the subject of your message

If there is a topic that you have expertise in and you are interested in either writing

or contributing to a book, see our author guide on www.packtpub.com/authors

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com If you purchased this book

elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you

Trang 15

Although we have taken every care to ensure the accuracy of our content, mistakes do happen If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us By doing so, you can save other readers from frustration and help us improve subsequent versions of this book

If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link,

and entering the details of your errata Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list

of existing errata, under the Errata section of that title Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media

At Packt, we take the protection of our copyright and licenses very seriously If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy

Please contact us at copyright@packtpub.com with a link to the suspected

Trang 16

Getting Started with

Code::BlocksWhile writing this book, Code::Blocks—12.11 was the latest stable release available This release comes with GCC 4.7.1 compiler for Windows We'll use this release for C++ development throughout this book In this chapter, we'll download

Code::Blocks, install and learn more about it

Why Code::Blocks?

Before we go on learning more about Code::Blocks let us understand why we shall

use Code::Blocks over other IDEs

• It is a cross-platform Integrated Development Environment (IDE) It supports Windows, Linux, and Mac operating system

• It supports GCC compiler and GNU debugger on all supported

Trang 17

Installing Code::Blocks on Windows

Our primary focus of this book will be on Windows platform However, we'll touch upon other platforms wherever possible Official Code::Blocks binaries are available from www.codeblocks.org Perform the following steps for successful installation of Code::Blocks:

1 For installation on Windows platform download setup.exe file from http://www.codeblocks.org/downloads/26 or from sourceforge mirror http://sourceforge.net/projects/codeblocks/files/Binaries/12.11/Windows/codeblocks-12.11mingw-setup.exe/download and save it in a folder

codeblocks-12.11mingw-2 Double-click on this file and run it You'll be presented with the

following screen:

3 As shown in the following screenshot click on the Next button to

continue License text will be presented The Code::Blocks application is licensed under GNU GPLv3 and Code::Blocks SDK is licensed under

GNU LGPLv3 You can learn more about these licenses at this

URL—https://www.gnu.org/licenses/licenses.html

Trang 18

4 Click on I Agree to accept the License Agreement The component selection

page will be presented in the following screenshot:

Trang 19

5 You may choose any of the following options:

° Default install: This is the default installation option This will install

Code::Block's core components and core plugins

° Contrib Plugins: Plugins are small programs that extend

Code::Block's functionality Select this option to install plugins contributed by several other developers

° C::B Share Config: This utility can copy all/parts of

configuration file

° MinGW Compiler Suite: This option will install GCC 4.7.1

for Windows

6 Select Full Installation and click on Next button to continue As shown in the

following screenshot installer will now prompt to select installation directory:

Trang 20

[ 9 ]

7 You can install it to default installation directory Otherwise choose

Destination Folder and then click on the Install button Installer will

now proceed with installation

8 As shown in the following screenshot Code::Blocks will now prompt us to run it after the installation is completed:

Trang 21

9 Click on the No button here and then click on the Next button Installation

will now be completed:

10 Click on the Finish button to complete installation A shortcut will be created

on the desktop

This completes our Code::Blocks installation on Windows

Installing Code::Blocks on Linux

Code::Blocks runs numerous Linux distributions In this section we'll learn about installation of Code::Blocks on CentOS linux CentOS is a Linux distro based on Red Hat Enterprise Linux and is a freely available, enterprise grade Linux distribution Perform the following steps to install Code::Blocks on Linux OS:

1 Navigate to Settings | Administration | Add/Remove Software menu

option Enter wxGTK in the Search box and hit the Enter key As of writing

wxGTK-2.8.12 is the latest wxWidgets stable release available Select it and

click on the Apply button to install wxGTK package via the package manager,

as shown in the following screenshot

Trang 22

2 Download packages for CentOS 6 from this URL—http://www.

codeblocks.org/downloads/26

Unpack the tar.bz2 file by issuing the following command in shell:

tar xvjf codeblocks-12.11-1.el6.i686.tar.bz2

3 Right-click on the codeblocks-12.11-1.el6.i686.rpm file as shown in the

following screenshot and choose the Open with Package Installer option.

Trang 23

4 The following window will be displayed Click on the Install button to begin

installation, as shown in the following screenshot:

5 You may be asked to enter the root password if you are installing it from a

user account Enter the root password and click on the Authenticate button

Code::Blocks will now be installed

6 Repeat steps 4 to 6 to install other rpm files

We have now learned to install Code::Blocks on the Windows and Linux platforms

We are now ready for C++ development Before doing that we'll learn about the Code::Blocks user interface

First run

On the Windows platform navigate to the Start | All Programs | CodeBlocks |

CodeBlocks menu options to launch Code::Blocks Alternatively you may

double-click on the shortcut displayed on the desktop to launch Code::Blocks,

as in the following screenshot:

On Linux navigate to Applications | Programming | Code::Blocks IDE menu

options to run Code::Blocks Please note that in subsequent chapters of this book we'll limit our discussion mostly to the Windows platform However, usage of Code::Blocks and C++ development (excluding platform specific areas) remain the same over both platforms

Trang 24

Code::Blocks will now ask the user to select the default compiler Code::Blocks supports several compilers and hence, is able to detect the presence of other

compilers The following screenshot shows that Code::Blocks has detected GNU GCC Compiler (which was bundled with the installer and has been installed)

Click on it to select and then click on Set as default button, as shown in the

following screenshot:

Do not worry about the items highlighted in red in the previous screenshot

Red colored lines indicate Code::Blocks was unable to detect the presence of a particular compiler

Finally, click on the OK button to continue with the loading of Code::Blocks After

the loading is complete the Code::Blocks window will be shown

Trang 25

The following screenshot shows main window of Code::Blocks Annotated portions highlight different User Interface (UI) components:

Now, let us understand more about different UI components:

• Menu bar and toolbar: All Code::Blocks commands are available via

menu bar On the other hand toolbars provide quick access to commonly used commands

• Start page and code editors: Start page is the default page when

Code::Blocks is launched This contains some useful links and recent project and file history Code editors are text containers to edit C++ (and other language) source files These editors offer syntax highlighting—a feature that highlights keywords in different colors

Trang 26

• Management pane: This window shows all open files (including source files,

project files, and workspace files) This pane is also used by other plugins to

provide additional functionalities In the preceding screenshot FileManager plugin is providing a Windows Explorer like facility and Code Completion

plugin is providing details of currently open source files

• Log windows: Log messages from different tools, for example, compiler,

debugger, document parser, and so on, are shown here This component is also used by other plugins

• Status bar: This component shows various status information of

Code::Blocks, for example, file path, file encoding, line numbers, and so on

Introduction to important toolbars

Toolbars provide easier access to different functions of Code::Blocks Amongst the several toolbars following ones are most important

Main toolbar

The main toolbar holds core component commands From left to right there are new file, open file, save, save all, undo, redo, cut, copy, paste, find, and replace buttons

Compiler toolbar

The compiler toolbar holds commonly used compiler related commands From left

to right there are build, run, build and run, rebuild, stop build, build target buttons Compilation of C++ source code is also called a build and this terminology will be used throughout the book

Trang 27

Debugger toolbar

The debugger toolbar holds commonly used debugger related commands From left to right there are debug/continue, run to cursor, next line, step into, step out, next instruction, step into instruction, break debugger, stop debugger, debugging windows, and various info buttons

Summary

In this chapter we learnt to download and install Code::Blocks We also learnt about different interface elements In the next chapter we shall start coding in C++ with Code::Blocks

Trang 28

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:

Trang 29

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:

After the code is typed in the editor window Code::Blocks will look similar to the following screenshot

Trang 30

[ 19 ]

Now click on the save button in main toolbar to save this file (navigate to File | Save

from the dropdown menu bar) Alternatively Ctrl + S key combination can be used to

save a file We can see that Code::Blocks has applied syntax highlighting to the code and it has made the code more readable

Now click on the build button in the Compiler toolbar or hit Ctrl + F9 key

combination to compile it If everything goes well Code::Blocks will look similar to the previous screenshot Now click on the run button in Compiler toolbar Code::Blocks will now run the program As seen in the following screenshot our first program has run successfully:

The previous screenshot shows that the program execution has been completed and

it is waiting for user input to close the window This is a Code::Blocks feature

which stops after the execution is completed in order to allow the users to study program output

Our first assignment is successful However, this approach has several drawbacks

• Code::Blocks applies global compiler/linker flags during compilation of individual files

• Code::Blocks behaves purely as a text editor (imagine Notepad) and most features can't be used to compile individual files

Also management of large projects comprising of individual files is cumbersome So

the concept of Project has evolved In the next section we'll learn more about projects

Trang 31

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

File 1 File 2

Target 2 File 1

File 2

Target 1

File 2 File 3 Target 3

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 main toolbar, then click on the Project menu

option A wizard will be presented, as shown in the following screenshot

Now select Console application and click on the Go button:

Trang 32

2 Click on the Next button on the first page of the wizard Then choose C++ and click on the Next button as shown in the following screenshot:

3 As shown in the following screenshot enter Project title (app name) as App1and choose a folder to create App1 project Now, click on the Next button

to continue

Trang 33

4 Click on the Finish button in the Console application window as shown

in the following screenshot and the project will be generated with a

default code:

The following screenshot shows the Management window that has been populated

with the newly created project files Double-click on the main.cpp item on the tree to open the Code::Blocks editor

Let's replace the default code with the following code:

#include <iostream>

class HelloWorld {

public:

HelloWorld() {}

Trang 34

We have replaced earlier HelloWorld code with a code using Object Oriented

Programming (OOP) concepts of C++ We have used C++ classes to achieve the

same goal of printing "Hello World!" text

C++ classes are specific data types that can be defined as a collection of data

structure and member functions that operate on these data structures All member functions and base classes are private by default Classes can contain overloaded operator which allows customized operations associated with a particular class.Classes can also be defined with a struct keyword However, all members, that

is, functions and base classes are public by default if a class is defined with a struct keyword

Let's analyze our code We have defined a class named HelloWorld We have

also defined a constructor function HelloWorld() and a destructor function

~HelloWorld() We have a publicly accessible function named Print() to print out

"Hello World!" text In the main() function we created an object named hello of class HelloWorld and then we have used it to call Print() function

Hit F9 key to build and then run this project A console window will pop up

displaying "Hello World!" text

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

Trang 35

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:

size_t GetCount() const;

bool Set(size_t id, DATA_TYPE data);

DATA_TYPE operator[] (size_t id);

There are several member functions that operate on the member variables The GetCount() function returns number array size, Set() function assigns a value

to an element in array An operator [] has been overloaded to access array data

Trang 36

The Vector class has been implemented in the vector.cpp file Create and add this new file to App2 project and then copy the following code to it:

m_data = new DATA_TYPE[m_size];

::memset(m_data, 0, m_size * sizeof(DATA_TYPE));

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub

com If you purchased this book elsewhere, you can visit http://

www.packtpub.com/support and register to have the files e-mailed directly to you

Trang 37

The line m_size(size) defines an initializer list where member variables are initialized as per the order they have been declared We have used new operator to allocate an array of size given by user The memset() function initializes that array with zeroes In destructor internal array is checked for null pointer and then de-allocated with delete [] keyword and assigned a null pointer.

Null pointers have a value (typically 0) that is reserved to indicate that it doesn't point to any valid object Any operation on null pointers will lead to a segmentation fault or access violation In such

a case, app will die instantly C++ 11 defines a separate nullptr constant to define a null pointer

There are two member functions, Set() and GetCount() that operate on the internal array

Finally, replace code inside the main.cpp file with the following code It creates an object of Vector class and subsequently uses it:

#include <iostream>

#include "vector.h"

int main() {

Vector vec(4);

vec.Set(0, 10); // Set first item = 10

vec.Set(2, 55); // Set first item = 55

std::cout << "Number of elements = " << vec.GetCount() <<

std::endl;

std::cout << "vec[1] = " << vec[1] << std::endl;

std::cout << "vec[2] = " << vec[2] << std::endl;

return 0;

}

Now, the Management window will look similar to the following screenshot:

Trang 38

We'll define a pre-processor define to ensure that the Vector class is compiled as

an array of integers Navigate to Project | Build options… menu option and the

Project build options window will be presented:

As we intend to apply the settings throughout the project click on the root of the

project tree in that window Now, click on the Compiler settings | #defines tab and add the line as per the preceding screenshot Further, click on the OK button to close

that dialog box Now compile and run this project This will produce result as per the following screenshot:

In our code we have a pre-processor macro DATA_TYPE that defines the data type that this class holds If we intend to use it as an array of double we have to recompile this app

Trang 39

Do note that pre-processor macros work by simple text substitution and no type checking is performed on them during substitution This can introduce other

bugs in the program if it is used incorrectly

In this section we learned about app development with multiple files, tweaking of compiler options

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:

Trang 40

[ 29 ]

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 as it is difficult to debug a program that has been compiled with compiler optimization flags enabled It is highly recommended that the program is compiled without optimization in debug target

To understand this problem we'll use the following code snippet, then compile and debug it Note that we will use command-line tools to avoid abstraction of any error message by Code::Blocks UI:

Ngày đăng: 12/10/2020, 17:38

TỪ KHÓA LIÊN QUAN

w