解析VC中創(chuàng)建DLL,導(dǎo)出全局變量,函數(shù)和類的深入分析
更新時(shí)間:2013年05月17日 15:49:40 作者:
本篇文章是對VC中創(chuàng)建DLL,導(dǎo)出全局變量,函數(shù)和類進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
一.創(chuàng)建DLL
1.在VC中新建一個(gè)Win32空項(xiàng)目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)出元素的實(shí)現(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中新建一個(gè)Win32空項(xiàng)目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)出元素的實(shí)現(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動(dòng)態(tài)鏈接庫的方法
- VC創(chuàng)建進(jìn)程CreateProcess的方法
- VC實(shí)現(xiàn)動(dòng)態(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)讀取實(shí)例
- VC6.0如何創(chuàng)建以及調(diào)用動(dòng)態(tài)鏈接庫實(shí)例詳解
- VC創(chuàng)建圓角dialog的實(shí)現(xiàn)方法
相關(guān)文章
c語言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具代碼分享
這篇文章主要介紹了c語言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具,大家參考使用吧2014-01-01C++類與對象的基礎(chǔ)知識點(diǎn)詳細(xì)分析
類和對象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語言的合稱。對象是對客觀事物的抽象,類是對對象的抽象。類是一種抽象的數(shù)據(jù)類型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域2023-02-02Matlab實(shí)現(xiàn)簡易紀(jì)念碑谷游戲的示例代碼
《紀(jì)念碑谷》是USTWO公司開發(fā)制作的解謎類手機(jī)游戲,在游戲中,通過探索隱藏小路、發(fā)現(xiàn)視力錯(cuò)覺以及躲避神秘的烏鴉人來幫助沉默公主艾達(dá)走出紀(jì)念碑迷陣。本文將用Matlab編寫簡易版的紀(jì)念碑谷游戲,感興趣的可以了解一下2022-03-03C++實(shí)現(xiàn)動(dòng)態(tài)規(guī)劃過程詳解
動(dòng)態(tài)規(guī)劃是解決一類最優(yōu)問題的常用方法,它是解決最優(yōu)化問題的一種途徑,在本文中,我們將討論如何使用C++實(shí)現(xiàn)動(dòng)態(tài)規(guī)劃算法,并提供一些示例來幫助您更好地理解該算法2023-05-05