第五天2017/04/06(下午2:动态链接库(DLL))
生活随笔
收集整理的這篇文章主要介紹了
第五天2017/04/06(下午2:动态链接库(DLL))
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
創(chuàng)建動(dòng)態(tài)庫(kù):新建一個(gè)DLL的VS工程,選擇“空項(xiàng)目”,此時(shí)會(huì)進(jìn)入到新建的項(xiàng)目工程中;在項(xiàng)目工程中添加.h、.c、.cpp文件,并且填寫代碼;編譯運(yùn)行,在Debug下生成.dll和.lib文件,此時(shí)創(chuàng)建成功。
【注解】在創(chuàng)建動(dòng)態(tài)庫(kù)過程中,用到很多語法,比如:C與C++的混合編程#ifdef __cplusplus extern "C"{#endif //函數(shù)的聲明 #ifdef __cplusplus }#endif 、導(dǎo)出DLL函數(shù)關(guān)鍵字__declspec (dllexport) + 函數(shù)的聲明與實(shí)現(xiàn)、#ifdef...#else...#endif(#ifdef __cplusplus...#else...#endif) 、 防止重定義頭文件#ifndef...#define...#endif 、 #define =====================================================================使用動(dòng)態(tài)庫(kù):新建一個(gè)使用動(dòng)態(tài)庫(kù)的項(xiàng)目,把上面生成的.dll和.lib文件以及.h文件拷貝到當(dāng)前工程目錄下;在項(xiàng)目中新建main.c或者main.cpp,并加上調(diào)用的代碼,編譯運(yùn)行,此時(shí)動(dòng)態(tài)庫(kù)被使用。
【注解】在使用動(dòng)態(tài)庫(kù)過程中,用到很多語法,比如:鏈接器的附加依賴項(xiàng)#pragma comment(lib,"DLL.lib")
【下面有兩個(gè)創(chuàng)建和使用動(dòng)態(tài)庫(kù)的示例】
例1:
【功能介紹】
創(chuàng)建動(dòng)態(tài)庫(kù):①在頭文件中聲明兩個(gè)函數(shù)②一個(gè)在.c文件中實(shí)現(xiàn),另一個(gè)在.cpp文件中實(shí)現(xiàn)
使用動(dòng)態(tài)庫(kù):在main.c和main.cpp使用上面生成的動(dòng)態(tài)庫(kù)中的兩個(gè)函數(shù)
(1)創(chuàng)建動(dòng)態(tài)庫(kù)
//DLL.h頭文件聲明2個(gè)函數(shù)
#ifndef DLL_H
#define DLL_H #ifdef BUILD_DLL
#define PORT_DLL __declspec(dllexport)
#else
#define PORT_DLL __declspec(dllimport)
#endif #ifdef __cplusplus
#include <iostream>
using namespace std;
extern "C"
{
#endifPORT_DLL int add(int a, int b); PORT_DLL void show(); #ifdef __cplusplus
};
#endif#endif // DLL1_H
------------------------------
//define_add.cpp
#define BUILD_DLL#include "DLL.h" PORT_DLL int add(int a,int b)
{
#ifdef __cplusplusprintf("DLL生成使用成功:add()函數(shù) —— C++編譯環(huán)境\n");return a+b;
#elseprintf("DLL生成使用成功:add()函數(shù) —— C編譯環(huán)境\n");return a+b;
#endif
}
------------------------------
//define_show.c
#define BUILD_DLL#include "DLL.h"PORT_DLL void show()
{
#ifdef __cplusplusprintf("\nDLL生成使用成功:show()函數(shù) —— C++編譯環(huán)境\n");
#elseprintf("\nDLL生成使用成功:show()函數(shù) —— C編譯環(huán)境\n");
#endif
}//編譯運(yùn)行,會(huì)生成DLL.dll和DLL.lib文件,再加上DLL.h文件用于下面的DLL的使用
(2)使用動(dòng)態(tài)庫(kù)
DLL.dll和DLL.lib文件,再加上DLL.h文件拷貝到新建的工程目錄下,在配置好鏈接器中的“附加依賴項(xiàng)”,最后在main.cpp中寫上下面的代碼://main.c或者main.cpp調(diào)用DLL.dll中的2個(gè)函數(shù)
#include "DLL.h"
#pragma comment(lib,"DLL.lib")
int main()
{cout<<"add(1,2) = "<<add(1,2)<<endl;show();getchar();
}
編譯運(yùn)行,結(jié)果如下圖所示:
——————————————————————————————————————
例2: 【創(chuàng)建DLL】 //head.h #ifndef _HEAD_H_ #define _HEAD_H_#ifdef __cplusplus #include <iostream> using namespace std; extern "C" { #endif__declspec(dllexport) void show1();__declspec(dllexport) void show2(); #ifdef __cplusplus }; #endif#endif ---------------------------------------------------------------- //define1.cpp #include "head.h"__declspec(dllexport) void show1() { #ifdef __cpluspluscout<<"show1:這是一個(gè)c++程序"<<endl; #elseprintf("show1:這是一個(gè)C程序\n"); #endif } ---------------------------------------------------------------- //define2.c #include "head.h"__declspec(dllexport) void show2() { #ifdef __cpluspluscout<<"show1:這是一個(gè)c++程序"<<endl; #elseprintf("show2:這是一個(gè)C程序\n"); #endif }【使用DLL】 //.dll,.lib,.h三個(gè)文件拷貝到新建的工程中,再在main.c或者main.cpp添加下面代碼 #include "head.h" #pragma comment(lib,"DLL.lib") int main() {show1();show2();getchar(); } 編譯運(yùn)行,結(jié)果如下圖所示:總結(jié)
以上是生活随笔為你收集整理的第五天2017/04/06(下午2:动态链接库(DLL))的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第五天2017/04/06(下午3:静态
- 下一篇: 第六天2017/04/11(1:结构体链