解析VC中創(chuàng)建DLL,導(dǎo)出全局變量,函數(shù)和類的深入分析
更新時間:2013年05月17日 15:49:40 作者:
本篇文章是對VC中創(chuàng)建DLL,導(dǎo)出全局變量,函數(shù)和類進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
一.創(chuàng)建DLL
1.在VC中新建一個Win32空項目MathLib;
2.添加預(yù)編譯頭文件stdafx.h,定義導(dǎo)入導(dǎo)出控制符號:
//stdafx.h
#pragma once
#define MATHLIB_EXPORT
3.添加包含要導(dǎo)出的全局變量,函數(shù)和類的頭文件MathLib.h:
//MathLib.h
#pragma once
#ifdef MATHLIB_EXPORT
#define MATHLIBAPI __declspec(dllexport)
#else
#define MATHLIBAPI __declspec(dllimport)
#endif
//macro
#define PI 3.14149
//Global variable
extern MATHLIBAPI int GlobalVariable;
//Function
MATHLIBAPI int Add(int a,int b);
//Class
class MATHLIBAPI Math
{
public:
int Multiply(int a,int b);
};
4.添加所導(dǎo)出元素的實現(xiàn)文件MathLib.cpp
//MathLib.cpp
#include "stdafx.h"
#include "MathLib.h"
int GlobalVariable = 100;
int Add(int a,int b)
{
return a+b;
}
int Math::Multiply(int a,int b)
{
return a*b;
}
二,測試所創(chuàng)建的DLL
測試代碼:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "../MathLib/MathLib.h"
#pragma comment(lib,"../Debug/MathLib.lib")
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Pi = "<<PI<<endl;
cout<<"GlobalVariable = "<<GlobalVariable<<endl;
int a = 20,b = 30;
cout<<"a="<<a<<", "<<"b="<<b<<endl;
cout<<"a+b = "<<Add(a,b)<<endl;
Math math;
cout<<"a*b = "<<math.Multiply(a,b)<<endl;
return 0;
}
1.在VC中新建一個Win32空項目MathLib;
2.添加預(yù)編譯頭文件stdafx.h,定義導(dǎo)入導(dǎo)出控制符號:
復(fù)制代碼 代碼如下:
//stdafx.h
#pragma once
#define MATHLIB_EXPORT
3.添加包含要導(dǎo)出的全局變量,函數(shù)和類的頭文件MathLib.h:
復(fù)制代碼 代碼如下:
//MathLib.h
#pragma once
#ifdef MATHLIB_EXPORT
#define MATHLIBAPI __declspec(dllexport)
#else
#define MATHLIBAPI __declspec(dllimport)
#endif
//macro
#define PI 3.14149
//Global variable
extern MATHLIBAPI int GlobalVariable;
//Function
MATHLIBAPI int Add(int a,int b);
//Class
class MATHLIBAPI Math
{
public:
int Multiply(int a,int b);
};
4.添加所導(dǎo)出元素的實現(xiàn)文件MathLib.cpp
復(fù)制代碼 代碼如下:
//MathLib.cpp
#include "stdafx.h"
#include "MathLib.h"
int GlobalVariable = 100;
int Add(int a,int b)
{
return a+b;
}
int Math::Multiply(int a,int b)
{
return a*b;
}
二,測試所創(chuàng)建的DLL
測試代碼:
復(fù)制代碼 代碼如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "../MathLib/MathLib.h"
#pragma comment(lib,"../Debug/MathLib.lib")
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Pi = "<<PI<<endl;
cout<<"GlobalVariable = "<<GlobalVariable<<endl;
int a = 20,b = 30;
cout<<"a="<<a<<", "<<"b="<<b<<endl;
cout<<"a+b = "<<Add(a,b)<<endl;
Math math;
cout<<"a*b = "<<math.Multiply(a,b)<<endl;
return 0;
}
您可能感興趣的文章:
- VC創(chuàng)建DLL動態(tài)鏈接庫的方法
- VC創(chuàng)建進(jìn)程CreateProcess的方法
- VC實現(xiàn)動態(tài)菜單的創(chuàng)建方法
- VC++創(chuàng)建msi文件的方法
- MVC 5 第一章 創(chuàng)建MVC 5 web應(yīng)用程序
- c#創(chuàng)建vc可調(diào)用的com組件方法分享
- MVC后臺創(chuàng)建Json(List)前臺接受并循環(huán)讀取實例
- VC6.0如何創(chuàng)建以及調(diào)用動態(tài)鏈接庫實例詳解
- VC創(chuàng)建圓角dialog的實現(xiàn)方法
相關(guān)文章
Matlab實現(xiàn)簡易紀(jì)念碑谷游戲的示例代碼
《紀(jì)念碑谷》是USTWO公司開發(fā)制作的解謎類手機游戲,在游戲中,通過探索隱藏小路、發(fā)現(xiàn)視力錯覺以及躲避神秘的烏鴉人來幫助沉默公主艾達(dá)走出紀(jì)念碑迷陣。本文將用Matlab編寫簡易版的紀(jì)念碑谷游戲,感興趣的可以了解一下2022-03-03