用作链接器输入的 .Lib 文件.Lib Files as Linker Input

LINK 接受 COFF 标准库和 COFF 导入库,这两者通常都具有扩展名 .lib。LINK accepts COFF standard libraries and COFF import libraries, both of which usually have the extension .lib. 标准库包含对象,由 LIB 工具创建。Standard libraries contain objects and are created by the LIB tool. 导入库包含有关其他程序中的导出的信息,并且在生成包含导出或 LIB 工具的程序时通过链接创建。Import libraries contain information about exports in other programs and are created either by LINK when it builds a program that contains exports or by the LIB tool. 有关使用 LIB 创建标准或导入库的信息,请参阅 LIB ReferenceFor information on using LIB to create standard or import libraries, see LIB Reference. 有关使用 LINK 创建导入库的详细信息,请参阅 /DLL 选项。For details on using LINK to create an import library, see the /DLL option.

指定库以链接为文件名参数或默认库。A library is specified to LINK as either a file name argument or a default library. LINK 解析外部引用,方法是在命令行中指定的库中首先搜索,然后在使用 /DEFAULTLIB 选项指定的默认库中搜索,然后在 .obj 文件中命名的默认库中进行搜索。LINK resolves external references by searching first in libraries specified on the command line, then in default libraries specified with the /DEFAULTLIB option, and then in default libraries named in .obj files. 如果使用库名称指定路径,则会在该目录中查找库。If a path is specified with the library name, LINK looks for the library in that directory. 如果未指定路径,则 LINK 首先在运行链接的目录中查找,然后在 LIB 环境变量中指定的任何目录中查找。If no path is specified, LINK looks first in the directory that LINK is running from, and then in any directories specified in the LIB environment variable.

添加用作开发环境中的链接器输入的 .lib 文件To add .lib files as linker input in the development environment

  1. 打开项目的“属性页” 对话框。Open the project's Property Pages dialog box. 有关详细信息,请参阅在 Visual Studio 中设置 C++ 编译器和生成属性For details, see Set C++ compiler and build properties in Visual Studio.

  2. 选择 "链接器" 文件夹中的 "输入" 属性页。Choose the Input property page in the Linker folder.

  3. 修改 " 附加依赖项 " 属性以添加 .lib 文件。Modify the Additional Dependencies property to add the .lib files.

以编程方式添加用作链接器输入的 .lib 文件To programmatically add .lib files as linker input

示例Example

下面的示例演示如何生成和使用 .lib 文件。The following sample shows how to build and use a .lib file. 首先,生成 .lib 文件:First, build a .lib file:

// lib_link_input_1.cpp
// compile by using: cl /LD lib_link_input_1.cpp
__declspec(dllexport) int Test() {
   return 213;
}

然后,使用刚刚创建的 .lib 文件来编译此示例:And then, compile this sample by using the .lib file you just created:

// lib_link_input_2.cpp
// compile by using: cl /EHsc lib_link_input_1.lib lib_link_input_2.cpp
__declspec(dllimport) int Test();
#include <iostream>
int main() {
   std::cout << Test() << std::endl;
}
213

请参阅See also

链接输入文件LINK Input Files
MSVC 链接器选项MSVC Linker Options