C++ application development with codeblocks

128 37 0
C++ application development with codeblocks

Đ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

www.allitebooks.com 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 www.allitebooks.com 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 Production Reference: 1181013 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78328-341-5 www.packtpub.com Cover Image by Paul Steven (mediakitchenuk@gmail.com) www.allitebooks.com Credits Author Project Coordinator Biplab Kumar Modak Reviewers Sageer Parkar Proofreader Zhia Chong Ting Baker Lee Zhi Eng Indexer Abhishek Gupta Hemangini Bari Dinesh Subedi Acquisition Editors Sam Birch Disha Haria Commissioning Editor Technical Editor Dipika Gaonkar Sheetal Aute Ronak Dhruv Rebecca Youe Neil Alexander Graphics Production Coordinator Aparna Bhagat Cover Work Aparna Bhagat www.allitebooks.com 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 www.allitebooks.com 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 www.allitebooks.com Dinesh Subedi is a software developer at Yomari Incorporated Pvt Ltd He is 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 www.allitebooks.com www.PacktPub.com 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 www.allitebooks.com www.allitebooks.com Table of Contents Preface 1 Chapter 1: Getting Started with Code::Blocks Why Code::Blocks? Installing Code::Blocks on Windows Installing Code::Blocks on Linux 10 First run 12 Introduction to important toolbars 15 Main toolbar 15 Compiler toolbar 15 Debugger toolbar 16 Summary 16 Chapter 2: App Development with Code::Blocks 17 Chapter 3: App Debugging with Code::Blocks 39 Creating your first app with Code::Blocks Project in Code::Blocks Project with multiple files Debug versus release target Project with external library Workspace Summary 17 19 23 28 31 35 37 Introduction to debugging in Code::Blocks 39 First app debugging 42 Multiple app debugging 51 Summary 59 www.allitebooks.com Appendix This Appendix focuses on the feature set of Code::Blocks Apart from code editing, managing, and building, Code::Blocks has numerous other features It can be scripted to extend features of Code::Blocks It has plugin to generate documentation of code It is also able to export code in different formats such as rich text format, portable document format, and so on It can also manage snippets of code to ease the development process We shall discuss about them in the next couple of sections Scripting Code::Blocks Code::Blocks uses Squirrel language for scripting Squirrel language is a high-level, object oriented, and light weight programming language Squirrel syntax is similar to C/C++ programing language Code::Blocks exposes a large amount of its API via scripting As a result several aspects of Code::Blocks can be extended via scripting Refer to the documentation from the following URLS for scripting references: • Scripting commands: http://wiki.codeblocks.org/index php?title=Scripting_commands • API bindings: http://wiki.codeblocks.org/index.php?title=Script_ bindings Documentation generation Documentation of code is very important for any project It builds an overview of the written code, explains its usage, and helps developers understand the code Code::Blocks allows generation of code documentation from the IDE itself Appendix Doxygen is a standard tool to create documentation from annotated C++ files Code::Blocks comes with a plugin called DoxyBlocks that creates an interface with the externally installed doxygen tool We need to download and install doxygen tool first Subsequently we can use DoxyBlocks plugin to generate documentation Perform the following steps: Download doxygen from the following URL—http://www.stack nl/~dimitri/doxygen/download.html Also download doxygen-x.x.xsetup.exe file Double-click on that file to install it We need to connect DoxyBlocks plugin with doxygen tool Go to DoxyBlocks | Open preferences… menu option The following screenshot will be displayed: [ 104 ] Appendix Click on the General tab Next click on the Browse button next to Path To doxygen option and set the path to C:\Program Files\doxygen\bin\ doxygen.exe Next create a new C++ console project and add the following code to wizard generated main.cpp file: class A { public: A() {}; ~A() {}; virtual int CallMe(int a) = 0; }; class B : public A { public: B() {}; ~B() {}; int CallMe(int a) { return a; } }; int main() { return 0; } Navigate to DoxyBlocks | Extract documentation menu option or press Ctrl + Alt+ E key combination Code::Blocks will now generate documentation of the project inside doxygen folder Go to DoxyBlocks | Run HTML menu option or press the Ctrl + Alt + H key combination to open the newly created documentation in a Web browser We can also add additional detailed description of function, class, etc to create a detailed documentation Move the cursor to the beginning of B::CallMe() function and click on the DoxyBlocks | /** Block comment menu option or press Ctrl + Alt + B key combination Code::Blocks will analyze the function parameters and will insert a default comment block suitable for doxygen tool Adjust the comment block and our code will look similar to the following snippet: ~B() {}; /** \brief Virtual function CallMe() is defined here * * \param a int [ 105 ] Appendix * \return int * */ int CallMe(int a) { Press Ctrl + Alt + E key combination to regenerate the documentation and use the Ctrl + Alt + H key combination to open it inside Web browser Documentation of B::CallMe() will look similar to the following screenshot: We can also customize DoxyBlocks plugin option to use advanced features of doxygen Management of code snippets Code::Blocks allows developers to store and retrieve frequently used code snippets We have used DoxyBlocks plugin in our previous example to comment blocks suitable for generating documents However we can also save a blank template as a code snippet and reuse it wherever needed Go to View | Code snippets menu option to show the CodeSnippets window Right-click on the codesnippets in the tree and select Add SubCategory menu option [ 106 ] Appendix Name it doxygen Right-click on this doxygen category and click on the Add snippet menu option Enter Block comment as Label and following code as the snippet text: \** \brief * */ Click on the OK button to save this snippet CodeSnippets window will look similar to the following screenshot: Now click on any place in the Code::Blocks editor window, right-click on this snippet and select Apply menu option This snippet will now be pasted into the Editor window We can add code, bookmarks, text files as a code snippet Code snippets are not project specific and works across all the projects Use of external tools for a project Code::Blocks allows user to use external tools for any project Imagine we want to use doxygen tool to generate documentation without using the DoxyBlocks plugin We can add doxygen as an external tool and then use it on demand Go to Tools | Configure tools… menu option to add a new tool The following window will be opened: [ 107 ] Appendix Click on the Add button to add a new tool The following window will be opened: Enter following details: °° Set Name property to doxygen This value will be used to create a new menu item under Tools menu °° Set Executable property to C:\Program Files\doxygen\bin\ doxygen.exe °° Set Parameters property to ${PROJECT_DIR}doxygen\doxyfile °° Set Working directory property to ${PROJECT_DIR}doxygen\ Click on the OK button to close this window and then click on the OK button to close User-defined tools window A menu item will be created under Tools menu option Navigate to Tools | doxygen menu option and the doxygen tool will be launched inside a console window Press any key to close this console window when it has completed We can use any other tool in a similar manner [ 108 ] Appendix Exporting source code in different format Code::Blocks allows user to export source code to HTML, RTF, ODF, or PDF format Perform the following steps to export source in different format: To export a file as PDF format go to File | Export | As PDF… menu option Enter a file name and path in the next dialog Click on the Save button to continue Code::Block will prompt to confirm inclusion of line numbers in the exported source code Select Yes or No option and that particular source file will be exported [ 109 ] Index Symbols declspec(dllexport) 53 declspec(dllimport) 53 A App2 project, with multiple files creating 23-28 app debugging performing 42-50 app, with external library developing 31-35 AT&T style 46 C CDB about 39 downloading 39 Code::Blocks code snippets, managing 106, 107 debuggers 39 debugging 39 debug, vs release target 28 documentation generation 103, 105 external tools, using 107, 108 Hello World app, creating 17-19 project 19 project, with external library 31-33 project, with multiple files 23 scripting 103 source, exporting different format 109 toolbars 15 UI components 14 Windows app development 61 workspace 35 code snippets managing 106, 107 compiler toolbar 15 conio2 library 32 Console Debugger See  CDB CreateWindowEx() function 67 D debuggers CDB 39 GDB 39 debugger toolbar 16, 40 debugging 39 debug menu about 40, 41 debugger settings 41, 42 debug target about 28 vs release target 28-31 DECLARE_EVENT_TABLE() function 74 Destroy() function 75 DllMain() function 54 documentation generating 103-106 DoxyBlocks 104 Doxygen about 104 downloading 104 installing 104 Dynamic Link Libraries (DLL) 47 E event driven Windows app about 67 creating 67-70 exercise app exercise problem, solving 94-100 solution 94 external tools using, for project 107, 108 App1 project, creating 21, 22 creating 20, 21 pure virtual function 53 G Qt 71 GDB 39 GetCount() function 24 GetMessage() function 67 GNU Debugger See  GDB GUI toolkit about 70 Microsoft Foundation Class (MFC) 70 Qt 71 wxWidgets 71 R H Hello World app creating 17-19 I Intel style 46 M main toolbar 15 memset() function 26 MessageBox() function 63 Microsoft Foundation Class (MFC) 70 multiple app debugging performing 51-59 O Object Oriented Programming (OOP) 23 OnClickMe() function 75 OnClose() function 75 Open() function 86 P PostQuitMessage() function 69 project about 19, 20 Q RegisterClassEx() function 67 release target 29 S ShowModal() function 86 ShowWindow() function 67 source code exporting, in different format 109 Squirrel language 103 Standard Template Library (STL) 23 T toolbars about 15 compiler toolbar 15 debugger toolbar 16 main toolbar 15 U UI components 14 Unicode 65 Unicode support enabling, in Windows app 65 V Vector class 23 W Win32 API advantages 70 disadvantages 70 WindowProcedure() function 67, 69 [ 112 ] Windows app development entry point 63 event driven apps 67 functionalities, creating with wxWidgets 71-75 Hello World app, creating 62-65 Unicode support, enabling 65 wxWidgets GUI toolkit 70 Windows SDK 39 WinMain() function 63, 67 WM_COMMAND event 69 WM_CREATE event 69 WM_DESTROY event 69 WM_QUIT event 69 workspace 35, 36 wxSmith components 80 Property window 80 Resource tree 80 used, for rapid app development 80-88 wxSmith window 80 wxWidgets cross-platform development capability 71 wxWidgets toolkit 71 [ 113 ] Thank you for buying C++ Application Development with Code::Blocks About Packt Publishing Packt, pronounced 'packed', published its first book "Mastering phpMyAdmin for Effective MySQL Management" in April 2004 and subsequently continued to specialize in publishing highly focused books on specific technologies and solutions Our books and publications share the experiences of your fellow IT professionals in adapting and customizing today's systems, applications, and frameworks Our solution based books give you the knowledge and power to customize the software and technologies you're using to get the job done Packt books are more specific and less general than the IT books you have seen in the past Our unique business model allows us to bring you more focused information, giving you more of what you need to know, and less of what you don't Packt is a modern, yet unique publishing company, which focuses on producing quality, cutting-edge books for communities of developers, administrators, and newbies alike For more information, please visit our website: www.packtpub.com About Packt Open Source In 2010, Packt launched two new brands, Packt Open Source and Packt Enterprise, in order to continue its focus on specialization This book is part of the Packt Open Source brand, home to books published on software built around Open Source licences, and offering information to anybody from advanced developers to budding web designers The Open Source brand also runs Packt's Open Source Royalty Scheme, by which Packt gives a royalty to each Open Source project about whose software a book is sold Writing for Packt We welcome all inquiries from people who are interested in authoring Book proposals should be sent to author@packtpub.com If your book idea is still at an early stage and you would like to discuss it first before writing a formal book proposal, contact us; one of our commissioning editors will get in touch with you We're not just looking for published authors; if you have strong technical skills but no writing experience, our experienced editors can help you develop a writing career, or simply get some additional reward for your expertise Mastering Windows C++ App Development ISBN: 978-1-849695-02-2 Paperback: 304 pages A practical guide to developing Windows Store apps with C++ and XAML Details the most important features of C++, XAML, and WinRT for building fantastic Windows Store apps Full of detailed and engaging code samples that can be used as a basis for your own projects Provides a clear overview of Windows Runtime and C++/CX Boost.Asio C++ Network Programming ISBN: 978-1-782163-26-8 Paperback: 156 pages Enhance your skills with practical examples for C++ network programming Augment your C++ network programming using Boost.Asio Discover how Boost.Asio handles synchronous and asynchronous programming models Practical examples of client/server applications Learn how to deal with threading when writing network applications Please check www.PacktPub.com for information on our titles Microsoft Visual C++ Windows Applications by Example ISBN: 978-1-847195-56-2 Paperback: 440 pages Code and explanation for real-world MFC C++ Applications Learn C++ Windows programming by studying realistic, interesting examples A quick primer in Visual C++ for programmers of other languages, followed by deep, thorough examples Example applications include a Tetris-style game, a spreadsheet application, a drawing application, and a word processor Boost C++ Application Development Cookbook ISBN: 978-1-849514-88-0 Paperback: 348 pages Over 80 practical, task-based recipes to create applications using Boost libraries Explores how to write a program once and then use it on Linux, Windows, MacOS, and Android operating systems Includes everyday use recipes for multithreading, networking, metaprogramming, and generic programming from a Boost library developer Take advantage of the real power of Boost and C++ to get a good grounding in using it in any project Please check www.PacktPub.com for information on our titles ... we shall start coding in C++ with Code::Blocks [ 16 ] 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.. .C++ Application Development with Code::Blocks Develop advanced applications with Code::Blocks quickly and efficiently with this concise, hands-on guide Biplab... 103 103 106 107 109 Index 111 [ ii ] Preface C++ Development with Code::Blocks is a concise and practical guide for application development using C++ and Code::Blocks This book gives you several

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

Từ khóa liên quan

Mục lục

  • Cover

  • Copyright

  • Credits

  • About the Author

  • About the Reviewers

  • www.PacktPub.com

  • Table of Contents

  • Preface

  • Chapter 1: Getting Started with Code::Blocks

    • Why Code::Blocks?

    • Installing Code::Blocks on Windows

    • Installing Code::Blocks on Linux

    • First run

    • Introduction to important toolbars

      • Main toolbar

      • Compiler toolbar

      • Debugger toolbar

      • Summary

      • Chapter 2: App Development with Code::Blocks

        • Creating your first app with Code::Blocks

        • Project in Code::Blocks

        • Project with multiple files

        • Debug versus release target

Tài liệu cùng người dùng

Tài liệu liên quan