📄 0,1410,22694,00.html
字号:
<HTML>
<HEAD>
<TITLE>C++Builder Basics: Tutorial for Creating and Using a Static Library in C++Builder</TITLE>
</HEAD>
<BODY MARGINWIDTH="5" MARGINHEIGHT="5" TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF">
<A NAME="top"></A>
<table>
<TR>
<td>
<SPAN CLASS="title3"><b>C++Builder Basics: Tutorial for Creating and Using a Static Library in C++Builder</b></SPAN>
<P>
<BLOCKQUOTE CLASS="abstract"><B>Abstract:</B>Provides source code and a step-by-step procedure for creating and using a static library</BLOCKQUOTE><P>
</table>
- C++Builder Basics -
<br>Tutorial for Creating and Using a Static Library
<br>
<p>Part I: Short Introduction
<br>
<br>C++Builder provides several hundred classes, functions, and macros
that you call
<br>from within your C and C++ programs to perform a wide variety of tasks.
These
<br>classes, functions, and macros are collectively referred to as library
routines.
<br>In addition to using the provided libraries, you may have the need
to create you
<br>own libraries for use in your projects. This provides an easy
interface for code
<br>reusability and allows you to consolidate related source code files
into a single
<br>package. Also, consider that, because a library is a basically
a collection of
<br>binary object files, the functionality encapsulated within the library
is
<br>abstracted, such that users of the library are only able to view the
interfaces to
<br>the functionality, but not the actual source code.
<p>Conveniently, creating a static library is easy with C++Builder. This
document
<br>provides the basic steps for building your own static libraries.
<br>
<p>Part II. Building the static library from source and header files.
<p>The following code samples represent the entirety of our static library
example.
<br>Basically, our library will contain a class which has a integer data
member which
<br>may be read or written using the public "getter" and "setter" functions.
All
<br>declarations are found in the header file, and all of the implementation
is kept
<br>in the source file.
<p>A. Creation of your source and header files for the library.
<p>Run Notepad, paste the following code into the editor, and then save
the file as
<br>mylib.h (into an empty directory). This will be the header file:
<p>//----------------------------------------------
<br>#ifndef mylib_h
<br>#define mylib_h
<p>class A;
<p>class A
<br>{
<br> public:
<br> A();
<br> ~A() {};
<br> A(int newa);
<br> int getValue() const;
<br> void setValue(int newa);
<br> private:
<br> int a;
<br>};
<p>#endif
<br>//----------------------------------------------
<p>Now, do the same thing for the following code, but name the file, "mylib.cpp."
<br>This will be our source file.
<p>//----------------------------------------------
<br>#include "mylib.h"
<p>class A;
<p>A::A(int newa) : a(newa)
<br>{
<br>}
<p>int A::getValue() const
<br>{
<br> return a;
<br>}
<p>void A::setValue(int newa)
<br>{
<br> a = newa;
<br>}
<br>//----------------------------------------------
<br>
<p>B. Using the Library "wizard" to create the basic library.
<p>Builder provides an easy way to generate a basic library. What
you will be doing
<br>is creating a library using the Library Wizard, and then add the source
file to
<br>it, and build. Here are the quick steps to follow:
<p> 1. File | Close All
<br> 2. File | New | Library
<br> 3. View | Project Manager
<br> 4. In the Project Manger, select Project1.lib, right-click
and choose Add...
<br> 5. Navigate to the directory where you saved your files,
and select "mylib.cpp"
<br> 6. File | Save All
<br> 7. Now you are ready to actually build the library, so:
Project | Build
<br> 8. Your library is all ready to be used!
<br>
<p>II. Using your static library
<p>If you would like to use it in an application, please follow these steps:
<p> 1. File | Close All
<br> 2. File | New Application
<br> 3. File | Save Project As... |
& choose a new directory (not where your
<br>library is)
<br> 4. Copy, "project1.lib" and "mylib.h" (from your library
application folder)
<br>into the new directory you just saved your project in. Both of
these files are
<br>necessary in order to use your library.
<br> 5. Project | Add to Project... |
change the file type to .lib, and select
<br>your static library (now in the folder for this project).
<br> 6. In your Unit1.cpp file, you must include the header file,
"mylib.h," like so:
<p> #include "mylib.h"
<p> 7. Now, don't expect fireworks, but to see the library
actually work, place the
<br>following code in the OnCreate Event of the form:
<p> // Creates an object, A (from the library)
<br> A test(50);
<br> Form1->Caption = test.getValue();
<p> 8. If everything is as it should be, you should see a "50"
appear as the form's
<p>caption.
</body>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -