home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
CPPDLL.DOC
< prev
next >
Wrap
Text File
|
1993-05-17
|
6KB
|
168 lines
C++ DLL Building
INTRODUCTION
Building Dynamic Link libraries is not a difficult task, but
it does require the user to faithfully follow certain rules. The C/C++
Tools Programming Guide describes this topic - Chapter 12. Building
Dynamic Link Libraries - very well.
This document describes the steps for creating
and using C++ dynamic link libraries through various simple
examples.
You should already be familiar with the information
presented in Chapter 12 of the IBM C/C++ Tools Programming Guide.
If not, please take some time to review the chapter and
have it handy for reference.
You can use two methods to build C++ DLL's:
Method 1: the use of features _Export and #pragma export
Method 2: the use of .DEF building tool CPPFILT.EXE
We will show how these two methods actually work.
METHOD 1: Using _Export and #pragma export
This is the simplest method to create a C++ DLL.
It involves the use of the _Export keyword or the
#pragma export directive on the functions or classes you
wish to export. For example, to export the function
"int chris(int a)", simply insert the keyword as follow:
"int _Export chris(int a)".
The files for a sample program that demonstrates this method are
located in the directory IBMCPP\SAMPLES\COMPILER\SAMPLE07\METHOD1.
The command file run.cmd, demonstrates each of the steps
given below:
(1) Use _Export or #pragma export in your source files to
specify the items you want to export.
The sample source file VF.H, shows how the
_Export keyword is used to export a class.
For example, if you wish to export only a certain
member of a class, say getarea(), add the _Export
keyword to the declarator of the function:
class triangle : public area // deleted _Export
{
public:
static int objectCount;
double _Export getarea(); // added _Export
_Export triangle::triangle(void);
}; // you must always export constructors and destructors
You may also use #pragma export as follow:
#pragma export(triangle::getarea(),,1)
(2) Compile all files to objects (make sure you use the /Ge- option).
(3) Create a dummy .DEF file.
BASIC.DEF is a sample dummy .def file.
Note that the internal name of the DLL
is specified after the LIBRARY keyword
(4) Link the DLL with your objects, libraries, and DEF file.
You should use ICC to link the DLL. The /Fe option
specifies the file name of your DLL.
(5) Create the import library from the DLL.
(6) Link your application with the import library.
Use ICC to compile/link your application to the
import library.
Method 2: Using of .DEF file building tool CPPFILT.EXE
This method requires the use of CPPFILT tool to create
the export names list to be used in the .DEF file.
The advantage of this method is that it offers you
a convenient way to build DLL's without having to modify
the source files.
The files for a sample program that demonstrates this method are
located in the directory IBMCPP\SAMPLES\COMPILER\SAMPLE07\METHOD2.
Take a look at the command file run.cmd, which demonstrates
each of the steps given below:
(1) Compile all files to objects (make sure you use the /Ge- option).
(2) Compile all the tempinc files in the TEMPINC directory.
The tempinc files contain the implementation of
all the templates we have instantiated and used
by the objects you will link to create a DLL.
It is necessary to include these tempinc object
files when linking the DLL. Otherwise, unresolved
externals (missing implementation) will result.
(3) Copy the tempinc objects into your DLL building directory.
Since you want to link your DLL with the tempinc object files,
it is convenient to have all the objects in the same
directory.
(4) Run cppfilt.exe on all the object files using -b -p options,
directing the output to a temporary file. (In the example,
STK.TMP is the temporary file created).
-b option tells CPPFILT that we are using objects and
libraries
-p causes CPPFILT to dump out all public symbols that
can be exported
(5) Edit the temporary file (STK.TMP) created above
to exclude any symbols that you may not want to export and
add in the standard DEF file information.
If there are functions or variables that you do not
want to export from the DLL, simply comment the
symbols out.
You must add in the standard .DEF file header.
You should change the DLL name after the LIBRARY keyword
to the name you have given to the DLL you are building.
In the example, STACK.DEF is created by adding in the
standard DEF file information to STK.TMP.
(6) Link the DLL with your objects, libraries, and DEF file.
You should use ICC to link the DLL. The /Fe option
is used to specify the file name of your DLL.
(7) Create the import library from the DLL.
You can also use the .DEF file to create the import
library, for example, implib stack.lib stack.def
(8) Link your application with the import library.
In the sample program, the tempinc object
files included in the DLL have been deleted. If you forget
to delete those tempinc object files, you most likely
will get error messages from the linker saying
one or more symbols are multiply defined.
=== End of document ===