| < Day Day Up > |
|
OK. You have slaved at the computer and have developed a great function that you would like to reuse in other projects without having to recompile its source code. Or perhaps you have developed a set of functions that do something no one has ever seen done on a computer before and you would like to sell them and retire early! Whatever your motivation you will start by creating a function or code library.
A library is compiled code that can be used in other programs. All that’s needed to use the library is the library object code and a header file that provides the declarations for the functions in the library. Aren’t you lucky! You already know about header files and how to use them. And because you know how to create multiple file programs you already know how to do the hardest part of the library creation process.
The following is a quick overview to creating a function library: The first two steps you already know how to do from reading this chapter. Steps 3 through 8 apply to Metrowerks CodeWarrior but will be similar on other development environments. The final word on how to create a code library can be found in your IDE’s documentation. Here is the process at a glance:
Step 1: Put the function declarations for any functions you want in the library in a header file. (.h file)
Step 2: Put the definitions for the library functions in a separate implementation file. (.cpp file)
Step 3: Create an empty project in your Integrated Development Environment and add the implementation file to it.
Step 4: Add any library files to the project required to support the implementation file. For instance, if your function uses the iostream library then you need to add that library to your project.
Step 5: Set the required target settings for the project.
Step 6: Name the library output file and set project type.
Step 7: Compile the project.
Step 8: Use the library!
That’s pretty much it. Now, let us step through the process in more detail. For an example I will create a library using the dumbSort() function along with the two supporting functions compareAscending() and compareDescending(). These three functions will constitute the dumbSort library and after the library is created they can be used in other projects without having to add and recompile the dumbsort.cpp file.
Since you already know how to do the first two steps, creating separate header and implementation files, I will start with creating the empty project. Start your IDE and create a new empty project. An empty project is a project with no source or library files pre-added. Since you’re creating a library, all you will need is the implementation file for the code you want to turn into a library and any libraries your code depends on. Figure 9-17 shows a screen shot for creating an empty project in CodeWarrior:
Figure 9-17: Creating an Empty Project in CodeWarrior
Add to the empty project the implementation file containing the functions of interest. Figure 9-18 shows the DumbSortLibrary project with the dumbsort.cpp implementation file added.
Figure 9-18: dumpsort.cpp Added to the Empty Project
Now set the target setting for the library as shown in figure 9-19.
Figure 9-19: Setting Library Target Settings
Next, set the name of the library file and the project type. The name shown in 9-20 below is dumbsort.lib. Other names for the library could have also been used such as dumbsort.o, dumbsort.out, or dumbsortlib.lib. This name will be used to name the code module produced by the compiler when the project is built.
Figure 9-20: Selecting Project Type and Library Name
Build the project. The library file you named in the previous step will be generated. You will not be able to run this type of project, so, to test your library you will have to create a new project and write some code that uses the functions contained in the library. The following step shows this being done.
To use the library you will need the library header file and the library code module. You will use the header file as normal and add the library code module to the project as shown in figure 9-21.
Figure 9-21: Using the dumbsort Library
The contents of the main() file in this project is the same as that shown in example 9.54. The difference between the previous dumbsort project and this project is that what was once the dumbsort.cpp file has been transformed into a code library.
| < Day Day Up > |
|