作为C++的一个完整新手,我在MS教程发现了{A1}之后,创建了我的第一个动态链接库。
头文件如下所示:// 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);
};
}
现在,我想把这个文件读入Python ctypes。我使用:
^{pr2}$
现在,我很难真正访问这些函数。我的直觉引导我去尝试a = ctypes.c_double(54)
b = ctypes.c_double(12)
summation = MyFuncs.Add(a,b)
返回错误AttributeError: function 'Add' not found
我的问题是函数嵌套在class MyMathFuncs中,而该函数也在namespace MathFuncs中?如何访问这些功能?在
以下是用于生成dll的.cpp文件的内容以供参考// MathFuncsDll.cpp
// compile with: /EHsc /LD
#include "MathFuncsDll.h"
#include
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;
}
}