- C++Builder Basics -
Tutorial for Creating and Using a Static Library
Part I: Short Introduction
C++Builder provides several hundred classes, functions, and macros
that you call
from within your C and C++ programs to perform a wide variety of tasks.
These
classes, functions, and macros are collectively referred to as library
routines.
In addition to using the provided libraries, you may have the need
to create you
own libraries for use in your projects. This provides an easy
interface for code
reusability and allows you to consolidate related source code files
into a single
package. Also, consider that, because a library is a basically
a collection of
binary object files, the functionality encapsulated within the library
is
abstracted, such that users of the library are only able to view the
interfaces to
the functionality, but not the actual source code.
Conveniently, creating a static library is easy with C++Builder. This
document
provides the basic steps for building your own static libraries.
Part II. Building the static library from source and header files.
The following code samples represent the entirety of our static library
example.
Basically, our library will contain a class which has a integer data
member which
may be read or written using the public "getter" and "setter" functions.
All
declarations are found in the header file, and all of the implementation
is kept
in the source file.
A. Creation of your source and header files for the library.
Run Notepad, paste the following code into the editor, and then save
the file as
mylib.h (into an empty directory). This will be the header file:
//----------------------------------------------
#ifndef mylib_h
#define mylib_h
class A;
class A
{
public:
A();
~A() {};
A(int newa);
int getValue() const;
void setValue(int newa);
private:
int a;
};
#endif
//----------------------------------------------
Now, do the same thing for the following code, but name the file, "mylib.cpp."
This will be our source file.
//----------------------------------------------
#include "mylib.h"
class A;
A::A(int newa) : a(newa)
{
}
int A::getValue() const
{
return a;
}
void A::setValue(int newa)
{
a = newa;
}
//----------------------------------------------
B. Using the Library "wizard" to create the basic library.
Builder provides an easy way to generate a basic library. What
you will be doing
is creating a library using the Library Wizard, and then add the source
file to
it, and build. Here are the quick steps to follow:
1. File | Close All
2. File | New | Library
3. View | Project Manager
4. In the Project Manger, select Project1.lib, right-click
and choose Add...
5. Navigate to the directory where you saved your files,
and select "mylib.cpp"
6. File | Save All
7. Now you are ready to actually build the library, so:
Project | Build
8. Your library is all ready to be used!
II. Using your static library
If you would like to use it in an application, please follow these steps:
1. File | Close All
2. File | New Application
3. File | Save Project As... |
& choose a new directory (not where your
library is)
4. Copy, "project1.lib" and "mylib.h" (from your library
application folder)
into the new directory you just saved your project in. Both of
these files are
necessary in order to use your library.
5. Project | Add to Project... |
change the file type to .lib, and select
your static library (now in the folder for this project).
6. In your Unit1.cpp file, you must include the header file,
"mylib.h," like so:
#include "mylib.h"
7. Now, don't expect fireworks, but to see the library
actually work, place the
following code in the OnCreate Event of the form:
// Creates an object, A (from the library)
A test(50);
Form1->Caption = test.getValue();
8. If everything is as it should be, you should see a "50"
appear as the form's
caption.
Connect with Us