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的路徑,告訴程序調用的外部導入函數的地址,否則找不到函數,鏈接出錯。 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章