Creating and Using a Static Library |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
資料來源:http://community.borland.com/article/0,1410,22694,00.html 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.
-------------------------------------------------------------------------------- Add or View comments on this article Products:
Borland C Builder 4.0, Borland C Builder 5.x Platforms:
Windows 2000 1.0; Windows 95 1.00B; Windows 98 1.0; Windows NT 4.0, 4.0 SP5 網路志工聯盟----Visita網站http://www.vista.org.tw
---[ 發問前請先找找舊文章 ]---
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |