⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,22694,00.html

📁 C++builder学习资料C++builder
💻 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>&nbsp;  

<p>Part I:&nbsp; Short Introduction  

<br>&nbsp;  

<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.&nbsp;  

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.&nbsp; This provides an easy  

interface for code  

<br>reusability and allows you to consolidate related source code files  

into a single  

<br>package.&nbsp; 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>&nbsp;  

<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.&nbsp;  

All  

<br>declarations are found in the header file, and all of the implementation  

is kept  

<br>in the source file.  

<p>A.&nbsp; 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).&nbsp; This will be the header file:  

<p>//----------------------------------------------  

<br>#ifndef mylib_h  

<br>#define mylib_h  

<p>class A;  

<p>class A  

<br>&#123;  

<br>&nbsp;&nbsp; public:  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A();  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~A() &#123;&#125;;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A(int newa);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int getValue() const;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void setValue(int newa);  

<br>&nbsp;&nbsp; private:  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a;  

<br>&#125;;  

<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>&#123;  

<br>&#125;  

<p>int A::getValue() const  

<br>&#123;  

<br>&nbsp; return a;  

<br>&#125;  

<p>void A::setValue(int newa)  

<br>&#123;  

<br>&nbsp; a = newa;  

<br>&#125;  

<br>//----------------------------------------------  

<br>&nbsp;  

<p>B.&nbsp; Using the Library "wizard" to create the basic library.  

<p>Builder provides an easy way to generate a basic library.&nbsp; 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.&nbsp; Here are the quick steps to follow:  

<p>&nbsp; 1.&nbsp; File&nbsp; |&nbsp; Close All  

<br>&nbsp; 2.&nbsp; File&nbsp; |&nbsp; New&nbsp; |&nbsp; Library  

<br>&nbsp; 3.&nbsp; View&nbsp; |&nbsp; Project Manager  

<br>&nbsp; 4.&nbsp; In the Project Manger, select Project1.lib, right-click  

and choose Add...  

<br>&nbsp; 5.&nbsp; Navigate to the directory where you saved your files,  

and select "mylib.cpp"  

<br>&nbsp; 6.&nbsp; File&nbsp; |&nbsp; Save All  

<br>&nbsp; 7.&nbsp; Now you are ready to actually build the library, so:&nbsp;  

Project&nbsp; |&nbsp; Build  

<br>&nbsp; 8.&nbsp; Your library is all ready to be used!  

<br>&nbsp;  

<p>II. Using your static library  

<p>If you would like to use it in an application, please follow these steps:  

<p>&nbsp; 1.&nbsp; File&nbsp; |&nbsp; Close All  

<br>&nbsp; 2.&nbsp; File&nbsp; |&nbsp; New Application  

<br>&nbsp; 3.&nbsp; File&nbsp; |&nbsp; Save Project As...&nbsp; |&nbsp;  

&amp; choose a new directory (not where your  

<br>library is)  

<br>&nbsp; 4.&nbsp; Copy, "project1.lib" and "mylib.h" (from your library  

application folder)  

<br>into the new directory you just saved your project in.&nbsp; Both of  

these files are  

<br>necessary in order to use your library.  

<br>&nbsp; 5.&nbsp; Project&nbsp; |&nbsp; Add to Project...&nbsp; |&nbsp;  

change the file type to .lib, and select  

<br>your static library (now in the folder for this project).  

<br>&nbsp; 6. In your Unit1.cpp file, you must include the header file,  

"mylib.h," like so:  

<p>&nbsp;&nbsp;&nbsp; #include "mylib.h"  

<p>&nbsp; 7.&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Creates an object, A (from the library)  

<br>&nbsp;&nbsp;&nbsp; A test(50);  

<br>&nbsp;&nbsp;&nbsp; Form1->Caption = test.getValue();  

<p>&nbsp; 8.&nbsp; 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 + -