有些时候,别人提供dll文件,却没提供lib文件,而windows平台VC++编译器又需要lib文件来链接,不然编译根本无法通过,找别人比较麻烦的情况下,只能自己来生成lib文件。
lib命令说明:
lib命令说明:
usage: LIB [options] [files]
options:
/DEF[:filename]
/EXPORT:symbol
/EXTRACT:membername
/INCLUDE:symbol
/LIBPATH:dir
/LIST[:filename]
/MACHINE:{AM33|ARM|EBC|IA64|M32R|MIPS|MIPS16|MIPSFPU|MIPSFPU16|MIPSR41XX|SH3|SH3DSP|SH4|SH5|THUMB|X86}
/NAME:filename
/NODEFAULTLIB[:library]
/NOLOGO
/OUT:filename
/REMOVE:membername
/SUBSYSTEM:{CONSOLE|EFI_APPLICATION|EFI_BOOT_SERVICE_DRIVER|
EFI_ROM|EFI_RUNTIME_DRIVER|NATIVE|POSIX|WINDOWS|
WINDOWSCE}[,#[.##]]
/VERBOSE
/MACHINE参数必须选择正确的机器类型。
- 第一步: 切换到VC安装目录E:\Program Files\Microsoft Visual Studio 10.0\VC\bin\amd64
E:\Program Files\Microsoft Visual Studio 10.0\VC\bin\amd64>vcvars64.bat
Setting environment for using Microsoft Visual Studio 2010 x64 tools.
E:\Program Files\Microsoft Visual Studio 10.0\VC\bin\amd64>
C:\Users\Administrator\Desktop\test>dumpbin /EXPORTS comerr32.dll > comerr32.def
C:\Users\Administrator\Desktop\test>
切换dll所在目录,然后运行dumpbin命令,dumpbin是为了导出函数名称,运行完成之后,生成comerr32.def如下内容:
Microsoft (R) COFF/PE Dumper Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file comerr32.dll
File Type: DLL
Section contains the following exports for comerr32.dll
00000000 characteristics
4B5493F2 time date stamp Tue Jan 19 01:01:38 2010
0.00 version
1 ordinal base
7 number of functions
7 number of names
ordinal hint RVA name
1 0 000019F3 add_error_table
2 1 0000218C com_err
3 2 00001DB7 com_err_va
4 3 00001588 error_message
5 4 00001B09 remove_error_table
6 5 00002298 reset_com_err_hook
7 6 000021AD set_com_err_hook
Summary
1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
2000 .text
- 第二步: 修改comerr32.def,去掉不必要的,留下函数名称,内容如下:
LIBRARY
EXPORTS
add_error_table
com_err
com_err_va
error_message
remove_error_table
reset_com_err_hook
set_com_err_hook
- 第三步:生成对应平台的lib文件,x86,x64自己选择,主要看你自己的目标平台是什么。详情查看文章开头lib命令的参数
C:\Users\Administrator\Desktop\test>lib /def:comerr32.def /out:comerr32.lib /MAC
HINE:X86
Microsoft (R) Library Manager Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
正在创建库 comerr32.lib 和对象 comerr32.exp
C:\Users\Administrator\Desktop\test>
C:\Users\Administrator\Desktop\test>dir
驱动器 C 中的卷是 系统
卷的序列号是 6E93-F0CE
C:\Users\Administrator\Desktop\test 的目录
2019/12/03 11:43 <DIR> .
2019/12/03 11:43 <DIR> ..
2019/12/03 11:39 145 comerr32.def
2017/12/01 04:08 43,168 comerr32.dll
2019/12/03 11:42 1,428 comerr32.exp
2019/12/03 11:42 2,946 comerr32.lib
4 个文件 47,687 字节
2 个目录 36,776,378,368 可用字节
C:\Users\Administrator\Desktop\test>
生成完成之后,就可以利用lib文件链接了。