[時間:2016-05] [狀態:Open]
引言
近期由於不再使用vs生成lib,考慮使用windows下gcc生成一個動態庫,供第三方調用,發現編譯之后只有dll,lib如何處理?
好吧,這就是本文的目的。
由dll導出lib
Visual C++ 開發工具提供了兩個命令行工具,一個是dumpbin.exe,另一個是lib.exe。利用這兩個工具即可從dll導出其對應的lib。
1、導出def文件
在命令行執行:
dumpbin /exports target.dll > target.def
2、編輯target.def 文件,使之格式與.def文件格式一致 。比如:
EXPORTS
fn @1
fn @2
3、導出lib文件
在命令行執行
lib /def:target.def /machine:i386 /out:target.lib
實踐
用下面的源碼驗證下,保存為export.c
/*
* compile with gcc 4.8.1
* gcc export.c -c -o test.exe
* gcc -Wall -shared export.c -o export.dll
*
*/
#include <stdlib.h>
const char * get_version_string()
{
return "tocy-gcc-dll-test-interface";
}
int select_mode(int mode)
{
return mode;
}
gcc編譯后生成export.dll,執行上面的第一步,輸出的def格式如下:
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file export.dll
File Type: DLL
Section contains the following exports for export.dll
00000000 characteristics
5732C473 time date stamp Wed May 11 13:34:43 2016
0.00 version
1 ordinal base
2 number of functions
2 number of names
ordinal hint RVA name
1 0 00001280 get_version_string
2 1 0000128A select_mode
Summary
1000 .CRT
1000 .bss
1000 .data
1000 .edata
1000 .eh_fram
1000 .idata
1000 .rdata
1000 .reloc
1000 .text
1000 .tls
將其修改為標准的def文件格式,如下:
LIBRARY export
EXPORTS
get_version_string @1
select_mode @2
執行第三步就可以導出lib。
參考資料
關於dll和so的區別
windows下動態庫和linux下動態庫的區別。
基本機制差不多,比較奇怪的是為什么linux下的動態庫*.so隱式鏈接的時候不需要符號表,而windows下的動態庫*.dll卻需要呢?
其實從文件格式上來說就知道,dll是PE格式(Portable Executable);而so是ELF格式(Executable and Linkable Format)。顧名思義,就是由於文件格式確定的。有興趣可以深入研究下二者的不同。
