次の方法で共有


Working with Import Libraries and Export Files

You can use LIB with the /DEF option to create an import library and an export file. LINK uses the export file to build a program that contains exports (usually a dynamic-link library (DLL)), and it uses the import library to resolve references to those exports in other programs.

In most situations, you do not need to use LIB to create your import library. When you link a program (either an executable file or a DLL) that contains exports, LINK creates an import library that describes the exports. Later, when you link a program that references those exports, you specify the import library.

However, when a DLL exports to a program that it also imports from, whether directly or indirectly, you must use LIB to create one of the import libraries. When LIB creates an import library, it also creates an export file. You must use the export file when linking one of the DLLs.

Building an Import Library and Export File

To build an import library and export file, use the following command line syntax.

LIB /DEF[:deffile] [options] [objfiles] [libraries]

When /DEF is specified, LIB creates the output files from export specifications that are passed in the LIB command. There are three methods for specifying exports, listed in recommended order of use:

  1. A __declspec(dllexport) definition in one of the objfiles or libraries
  2. A specification of /EXPORT:name on the LIB command line
  3. A definition in an EXPORTS statement in a deffile

These are the same methods you use to specify exports when linking an exporting program. A program can use more than one method. You can specify parts of the LIB command (such as multiple objfiles or /EXPORT specifications) in a command file in the LIB command, just as you can in a LINK command.

The following options apply to building an import library and export file:

  • /DEBUGTYPE:CV
    Sets the format of debugging information. Specify CV for Microsoft Symbolic Debugging Information, required by eMbedded Visual C++.

  • /OUT:import
    Overrides the default output filename for the import library being created. When /OUT is not specified, the default name is the base name of the first object file or library in the LIB command and the extension .lib. The export file is given the same base name as the import library and the extension .exp.

  • /EXPORT:entryname[=internalname][,@ordinal[,NONAME]][,DATA]
    Exports a function from your program to allow other programs to call the function. You can also export data (using the DATA keyword). Exports are usually defined in a DLL.

    The entryname is the name of the function or data item as it is to be used by the calling program. Optionally, you can specify the internalname as the function known in the defining program; by default, internalname is the same as entryname. The ordinal specifies an index into the export table in the range 1 through 65,535; if you do not specify ordinal, LIB assigns one. The NONAME keyword exports the function only as an ordinal, without an entryname. The DATA keyword is used to export data-only objects.

  • /INCLUDE:symbol
    Adds the specified symbol to the symbol table. This option is useful for forcing the use of a library object that otherwise would not be included.

Using an Import Library and Export File

When a an executable file or a DLL exports to a program that it imports from, or if more than two programs export to and import from each other, the commands to link these programs must accommodate circular exports.

In a situation without circular exports, when you link a program that uses exports from another program, you must specify the import library for the exporting program. The import library for the exporting program is created when you link that exporting program. Therefore, you must link the exporting program before the importing program. For example, if Two.dll imports from One.dll, you must first link One.dll and get the import library One.lib. You then specify One.lib when you link Two.dll. When the linker creates Two.dll, it also creates its import library, Two.lib. You use Two.lib when linking programs that import from Two.dll.

However, in a circular export situation, it is not possible to link all of the interdependent programs using import libraries from the other programs. In the example discussed earlier, if Two.dll also exports to One.dll, the import library for Two.dll will not exist yet when One.dll is linked. When circular exports exist, you must use LIB to create an import library and export file for one of the programs.

To begin, choose one of the programs on which to run LIB. In the LIB command, list all objects and libraries for the program and specify /DEF. If the program uses a .def file or /EXPORT specifications, specify these as well.

After you create the import library (.lib) and the export file (.exp) for the program, use the import library when linking the other program or programs. LINK creates an import library for each exporting program it builds. For example, if you run LIB on the objects and exports for ONE.dll, you create One.lib and One.exp. You can now use One.lib when linking Two.dll; this step also creates the import library Two.lib.

Finally, link the program you began with. In the LINK command, specify the objects and libraries for the program, the .exp file that LIB created for the program, and the import library or libraries for the exports used by the program. To continue the example, the LINK command for One.dll contains One.exp and Two.lib, as well as the objects and libraries that go into ONE.dll. Do not specify the .def file or /EXPORT specifications in the LINK command; these are not needed, because the export definitions are contained in the .exp file. When you link using an .exp file, LINK does not create an import library, because it assumes that one was created when the .exp file was created.