我译了一篇小短文,大虾们给看看译的对不对。

lovebcb 2003-08-21 10:34:48
从borland下载的cpp-ti中,编号为22694的短文,看了看不错,试着翻译了,头
一次译文,大虾们给看看译的对不对。不对和不合适的地方,一定要指出来啊!

bdn.borland.com

文章 #22694: C++Builder 基础: 在C++Builder中建立并使用静态库的简单教程

- C++Builder 基础 -
建立和使用静态库的简单教程


Part I: 简介

C++Builder提供了数百个类、函数和宏,在你的C和C++程序中,你可以调用它们来完成各种各样的工作。 这些类、函数和宏都是常规库。你不仅仅可以使用这些原有的库,而且还能够建立自己的库,并将其用在自己的程序里。这就为代码重用,以及将相关的源代码文件封装到单独的包中,提供了一个简单的途径。而且,考虑到,因为库基本上是一个二进制目标文件的集合,封装到库内的功能是抽象的,因此,库的使用者只能够看到功能的接口,而不能看到实际的源代码。

在C++Builder中,建立静态库是容易的、便利的。本文向你提供建立自己的静态库的基本步骤。

Part II. 准备源文件和头文件,建立静态库。

下面的代码示例是我们静态库例子的全部代码。我们的库主要有一个包含一个整型变量的类,这个整数变量可以用public的方法“getter”和“setter”来读写。全部定义可以在头文件中找到,在源文件里实现所有的功能。

A. 为你的库建立源文件和头文件。

运行记事本,将下面的代码粘贴进去,将文件保存为mylib.h(放入一个空目录)。这是头文件:

//----------------------------------------------
#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
//----------------------------------------------

现在,同样方法操作下面代码,但命名文件为mylib.cpp。这是我们的源文件。

//----------------------------------------------
#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. 使用库“向导”建立基本的库。

C++Builder提供了一个简单的方法来产生最基本的库。你需要做的只是使用库向导(the Library Wizard)建立库,然后将源文件加进去,然后编译。快捷的步骤如下:

1. File | Close All
2. File | New | Library
3. View | Project Manager
4. 在Project Manger里, 选中Project1.lib, 点击右键并选择Add...
5. 找到你保存文件的目录,选中“mylib.cpp”
6. File | Save All
7. 现在,你可以真正地编译这个库,即:Project | Build
8. 可以使用你的库了!


II. 使用你的静态库

如果你想在你的项目中使用它,请遵循以下步骤:

1. File | Close All
2. File | New Application
3. File | Save Project As... | 选择一个新目录(不是你的库所在的目录)
4. (从你的库文件目录中)复制“project1.lib”和“mylib.h”到你刚才保存项目的新目录。对于库的使用来说,这两个文件是必须的。
5. Project | Add to Project... | 改变文件类型为 .lib, 选择你的静态库 (现在,在这个项目的文件夹中)。
6. 在 Unit1.cpp 文件内, 你必须把头文件"mylib.h"包括进来,像这样:

#include "mylib.h"

7. 现在不要着急,想看到库实际的作用,还要把下面的代码放到窗体(form)的OnCreate事件中:

// Creates an object, A (from the library)
A test(50);
Form1->Caption = test.getValue();

8. 假如一切都做对的话,你会看到“50”出现在窗体的标题(caption)上。


最后更改日期: 08-AUG-00
...全文
23 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovebcb 2003-08-21
  • 打赏
  • 举报
回复
cker大哥也给鼓励了,呵呵,高兴!
  • 打赏
  • 举报
回复
翻的不错!鼓励!多多翻!
annkie 2003-08-21
  • 打赏
  • 举报
回复
鼓励一下
lovebcb 2003-08-21
  • 打赏
  • 举报
回复
的确,我自己觉得这里都怪怪的。
Format_CIH 2003-08-21
  • 打赏
  • 举报
回复
翻译的还可以,只是有点外国味,替你修改一下
C++Builder提供了数百个类、函数和宏,在C和C++程序中,你可以调用这些类、函数和宏来完成各种各样的工作。 这些类、函数和宏都是常规库。你不仅可以使用这些原有的库,还可以建立自己的库,并将其用在自己的程序里。
lovebcb 2003-08-21
  • 打赏
  • 举报
回复
原文如下:

bdn.borland.com

Article #22694: C++Builder Basics: Tutorial for Creating and Using a Static Library in C++Builder
- 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.


Last Modified: 08-AUG-00

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧