A. VC++ 绋嫔簭 濡备綍灏佽呬负dll鏂囦欢
鍒涘缓鏂扮殑锷ㄦ侀摼鎺ュ簱 (DLL) 椤圭洰
浠 钬沧枃浠垛濊彍鍗曚腑锛岄夋嫨 钬沧柊寤衡濓纴铹跺悗阃夋嫨 钬滈”鐩钬︹濄
鍦 钬滈”鐩绫诲瀷钬濈獥镙间腑锛岄夋嫨 钬淰isual C++钬濅笅镄 钬淲in32钬濄
鍦 钬沧ā𨱒库濈獥镙间腑锛岄夋嫨 钬淲in32 鎺у埗鍙板簲鐢ㄧ▼搴忊濄
涓洪”鐩阃夋嫨涓涓钖岖О锛屽
MathFuncsDll锛屽苟灏嗗叾阌鍏 钬滃悕绉扳濆瓧娈点 涓鸿В鍐虫柟妗堥夋嫨涓涓钖岖О锛屽
DynamicLibrary锛屽苟灏嗗叾阌鍏 钬滆В鍐虫柟妗埚悕绉扳濆瓧娈点
鍗曞嚮 钬灭‘瀹气濆惎锷 Win32 搴旂敤绋嫔簭钖戝笺
鍦 钬淲in32 搴旂敤绋嫔簭钖戝尖濆硅瘽妗嗙殑 钬沧傝堪钬濋〉涓锛屽崟鍑 钬滀笅涓姝モ濄
鍦 钬淲in32 搴旂敤绋嫔簭钖戝尖濅腑镄 钬滃簲鐢ㄧ▼搴忚剧疆钬濋〉涓锛岄夋嫨 钬滃簲鐢ㄧ▼搴忕被鍨嬧濅笅镄 钬淒LL钬濓纸濡傛灉鍙鐢锛夛纴鎴栬呴夋嫨 钬沧带鍒跺彴搴旂敤绋嫔簭钬濓纸濡傛灉 钬淒LL钬濅笉鍙鐢锛夈 镆愪簺鐗堟湰镄 Visual Studio
涓嶆敮鎸侀氲繃浣跨敤钖戝煎垱寤 DLL 椤圭洰銆傛偍鍙浠ョ◢钖庡规よ繘琛屾洿鏀癸纴浠ュ皢椤圭洰缂栬疟涓
DLL銆
鍦 钬淲in32 搴旂敤绋嫔簭钖戝尖濈殑 钬滃簲鐢ㄧ▼搴忚剧疆钬濋〉涓锛岄夋嫨 钬滈梼锷犻夐”钬濅笅镄 钬灭┖椤圭洰钬濄
鍗曞嚮 钬滃畬鎴悫濆垱寤洪”鐩銆
钖戝姩镐侀摼鎺ュ簱娣诲姞绫
鑻ヨ佷负鏂扮被鍒涘缓澶存枃浠讹纴璇蜂粠 钬滈”鐩钬濊彍鍗曚腑阃夋嫨 钬沧坊锷犳柊椤光︹濄
灏嗘樉绀 钬沧坊锷犳柊椤光濆硅瘽妗嗐
鍦 钬灭被鍒钬濈獥镙间腑锛岄夋嫨 钬淰isual C++钬濅笅镄 钬滀唬镰佲濄 鍦 钬沧ā𨱒库濈獥镙间腑阃夋嫨 钬滃ご鏂囦欢(.h)钬濄 涓哄ご鏂囦欢阃夋嫨涓涓钖岖О锛屽
MathFuncsDll.h锛屽苟鍗曞嚮 钬沧坊锷犫濄 灏嗘樉绀轰竴涓绌虹槠鏂囦欢銆
娣诲姞涓涓钖崭负钬淢yMathFuncs钬濈殑绠鍗旷被锛屼互镓ц屽父瑙佺殑绠楁湳杩愮畻锛屽傚姞銆佸噺銆佷箻鍜岄櫎銆备唬镰佸簲涓庝互涓嫔唴瀹圭被浼硷细
浠ュ甫链夐滆壊鍖哄垎镄勬牸寮忔煡鐪嫔嶅埗鍒板壀璐存澘镓揿嵃
// MathFuncsDll.h
namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static __declspec(dllexport) double Add(double a, double b);
// Returns a - b
static __declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
static __declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0
static __declspec(dllexport) double Divide(double a, double b);
};
}
// MathFuncsDll.h
namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static __declspec(dllexport) double Add(double a, double b);
// Returns a - b
static __declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
static __declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0
static __declspec(dllexport) double Divide(double a, double b);
};
}
璇锋敞镒忔や唬镰佹柟娉曞0鏄庝腑镄 __declspec(dllexport) 淇楗扮︺ 杩欎簺淇楗扮︿娇 DLL
鑳藉熷煎嚭璇ユ柟娉曚互渚涘叾浠栧簲鐢ㄧ▼搴忎娇鐢ㄣ傛湁鍏虫洿澶氢俊鎭锛岃峰弬瑙 dllexport, dllimport銆
鑻ヨ佷负鏂扮被鍒涘缓婧愭枃浠讹纴璇蜂粠 钬滈”鐩钬濊彍鍗曚腑阃夋嫨 钬沧坊锷犳柊椤光︹濄
灏嗘樉绀 钬沧坊锷犳柊椤光濆硅瘽妗嗐
鍦 钬灭被鍒钬濈獥镙间腑锛岄夋嫨 钬淰isual C++钬濅笅镄 钬滀唬镰佲濄 鍦 钬沧ā𨱒库濈獥镙间腑锛岄夋嫨 钬淐++ 鏂囦欢(.cpp)钬濄 涓烘簮鏂囦欢阃夋嫨涓涓钖岖О锛屽
MathFuncsDll.cpp锛屽苟鍗曞嚮 钬沧坊锷犫濄 灏嗘樉绀轰竴涓绌虹槠鏂囦欢銆
鍦ㄦ簮鏂囦欢涓瀹炵幇钬淢yMathFuncs钬濈殑锷熻兘銆备唬镰佸簲涓庝互涓嫔唴瀹圭被浼硷细
浠ュ甫链夐滆壊鍖哄垎镄勬牸寮忔煡鐪嫔嶅埗鍒板壀璐存澘镓揿嵃
// MathFuncsDll.cpp
// compile with: /EHsc /LD
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
}
// MathFuncsDll.cpp
// compile with: /EHsc /LD
#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
}
鑻ヨ佸皢椤圭洰鐢熸垚涓 DLL锛岃蜂粠
钬滈”鐩钬濊彍鍗曚腑阃夋嫨
MathFuncsDll 钬滃睘镐...钬濄 鍦ㄥ乏绐楁牸涓锛岄夋嫨 钬滈厤缃灞炴р濅笅镄 钬滃父瑙勨濄
鍦ㄥ彸绐楁牸涓锛屽皢 钬滈厤缃绫诲瀷钬濇洿鏀逛负 钬滃姩镐佸簱(.dll)钬濄
鍗曞嚮 钬灭‘瀹气濅缭瀛樻洿鏀广
璇存槑
濡傛灉鎭ㄤ粠锻戒护琛岀敓鎴愰”鐩锛岃蜂娇鐢
/LD 缂栬疟鍣ㄩ夐”鎸囧畾杈揿嚭鏂囦欢搴斾负 DLL銆
链夊叧镟村氢俊鎭锛岃峰弬瑙 /MD銆/MT銆/LD锛堜娇鐢ㄨ繍琛屽簱锛夈
缂栬疟璇ュ姩镐侀摼鎺ュ簱锛屾柟娉曟槸阃夋嫨
钬灭敓鎴悫濊彍鍗曚腑镄 钬灭敓鎴愯В鍐虫柟妗堚濄
杩欐牱灏卞垱寤轰简涓涓鍙渚涘叾浠栫▼搴忎娇鐢ㄧ殑
DLL銆