c 總結系列( ) 動態庫(dll)

2021-04-02 07:51:21 字數 1549 閱讀 9555

(-)動態鏈結庫與靜態鏈結庫的區別:

第一:靜態鏈結庫種的指令包含在可執行檔案中(exe),動態鏈結庫不會

第二:靜態鏈結庫中不能包含其他動態或靜態庫,動態鏈結庫可以。

其他主要區別:dll(動態鏈結庫)的編制與具體的程式語言以及編譯器無關,   visual                           basic,visial      c++,delphi都可以使用他

(二)關於靜態庫:靜態庫(lib)的呼叫需要標頭檔案(#include ".h")和新增庫檔案(.lib),新增庫檔案可以利用開發環境新增,也可以**新增:#pragma comment(lib,"lib//tlib.lib")

例如:/*lib.h*/

#ifndef lib_h

#define lib_h

extern "c" int __declspec(dllexport)add(int x,int y);

#endif

/*lib.cpp*/

#include "lib.h"

int add(int x,int y)

/*main.cpp*/

#include

#include "lib//add.h"

#pragma comment(lib,"lib//tlib.lib")

void main()

/*main.cpp*/

#include

#include

typedef int (*lpaddfun)(int,int);

void main()

bool apientry dllmain(handle hmodle, dword ul_reason_for_call, lpvoid lpreserved)

(八) dll中類的匯出

//檔名:point.h,point類的宣告

#ifndef point_h

#define point_h

#ifdef dll_file

class _declspec(dllexport) point //匯出類point

#else

class _declspec(dllimport) point //匯入類point

#endif

;#endif

//檔名:point.cpp,point類的實現

#ifndef dll_file

#define dll_file

#endif

#include "point.h"

//類point的預設建構函式

point::point()

//類point的建構函式

point::point(float x_coordinate, float y_coordinate)

/*main.cpp*/

#include "../circle.h"   //包含類宣告標頭檔案,注意沒有定義dll_file

#pragma comment(lib,"dlltest.lib");

int main(int argc, char *argv)

C 呼叫C 的動態庫dll

以往我們經常是需要使用c 來呼叫c 的dll,這通過pinvoke就能實現。現在在實際的專案過程中,有時會遇到在c 的專案中呼叫某個c 的dll來完成特定的某個功能,我們都知道,native c 是沒辦法直接呼叫.net平台的dll的。那有沒有辦法來做到這一點了?答案是肯定的。雖然,native c...

DLL系列之一 如何編寫DLL 動態鏈結庫

首先我們建立乙個標頭檔案 newdll.h ifdef cplusplus define export extern c declspec dllexport else define export declspec dllexport endif export bool callback edrce...

動態連線庫(DLL)

把c 的類寫進dll裡,單獨的編譯。程式執行時可以快速裝載並連線到dll上,這樣叫做標準的模組軟體。大大提高 的重用型。dll是執行時的模組。win32 dll允許匯出全域性變數,就象匯出函式一樣。只要dll申請記憶體,就從客戶程式的程序的記憶體堆中進行記憶體分配。dll包含乙個匯出函式表,它包含了...