1.什么是靜態(tài)連接庫,什么是動態(tài)鏈接庫
靜態(tài)鏈接庫與動態(tài)鏈接庫都是共享代碼的方式,如果采用靜態(tài)鏈接庫,則無論你愿不愿意,lib 中的指令都全部被直接包含在最終生成的 EXE 文件中了。但是若使用 DLL,該 DLL 不必被包含在最終 EXE 文件中,EXE 文件執(zhí)行時可以“動態(tài)”地引用和卸載這個與 EXE 獨立的 DLL 文件。靜態(tài)鏈接庫和動態(tài)鏈接庫的另外一個區(qū)別在于靜態(tài)鏈接庫中不能再包含其他的動態(tài)鏈接庫或者靜態(tài)庫,而在動態(tài)鏈接庫中還可以再包含其他的動態(tài)或靜態(tài)鏈接 庫。靜態(tài)鏈接庫與靜態(tài)鏈接庫調(diào)用規(guī)則總體比較如下。
對于靜態(tài)鏈接庫(比較簡單):
首先,靜態(tài)鏈接庫的使用需要庫的開發(fā)者提供生成庫的.h頭文件和.lib文件。
生成庫的.h頭文件中的聲明格式如下:
extern "C" 函數(shù)返回類型 函數(shù)名(參數(shù)表);
在調(diào)用程序的.cpp源代碼文件中如下:
#include "..\lib.h"
#pragma comment(lib,"..\\debug\\libTest.lib")
//指定與靜態(tài)庫一起鏈接
第二,因為靜態(tài)鏈接庫是將全部指令都包含入調(diào)用程序生成的EXE文件中。因此如果用的是靜態(tài)鏈接庫,那么也就不存在“導(dǎo)出某個函數(shù)提供給用戶使用”的情況,要想用就得全要!要不就都別要!:)
對于動態(tài)鏈接庫:
動態(tài)鏈接庫的使用需要庫的開發(fā)者提供生成的.lib文件和.dll文件?;蛘咧惶峁ヾll文件。
首先我們必須先注意到DLL內(nèi)的函數(shù)分為兩種:
(1)DLL 導(dǎo)出函數(shù),可供應(yīng)用程序調(diào)用;
(2)DLL 內(nèi)部函數(shù),只能在 DLL 程序使用,應(yīng)用程序無法調(diào)用它們。
因此調(diào)用程序若想調(diào)用DLL中的某個函數(shù)就要以某種形式或方式指明它到底想調(diào)用哪一個函數(shù)。
2.示例
示例之一:
靜態(tài)鏈接庫的創(chuàng)建過程:
例如:我們創(chuàng)建一個自定義字符串的類CHironString,
只需要在IDE里面添加class即可,然后program相應(yīng)函數(shù)體
代碼如下所示:
SDLL.h文件
------------------------------------------------------------------------
// HironString.h: interface for the CHironString class.
//
////////////////////////////////////////////////////////////////////// #if !defined(AFX_HIRONSTRING_H__B23C5E5E_0E8B_4030_B057_34A40C934C59__INCLUDED_)
#define AFX_HIRONSTRING_H__B23C5E5E_0E8B_4030_B057_34A40C934C59__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CHironString
{
private:
char* m_data;
public:
char * GetData();
CHironString(CHironString &other);
int Length();
CHironString();
CHironString(char * str);
CHironString& operator=(CHironString &other);
virtual ~CHironString();
};
#endif // !defined(AFX_HIRONSTRING_H__B23C5E5E_0E8B_4030_B057_34A40C934C59__INCLUDED_)
SDLL.CPP如下:
--------------------------------------------------------------
// HironString.cpp: implementation of the CHironString class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HironString.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHironString::CHironString()
{
m_data=NULL;
}
CHironString::CHironString(char * str)
{
int len=strlen(str);
m_data=new char[len+1];
strcpy(m_data,str);
}
CHironString::~CHironString()
{
delete m_data;
}
int CHironString::Length()
{
return strlen(m_data);
}
CHironString::CHironString(CHironString &other)
{
int len=strlen(other.m_data)+1;
m_data=new char[len];
strcpy(m_data,other.m_data);
}
CHironString& CHironString::operator =(CHironString &other)
{
if(this==&other)
return *this;
if(m_data!=NULL)
delete[] m_data;
int len=strlen(other.m_data)+1;
m_data=new char[len];
strcpy(m_data,other.m_data);
return *this;
}
char * CHironString::GetData()
{
return m_data;
}
然后,將程序編譯后生成sdll.lib。
客戶調(diào)用:將CHironString.h和SDLL.lib發(fā)布給client,那么客戶端就可以調(diào)用我們編寫的靜態(tài)鏈接庫了。
示例之二:
動態(tài)鏈接庫的創(chuàng)建
我們還是創(chuàng)建一個自定義的字符串處理類CHironString,不同之處其是一個動態(tài)鏈接庫Dll。
動態(tài)鏈接庫的export 需要在在相應(yīng)的頭文件中編寫相應(yīng)的MACRO
MyDll.h:自定義了一些類(函數(shù))export 宏(該文件由IDE自動生成)如下
------------------------------------------------------------------
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
這是導(dǎo)出類的宏定義,將導(dǎo)出類必須加上該宏,才能被導(dǎo)出。
此處的MYDLL_EXPORTS會出現(xiàn)在 project-->settings-->C/C++頁面上的 PreProcessor definition中,這個MACRO表明其要定義一個導(dǎo)出宏
CHironString.h 自定義類頭文件
----------------------------------------------------------------
// HironString.h: interface for the CHironString class.
//
////////////////////////////////////////////////////////////////////// CHironString.Cpp
------------------------------------------------------------
// HironString.cpp: implementation of the CHironString class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HironString.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHironString::CHironString()
{
m_data=NULL;
}
CHironString::CHironString(char * str)
{
int len=strlen(str);
m_data=new char[len+1];
strcpy(m_data,str);
}
CHironString::~CHironString()
{
delete m_data;
}
int CHironString::Length()
{
return strlen(m_data);
}
CHironString::CHironString(CHironString &other)
{
int len=strlen(other.m_data)+1;
m_data=new char[len];
strcpy(m_data,other.m_data);
}
CHironString& CHironString::operator =(CHironString &other)
{
if(this==&other)
return *this;
if(m_data!=NULL)
delete[] m_data;
int len=strlen(other.m_data)+1;
m_data=new char[len];
strcpy(m_data,other.m_data);
return *this;
}
char * CHironString::GetData()
{
return m_data;
}
經(jīng)過compile之后,會生成MyDll.dll和MyDll.lib文件。
客戶端的調(diào)用:
將MyDll.dll和MyDll.lib和CHironString交由client即可。
notes:
如果要調(diào)用Dll中的function,需要經(jīng)歷3個步驟:
Handle h=LoadLibrary(dllName) --> GetProcAddress(h,functionName) 返回函數(shù)指針,通過函指針調(diào)用其function-->FreeLibrary(h)
例如:Another.dll有一個int Add(int x,int y)函數(shù)。則完整的調(diào)用過程如下:
typedef int (* FunPtr)(int,int);//定義函數(shù)指針
FunPtr funPtr;
Handle h=LoadLibrary("Another.dll");
funPtr=(FunPtr)GetProcAddress(h,"Add");
funPtr(2,3);//2+3;
FreeLibrary(h);
#if !defined(AFX_HIRONSTRING_H__518E9EC4_0837_4E45_9516_7D6A70CD3D0F__INCLUDED_)
#define AFX_HIRONSTRING_H__518E9EC4_0837_4E45_9516_7D6A70CD3D0F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "MyDll.h"
class MYDLL_API CHironString //加上MYDLL_API表明此為Export Class
{
private:
char* m_data;
public:
char * GetData();
CHironString(CHironString &other);
int Length();
CHironString();
CHironString(char * str);
CHironString& operator=(CHironString &other);
virtual ~CHironString();
};
#endif // !defined(AFX_HIRONSTRING_H__518E9EC4_0837_4E45_9516_7D6A70CD3D0F__INCLUDED_)