Getting started – our sample library
This chapter's example code has two pieces: a library that defines a public function and a console application that calls this function. Libraries are a great way to break up your applications, and while this example is simple, it also lets me show you how to create a library and include it in your application.
I'm going to stretch your imagination a bit; let's pretend that you're responsible for setting up a library of math functions. In this example, we'll only write one function, factorial. You should be able to recollect the factorial function from introductory programming; it's represented by a !
and is defined as follows:
- 0! is 0
- 1! is 1
- n! is n × (n - 1)!
This is a recursive definition and we can code it in the following way:
unsigned long factorial(unsigned int n) { switch(n) { case 0: return 0; case 1: return 1; default: return n * factorial(n-1); } }
An alternate definition that avoids the cost of function calls is given as follows:
unsigned long factorial(unsigned int n) { unsigned long result = 1; for(unsigned int i = n; i > 1; i--) { result *= i; } return result; }
Why did I pick the recursive definition? There are three reasons for this: I think that it's clearer, the function call's performance overhead isn't a big deal in this example, and many of you might be using this book as part of introductory Computer Science courses where recursion is taught and should be reinforced.
Let's begin by creating the library that implements our factorial function. To do this, follow these steps:
- In Qt Creator, from the File menu, choose New File or Project….
- Select Libraries in the left-hand side pane of the dialog and select C++ Library from the center pane.
- Qt Creator can create dynamic libraries (DLLs in Windows parlance), static libraries, or plugins that can be shared between applications. We're going to create a static library; so, in the next window that appears, select Statically Linked Library and name it
MathFunctions
. Choose a reasonable path for the project.Tip
A statically linked library is included in your program binary and is part of your application. If multiple applications use a static library, each will have its own copy. A dynamically linked library is stored as a separate file and can be shared by multiple applications at runtime because each application loads the dynamically linked library. Qt also supports plugins, which are dynamic libraries loaded at runtime that can extend an application's functionality.
- In the next step of the wizard, leave the Qt version, Debug, and Release items checked.
- Libraries built by Qt Creator can rely on the Qt libraries. Let this library rely on QtCore, the core data structures for Qt; in the Select Required Modules window, leave QtCore checked and click on Next.
- In the next window, name the skeleton files that Qt Creator will add to your project. Click on Next.
- In the Project Management window, choose <None> for the version control choice (we won't use version control for this project) and click on Finish.
- Edit
mathfunctions.h
to include a static method declaration for our factorial function:#ifndef MATHFUNCTIONS_H #define MATHFUNCTIONS_H class MathFunctions { public: MathFunctions(); static unsigned long int factorial(unsigned int n); }; #endif // MATHFUNCTIONS_H
- Open
mathfunctions.cpp
. You can do this in one of the two or three ways available: either by double-clicking on it in the Project pane, by right-clicking on the factorial function and selecting Switch Header/Source, or by simply hitting the F4 key. Write your factorial function;mathfunctions.cpp
should now comprise something similar to this:#include "mathfunctions.h" MathFunctions::MathFunctions() { } unsigned long int MathFunctions::factorial(unsigned int n) { switch(n) { case 0: return 0; case 1: return 1; default: return n * factorial(n-1); } }
- Click on the Projects button on the left-hand side and change the output paths for the Release and Debug builds to point to the same directory by editing the Build directory line under General, first for the Build and then for the Debug build configurations. To do this, remove the release and debug portions of the directory path from the Build directory path. This way, when you build your library, Qt Creator will place the release and debug builds of your library in folders named
release
anddebug
, respectively.
As you write the code, note that Qt Creator prompts you at various stages about things it can deduce from your header with automatic suggestions (called autosuggest). For example, once you type MathFunc
, it offers to autocomplete the class name or the C preprocessor guard; you can select the class name either using the mouse or just hit Return to get the class name.
Similarly, typing the double colons tells Qt Creator that you're trying to enter something in the MathFunctions
class, and it prompts you with the MathFunctions
class members; you can use the arrows to select factorial
and hit Return, and it will type that.
Finally, typing an opening parenthesis cues Qt Creator that you're defining a function, and it prompts you with the arguments to the function you defined in the header file. You'll see this autocompletion a lot when you type code; it's a great way to learn Qt too, because you can type a class name or part of a function name and Qt Creator prompts you with helpful hints along the way. Qt Creator can also autocomplete variable and method names; start typing a function name and press Ctrl + Space bar to see a menu of possible completions.
Before you continue, be sure to build your library in both the release and debug configurations. The easiest way to do this is to click on the build selector at the bottom-left of the software and select either Release or Debug and then click on the hammer icon to perform a build.
Tip
A combination of Ctrl + B offers a mouse-free shortcut for build.