【问题标题】:Using STL in C++ DLL project在 C++ DLL 项目中使用 STL
【发布时间】: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&lt;_Ty&gt; *,std::vector&lt;_Ty&gt; *)' : 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++ dllPython 之间的未来合作。

欢迎提出任何建议!再次感谢! :)

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 就是这样做的)。

标签: c++ dll stl


【解决方案1】:

我没有尝试编译您的代码,但我发现了一个问题:在 MathFuncsDll.h 中,您将“std::vector”类称为“vector” - 这应该是“std: :向量”。换句话说,MathFuncsDll.h 中的最后一个声明应该是这样的:

    static __declspec(dllexport) double Sumproduct(std::vector<double>* a, std::vector<double>* b);

当然,你也可能有其他问题 :-)

【讨论】:

  • 感谢您的建议。我试过了。但似乎更多的错误即将出现。我得到的新的是error C2039: 'vector' : is not a member of 'std'。这很奇怪,因为我们都知道vector 绝对是std 的成员...
  • 我现在尝试编译您的代码(尽管使用 VS2010),但没有看到该错误。您正在编译的代码是否与您发布的代码相同?
  • 是的,我直接从我的 IDE 复制它。
  • 错误提示你忘记#include in MathFuncsDll.h。
  • 这很奇怪。 cos #include &lt;vector&gt; 在 MathFuncsDll.h 中向我招手。 T_T
【解决方案2】:

Boost.Python 似乎正是您想要的。我自己没有尝试过(不是 Pythonista),但我一直在使用其他 Boost 库,它们都很棒。

【讨论】:

  • 非常感谢。我会尝试一下! :)
猜你喜欢
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多