用 vc 6.0 下的cl.exe 和 link.exe工具,請(qǐng)讀下文: 聲明:下面這篇文章不是我寫(xiě)的,源自:一個(gè)叫,有容乃大 的博客 如何手工編寫(xiě)動(dòng)態(tài)鏈接庫(kù)(windows dll)
1.本例介紹在命令行(Console)環(huán)境下制作dll的方法 2.讀者動(dòng)手前,請(qǐng)確保在windows中安裝有編譯、鏈接工具和必要的函數(shù)庫(kù)文件。 3.本例使用C語(yǔ)言實(shí)現(xiàn). 4.本例中使用路徑均為我機(jī)器上的絕對(duì)路徑,讀者需根據(jù)實(shí)際情況調(diào)整。
工具要求:
Microsoft的編譯器cl.exe MIcrosoft鏈接器link.exe
dll制作步驟: 1.編寫(xiě)dll函數(shù)實(shí)現(xiàn)源代碼hello.c
#include int say_hello(char* name) { printf( "hello %s\n ", name); return 1; }
2.編寫(xiě)dll函數(shù)輸出定義文件hello.def.
LIBRARY hello EXPORTS say_hello @1
3.編譯dll源碼,生成dll,lib文件.
3.1 新建命令行窗口 3.2 設(shè)置PATH | INCLUDE | LIB 3個(gè)環(huán)境變量.
SET PATH=K:\vcnet\vc7\bin;%PATH% SET INCLUDE=K:\vcnet\vc7\include;%INCLUDE% SET LIB=K:\vsnet\Vc7\lib;%LIB%
3.3 編譯hello.c
cd K:\Source\dllsample (hello.c和hello.def所在目錄) cl /c hello.c 3.4 鏈接hello.obj,生成hello.dll,hello.lib兩個(gè)文件.
link /def:hello.def /dll hello.obj
4.測(cè)試dll函數(shù).
4.1 編寫(xiě)測(cè)試代碼 test.c
extern int say_hello(char* name); int main(int argc,char** argv) { say_hello( "robbie "); return 0; }
4.2 編譯測(cè)試代碼test.c
cl /c test.c
4.3 鏈接test.obj和 hello.lib,生成可執(zhí)行文件test.exe
link test.obj hello.lib
4.4 運(yùn)行test.exe,屏幕輸出:
hello robbie
至此,一個(gè)dll構(gòu)造完畢.
下面是我自己的一點(diǎn)補(bǔ)充: 如果要在C++下,或者win32 MFC下使用標(biāo)準(zhǔn)c寫(xiě)的dll,必須把上面的聲明 extern int say_hello(char* name);改成:extern "C " int say_hello(char* name); 本文摘自: 編程十萬(wàn)個(gè)為什么(http://www.) 詳細(xì)出處請(qǐng)參考:http://www./thread-12994-11-1.html
|