Dar*_*con 67
dumpbin /exports sqlite3.dll > exports.txt
echo LIBRARY SQLITE3 > sqlite3.def
echo EXPORTS >> sqlite3.def
for /f "skip=19 tokens=4" %A in (exports.txt) do echo %A >> sqlite3.def
Run Code Online (Sandbox Code Playgroud)
图书管理员可以使用此DEF文件生成LIB:
lib /def:sqlite3.def /out:sqlite3.lib /machine:x86
Run Code Online (Sandbox Code Playgroud)
所有的文件名(的exports.txt,sqlite3.dll,sqlite3.def等)应具有完整路径预先考虑.
Con*_*man 14
我知道这个主题已经过时了,但我仍然无法在Internet上的任何地方找到脚本或批处理文件来执行此操作.所以根据Dark Falcon的回答,我制作了这个脚本,你可以保存为dll2lib.bat并运行:
REM Usage: dll2lib [32|64] some-file.dll
REM
REM Generates some-file.lib from some-file.dll, making an intermediate
REM some-file.def from the results of dumpbin /exports some-file.dll.
REM Currently must run without path on DLL.
REM (Fix by removing path when of lib_name for LIBRARY line below?)
REM
REM Requires 'dumpbin' and 'lib' in PATH - run from VS developer prompt.
REM
REM Script inspired by http://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dll
SETLOCAL
if "%1"=="32" (set machine=x86) else (set machine=x64)
set dll_file=%2
set dll_file_no_ext=%dll_file:~0,-4%
set exports_file=%dll_file_no_ext%-exports.txt
set def_file=%dll_file_no_ext%.def
set lib_file=%dll_file_no_ext%.lib
set lib_name=%dll_file_no_ext%
dumpbin /exports %dll_file% > %exports_file%
echo LIBRARY %lib_name% > %def_file%
echo EXPORTS >> %def_file%
for /f "skip=19 tokens=1,4" %%A in (%exports_file%) do if NOT "%%B" == "" (echo %%B @%%A >> %def_file%)
lib /def:%def_file% /out:%lib_file% /machine:%machine%
REM Clean up temporary intermediate files
del %exports_file% %def_file% %dll_file_no_ext%.exp
Run Code Online (Sandbox Code Playgroud)
我确信脚本可以使用改进,但我希望它有用.
此脚本从%1中传递的*.dll创建*.lib:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-4" %%1 in ('dumpbin /exports %1') do (
set /a ordinal=%%1 2>nul
set /a hint=0x%%2 2>nul
set /a rva=0x%%3 2>nul
if !ordinal! equ %%1 if !hint! equ 0x%%2 if !rva! equ 0x%%3 set exports=!exports! /export:%%4
)
for /f %%i in ("%1") do set dllpath=%%~dpni
start lib /out:%dllpath%.lib /machine:x86 /def: %exports%
Run Code Online (Sandbox Code Playgroud)
您可以将其命名为implib.bat并运行:implib.bat C:\folder\mydll.dll生成C:\ folder\mydll.lib
是否可以从 DLL 自动生成 MSVC 导入库(LIB 文件)?如何?
除了 Dark Falcon 的答案之外,Microsoft 还在How To Create Import Libraries Without .OBJs or Source中发布了过程。
微软的第一个程序与Dark Falcon的相同。第二个过程稍微麻烦一些,但它展示了如何使用存根对目标文件执行此操作。它适用于不同的调用约定和类。
以下是知识库中的第二个过程:
对于那些使用Linux并想为MinGW生成的.dll创建合适的导入库(.lib)的人,这里又涉及两个步骤:
使用MSVC,可以处理的输出dumpbin /exports foo.dll。在Linux上,您必须处理objdump -p(来自binutils)的输出。生成的模块定义文件应如下所示:
LIBRARY foo.dll
EXPORTS
your_symbol @1
another_symbol @2
; ... et cetera
Run Code Online (Sandbox Code Playgroud)
到DEF文件转换成的.lib文件,使用LLVM-dlltool(MinGW的(binutils的)dlltool是不适合的)。64位库的示例调用:
llvm-dlltool -m i386:x86-64 -d foo.def -l foo.lib
Run Code Online (Sandbox Code Playgroud)
说明:
-m i386:x86-64:生成一个64位库。使用-m i386,如果你需要一个32位的库,而不是。-d foo.def:从文件中读取导出的符号 foo.def-l foo.lib:将导入库写入 foo.lib.def文件不包含任何LIBRARY行,则必须附加该-D foo.dll选项(仅文件名,不带目录前缀)。这是一个自动化脚本:
# Given libxyz-1.dll, create import library libxyz-1.lib
make_implib() {
local machine=$1 dll="$2" dllname deffile libfile
dllname="${dll##*/}"
deffile="${dll%.dll}.def"
libfile="${dll%.dll}.lib"
# Extract exports from the .edata section, writing results to the .def file.
LC_ALL=C objdump -p "$dll" | awk -vdllname="$dllname" '
/^\[Ordinal\/Name Pointer\] Table$/ {
print "LIBRARY " dllname
print "EXPORTS"
p = 1; next
}
p && /^\t\[ *[0-9]+\] [a-zA-Z0-9_]+$/ {
gsub("\\[|\\]", "");
print " " $2 " @" $1;
++p; next
}
p > 1 && /^$/ { exit }
p { print "; unexpected objdump output:", $0; exit 1 }
END { if (p < 2) { print "; cannot find export data section"; exit 1 } }
' > "$deffile"
# Create .lib suitable for MSVC. Cannot use binutils dlltool as that creates
# an import library (like the one found in lib/*.dll.a) that results in
# broken executables. For example, assume executable foo.exe that uses fnA
# (from liba.dll) and fnB (from libb.dll). Using link.exe (14.00.24215.1)
# with these broken .lib files results in an import table that lists both
# fnA and fnB under both liba.dll and libb.dll. Use of llvm-dlltool creates
# the correct archive that uses Import Headers (like official MS tools).
llvm-dlltool -m "$machine" -d "$deffile" -l "$libfile"
rm -f "$deffile"
}
# Example invocations:
make_implib i386:x86_64 usr/x86_64-w64-mingw32/sys-root/mingw/bin/libgnutls-30.dll
make_implib i386 usr/i686-w64-mingw32/sys-root/mingw/bin/libgnutls-30.dll
Run Code Online (Sandbox Code Playgroud)
(请注意,如果库名的长度超过15个字符,则llvm-dlltool中存在一个错误,该错误会生成比所需的更大的.lib文件。除大小外,它仍可正常使用。此问题已通过https:// reviews修复。.llvm.org / D55860)
经过测试: