静态连接库、动态链接库
網(wǎng)上看到的不錯(cuò)的總結(jié),稍加整理,收藏下,方便今后回顧,
1、區(qū)別
(1)靜態(tài)鏈接庫與動(dòng)態(tài)鏈接庫都是共享代碼的方式,如果采用靜態(tài)鏈接庫,則無論你是否愿意,lib中的指令都會(huì)全部包含在最終生成的exe文件中,最終exe執(zhí)行時(shí)不再需要此lib。若使用動(dòng)態(tài)鏈接庫dll,該dll不必被包含在最終生成的exe文件中,exe文件執(zhí)行時(shí)可以“動(dòng)態(tài)”地引用和卸載這個(gè)與exe獨(dú)立的dll文件。
(2)靜態(tài)鏈接庫中不能在包含其他的動(dòng)態(tài)或者靜態(tài)鏈接庫,而動(dòng)態(tài)鏈接庫中還可以再包含其他的動(dòng)態(tài)或者靜態(tài)鏈接庫。
(3)靜態(tài)鏈接庫的使用,需要庫開發(fā)者提供生成庫的.h文件和.lib文件;動(dòng)態(tài)鏈接庫需要庫開發(fā)者提供.h文件、.lib文件、.dll文件(靜態(tài)加載),或只提供.dll文件(動(dòng)態(tài)加載)。
2、加載方式
靜態(tài)鏈接庫:
在調(diào)用程序的.cpp源代碼文件中如下:
#include "..\lib.h"
#pragma comment(lib,"..\\debug\\libTest.lib")
//指定與靜態(tài)庫一起鏈接
動(dòng)態(tài)鏈接庫:
->靜態(tài)加載:(需要提供.h、.lib、.dll文件)
在使用的cpp中指定.h 和.lib的路徑(或者在vc6.0的Project->Settings...中配置),.dll放到exe同目錄下。
#include "..\lib.h"
#pragma comment(lib,"..\\debug\\libTest.lib")
->動(dòng)態(tài)加載:(只需要提供.dll文件)
因此調(diào)用程序若想調(diào)用DLL中的某個(gè)函數(shù)就要以某種形式或方式指明它到底想調(diào)用哪一個(gè)函數(shù)。但是無法調(diào)用Class method了。
如果要調(diào)用Dll中的function,需要經(jīng)歷3個(gè)步驟:
Handle h=LoadLibrary(dllName) --> GetProcAddress(h,functionName) 返回函數(shù)指針,通過函指針調(diào)用其function-->FreeLibrary(h)
例如:Another.dll有一個(gè)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);
3、創(chuàng)建方式 (參考網(wǎng)上的)
示例之一:
靜態(tài)鏈接庫的創(chuàng)建過程:
? ? ? ? ? ? ? ? ? ? ? ?
例如:我們創(chuàng)建一個(gè)自定義字符串的類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)鏈接庫了。
示例之二:
動(dòng)態(tài)鏈接庫的創(chuàng)建
首先我們必須先注意到DLL內(nèi)的函數(shù)分為兩種:
(1)DLL 導(dǎo)出函數(shù),可供應(yīng)用程序調(diào)用;
(2)DLL 內(nèi)部函數(shù),只能在 DLL 程序使用,應(yīng)用程序無法調(diào)用它們。
我們還是創(chuàng)建一個(gè)自定義的字符串處理類CHironString,不同之處其是一個(gè)動(dòng)態(tài)鏈接庫Dll。
動(dòng)態(tài)鏈接庫的export 需要在在相應(yīng)的頭文件中編寫相應(yīng)的MACRO
MyDll.h:自定義了一些類(函數(shù))export 宏(該文件由IDE自動(dòng)生成)如下
------------------------------------------------------------------
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
這是導(dǎo)出類的宏定義,將導(dǎo)出類必須加上該宏,才能被導(dǎo)出。
此處的MYDLL_EXPORTS會(huì)出現(xiàn)在 project-->settings-->C/C++頁面上的 PreProcessor definition中,這個(gè)MACRO表明其要定義一個(gè)導(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;
}
2.如果是動(dòng)態(tài)加載,只需要提供*.dll即可
經(jīng)過compile之后,會(huì)生成MyDll.dll和MyDll.lib文件。
客戶端的調(diào)用:
1.如果是靜態(tài)加載,那么需要提供*.lib和*.h,運(yùn)行時(shí)候需提供*.dll
#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_)
?
原文:http://wenku.baidu.com/link?url=KzFoRIvlHDr9ptU8EsZ4x05Ku1oHA0d05kXzbM9jRPH2bCvV93CA6j23UEg-mYSvqHw6yA7hTFe_8wpiBUmtcOKztvhrkIiW3ol7RnoKJUu
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/weiquxiong/p/3547766.html
總結(jié)
以上是生活随笔為你收集整理的静态连接库、动态链接库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TEXTMETRIC 结构详解
- 下一篇: 学习网站随笔记录