1. 程式人生 > >vs2010 C++建立和使用動態連結庫(dll)

vs2010 C++建立和使用動態連結庫(dll)

一、用C++建立動態連結庫專案: 
1
、開啟Microsoft Visual Studio 2010,選擇File->New->Project。 
2
、在NewProject中選擇Installed Templates->Visual C++->Win32。 
3
、選擇Win32 Console Application,設定名稱:simpledll,設定解決方案名:zdddll。 
4
、單擊OK,在出現的Win32 Application Wizard的Overview對話方塊中點選Next。 
5
、在ApplicationSettings中,選擇Application type下的DLL。 
6

、勾選Additional options下的Emptyproject。 
7
、單擊Finish建立專案。 向動態連結庫新增類: 
1
、新增新類標頭檔案。右鍵單擊simpledll專案,Add->New Item,選擇Header File(.h),設定名稱為simpledll,單擊Add。 
2
、新增新類原始檔。右鍵單擊simpledll專案,Add->New Item,選擇C++ File(.cpp),設定名稱為simpledll,單擊Add。 
3
、為新類新增內容。內容如下: 標頭檔案simpledll.h: 

<span style="font-size:14px;">//------------------ simpledll.h ---------------- 
#pragma once; //編譯器相關的,保證標頭檔案只被編譯一次。

//該巨集完成在dll專案內部使用_declspec(dllexport)匯出 
//在dll專案外部使用時,用_declspec(dllimport)匯入 
//巨集DLL_IMPLEMENT在simpledll.cpp中定義 
#ifdef DLL_IMPLEMENT 
#define DLL_API _declspec(dllexport) 
#else 
#define DLL_API _declspec(dllimport) 
#endif 

namespace zdd 
{ 

//匯出類 
class DLL_API SimpleDll 
{ 
public: 
SimpleDll(); 
~SimpleDll(); 
int add(int x, int y); //簡單方法 
}; 
}</span>

原始檔simpledll.cpp: 

<span style="font-size:14px;">//------------------ simpledll.cpp---------------- 
//注意此處的巨集定義需要寫在#include "simpledll.h"之前 
//以完成在dll專案內部使用__declspec(dllexport)匯出 
//在dll專案外部使用時,用__declspec(dllimport)匯入 
#define DLL_IMPLEMENT 
#include "simpledll.h" 
namespace zdd 
{ 
SimpleDll::SimpleDll() 
{ 
} 
SimpleDll::~SimpleDll() 
{ 
} 
int SimpleDll::add(int x, int y) 
{ 
return x+y; 
} 

} </span>
4、完成後點選Build->Build Solution,生成解決方案。可在~zdddll\Debug下檢視生成的simpledll.libsimpledll.dll.檔案。 二、建立引用動態連結庫的C++應用程式: 
1
、選擇File->New->Project。 
2
、在NewProject中選擇Installed Templates->Visual C++->Win32。 
3
、選擇Win32 Console Application,設定名稱:usesimpledll。選擇Add to solution。 
4
、單擊OK,在出現的Win32 Application Wizard的Overview對話方塊中點選Next。 
5
、在ApplicationSettings中,選擇Application type下的Consoleapplication。 
6
、取消Additional options下的Precompiledheader,勾選Empty project。 
7
、單擊Finish建立專案。 在控制檯應用程式中使用類庫的功能: 
1
、為控制檯應用程式新增main.cpp。右鍵單擊usesimpledll專案,Add->New Item,選擇C++ File(.cpp),設定名稱為main,單擊Add。 
2
、為main.cpp新增內容。如下所示: 
<span style="font-size:14px;">//------------------ main.cpp ------------------- 
#include "..\include\simpledll.h"  //將生成的simpledll.h放於新建的include目錄下
using namespace zdd; 
#include <iostream> 
using namespace std; 

int main(char argc, char **argv) 
{ 
cout << "----------------------"<<endl; 
SimpleDll sd; 
cout << "sd.add: 3+5=" <<sd.add(3, 5)<<endl; 

SimpleDll *psd = new SimpleDll; 
cout << "psd->add: 5+5="<< psd->add(5, 5)<<endl; 

cout << "----------------------"<<endl; 
cout << "please press Enterexit."<<endl; 
getchar(); 
return 0; 
} </span>
3.在工程目錄下建立Include目錄,將動態連結庫的那個標頭檔案拷入。建立lib目錄,將生成的那個.lib檔案拷入。然後將生成的.dll檔案拷入生成.exe檔案的那個目錄(一般是專案下的Debug下,點生成解決方案才會生成次目錄)。 

4.程式中要包含那個標頭檔案,注意路徑要寫正確。Include “..\Include\simpledll.h”,或者右擊工程,property,Configuration Properties,c/c++,General,在AdditionalInclude Directories中加入“;..\Include”,這樣包含標頭檔案時直接寫標頭檔案名,不需要考慮路徑,因為當在工程目錄下找不到檔案時,就會從新增的那個目錄查詢檔案。 

5.新增.lib檔案 
右擊工程,property,Configuration Properties,Linker,Input,在AdditionalDependencies中編輯,新增.lib路徑(一般是..\lib\xxxxx.lib)。 


另外,lib引用有兩種方法: 
1.#pragma comment(lib,”opengl32.lib”) 
2.選擇project–> XX properties… –> linker –> Input –> Additional dependences,在其中加入lib檔名即可。 

總結: 
首先建立生成DLL的工程,生成.dll,.lib檔案。需要用到的還有.h檔案。 
建立應用DLL的工程。要包含標頭檔案,把3個檔案拷入相應的目錄。 
在附加依賴項Additional Dependencies中新增.lib的路徑,告訴程式呼叫的外部匯入函式的地址,否則找不到函式,連結出錯。