小工具, 在使用第三方dll 的时候, 直接用这个python 脚本生成对应dll 的 lib
废话不多说, 直接上代码, 有需要的可以下载附件
# coding:utf-8
import os
import sys
import re
'''
原理:
1. 利用Vs 自带工具Dumobin.exe dump 出导出表 例:dumpbin /EXPORTS example.dll > example.def
2. 将dump 出的调出表修正为标准def 导出文件
3. 调用vs 工程下的lib 工具 链接def, 生成lib库 例:lib /def:example.def /machine:i386 /out:example.dll
'''
path = 'E:\CppProject\wxMSW-3.0.3_vc120_ReleaseDLL\lib\\vc120_dll\\' # 需要生成lib 的目录path, 默认使用当前路径
dllends = ".dll" # 用于过滤需生成lib 的文件后缀
tagPath = 'E:\CppProject\wxMSW-3.0.3_vc120_ReleaseDLL\lib\\vc120_dll\lib\\' # 指定生成lib 的目标路径
vsPath = r'D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\\' #vs 路径, lib.exe 和dumpbin.exe 在该路径下
dumpbinExe = 'dumpbin.exe'
libExe = 'lib.exe'
def travelDir():
list_dirs = os.walk(path)
dirlist = []
for root, dirs, files in list_dirs:
for f in files:
if f.endswith(dllends):
newp = os.path.join(root, f)
dirlist.append(newp)
return dirlist
def ModifyDef(path, libName):
file = open(path,"r+") # 注意r+ 可读可写方式打开文件, 而w则是truncate the file first , 先清空文件在写入文件
fc = file.read()
file.seek(0)
file.truncate() #清空文件
regPat = ' *(\d+) +([0-9a-fA-f]+) (0[0-9a-fA-f]+) (.+)\n'
arr = re.findall(regPat, fc)
file.write('LIBRARY {0}\n'.format(libName))
file.write('EXPORTS\n')
for list in arr:
strFunName = list[3]
if(len(strFunName) ==0) :
strFunName = 'NONAME{0}'.format(list[0])
file.write('{0} @{1} NONAME\n'.format(list[3], list[0])) # 不导出函数名称
continue
file.write('{0} @{1}\n'.format(list[3], list[0]))
file.close()
def generateLib(fDlldir):
fileName = os.path.basename(fDlldir)
dllName = fileName[0:-len(dllends)] # 删除文件名结尾
defFname = dllName + '.def'
defFullPath = tagPath + defFname
libFullPath = tagPath + dllName + '.lib'
#########################################
# dll -> def
os.chdir(vsPath)
cmd = r"{0} /exports {1} /out:{2} ".format(dumpbinExe, fDlldir, defFullPath)
print(cmd)
os.system(cmd)
ModifyDef(defFullPath, dllName)
#######################################
# def -> lib
# 值“IX64”无效,必须是“ARM, EBC, X64, or X86”;已忽略选项
platform = 'x86'
cmd = r"{0} /def:{1} /MACHINE:{2} /out:{3} ".format(libExe, defFullPath, platform, libFullPath)
print(cmd)
os.chdir(vsPath)
os.system(cmd)
#删除无用的exp 文件
expPath = tagPath + dllName + '.exp'
os.remove(expPath)
def main():
dirlist = travelDir()
for dir in dirlist:
generateLib(dir)
if __name__ == "__main__":
main()
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!