【发布时间】:2013-09-11 14:08:48
【问题描述】:
我正在尝试编写一个简单的应用程序来熟悉 C++ 中的 dll 项目。我首先遵循了 MSDN 上的示例:http://msdn.microsoft.com/en-us/library/ms235636(v=vs.90).aspx
效果很好,所以我更进一步尝试。这是我的代码:
// MathFuncsDll.h
#include <vector>
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
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 const std::invalid_argument& if b is 0
static __declspec(dllexport) double Divide(double a, double b);
static __declspec(dllexport) double Sumproduct(vector<double>* a, vector<double>* b);
// **Here I use std::vector**
};
}
在MathFuncsDll.cpp:
// MathFuncsDll.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>
#include <vector>
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 invalid_argument("b cannot be zero!");
}
return a / b;
}
// **Here is the definition of Sumproduct**
double MyMathFuncs::Sumproduct(vector<double>* a, vector<double>* b)
{
int size1 = a->size(), size2 = b->size();
if(size1 != size2)
{
throw invalid_argument("The length of input vectors should be the same!");
}
double sum = 0;
for(int i = 0; i < size1; i++)
sum += a->at(i) * b->at(i);
return sum;
}
}
在main:
// MyExecRefsDll.cpp
// compile with: /EHsc /link MathFuncsDll.lib
#include <iostream>
#include "MathFuncsDll.h"
using namespace std;
int main()
{
double a = 7.4;
int b = 99;
cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b) << endl;
cout << "a - b = " <<
MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
cout << "a * b = " <<
MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
cout << "a / b = " <<
MathFuncs::MyMathFuncs::Divide(a, b) << endl;
try
{
cout << "a / 0 = " <<
MathFuncs::MyMathFuncs::Divide(a, 0) << endl;
}
catch (const invalid_argument &e)
{
cout << "Caught exception: " << e.what() << endl;
}
system("pause");
return 0;
}
当我尝试使用 VS2012 构建解决方案时,它会给出以下错误消息
error C2511: 'double MathFuncs::MyMathFuncs::Sumproduct(std::vector<_Ty> *,std::vector<_Ty> *)' : overloaded member function not found in 'MathFuncs::MyMathFuncs'
error C2061: syntax error : identifier 'vector'
我对dll项目很陌生,所以请原谅我的无知。我可以知道我错了吗?
非常感谢。
================================================ ===============
编辑1:
我认为通过提供大图可以更容易地找到答案。我打算编写一些 .dll 文件并从 Python 代码中调用它们。正如@Chad 建议的那样,在上述方法中这样做不是一个好主意,因为它会迫使用户(即我)使用与 STL 相同的实现。
那么,请问,为了实现我的计划,有没有更好的办法呢?
非常感谢。 :)
================================================ ===============
EDIT2:
感谢大家的热心帮助。特别感谢@Anodyne。现在我的起源问题被 Anodyne 完美地回答了。但是,正如我在EDIT1 中所说,现在我必须考虑C++ dll 和Python 之间的未来合作。
欢迎提出任何建议!再次感谢! :)
PS:我应该为此打开一个新线程吗?
【问题讨论】:
-
标头中缺少命名空间 std - 在标头中使用 std::vector 而不是矢量。
-
编译dll或主工程时报错?
-
@Raxvan 感谢您的回复。当我尝试运行主项目时会发生这种情况。
-
我建议避免在你的 DLL 接口中使用
C++。您当然可以在内部使用它,但是跨 DLL 边界发送C++类会对您的库的用户产生重大影响——迫使他们使用相同的 STL 实现(这可能意味着相同的编译器版本和 C 运行时)。 (相关:stackoverflow.com/questions/5107948/…) -
我在这里写过关于在 DLL 接口中使用 C++ 的问题:lenholgate.com/blog/2005/06/working-on-the-borders.html 您通常可以通过 a) 在接口中避免 STL 和 b) 使用标准 C 链接到工厂方法返回一个纯虚拟“接口”类(毕竟,COM 就是这样做的)。