【问题标题】:Cant use a class from an implicitly linked .dll无法使用隐式链接的 .dll 中的类
【发布时间】:2018-05-20 18:46:44
【问题描述】:

我无法在我的 ShapeTester.cpp(另一个 .dll 项目)中使用我的 Shape.dll 中的 Shape 类。

//Shape.h

#ifdef SHAPE_EXPORTS
#define SHAPE_API __declspec(dllexport)

class SHAPE_API Shape
{
public:
Shape();
Shape(int sides, int sideLength, int apothem);
~Shape();

int Perimeter();
double Area();
private:
int sides;
int sideLength;
int apothem;
};
#endif

------------------------------------------------------------
//Shape.cpp

#include "stdafx.h"
#include "Shape.h"

Shape::Shape() : sides(0), sideLength(0), apothem(0)
{
}

Shape::Shape(int sides, int sideLength, int apothem) : sides(sides), sideLength(sideLength), apothem(apothem)
{
}

Shape::~Shape()
{
}

double Shape::Area()
{
    //implementation
}

int Shape::Perimeter()
{
    //implementation
}

-----------------------------------------------------------
//ShapeTester.cpp (this is in another DLL project)
#include "stdafx.h"
#include "ShapesTester.h"
#include "Shape.h"

bool ShapesTester::Test()
{
    Shape myShape = Shape(3, 9, 5); // error here; cant resolve symbol Shape

    return myShape.Area() == 67.5;
}

我在预处理器指令中包含了 SHAPE_EXPORT,我可以获得 .dll、.lib

属性 > 配置属性 > 链接器 > 输入 > 将附加依赖项设置为 Shape.lib

属性>配置属性>链接器>常规>附加库目录(指向Shape.Lib的位置)

属性 > 配置属性 > C/C++ > 附加包含目录(指向 Shape.h 的位置)

【问题讨论】:

  • 请逐字发布您收到的编译器/链接器错误。在没有看到错误的情况下,除了猜测之外,您还指望我们做什么?
  • 我按字面意思表示无法解析符号“形状”
  • 没有发布错误消息逐字。这只是发布您的 interpreted 版本的错误消息。只需发布原始版本即可。

标签: c++ dll linker dllexport


【解决方案1】:

你的Shape 类应该在#ifdef 块之外,而不是在它里面。除非定义了 SHAPE_EXPORT 符号,否则代码不会声明 Shape 类。

你想做的是

#ifdef SHAPE_EXPORTS
#define SHAPE_API __declspec(dllexport)
#else
#define SHAPE_API __declspec(dllimport)
#endif

class SHAPE_API Shape
// etc

【讨论】:

  • 感谢您的回复。我现在要试试这个,但我在预处理器指令中定义了SHAPE_EXPORT
  • 嘿哇这工作,或者至少我得到了新的错误。对于行'Shape myShape = Shape(3, 9, 5);'它现在说 SHAPE_API 类型不完整,并且没有需要 3 个参数的重载,但有......
  • 做到了非常感谢你,我从昨晚开始就一直在努力解决这个问题。你能解释一下到底发生了什么吗?我认为定义 SHAPE_EXPORT 预处理器指令就足够了
  • 通常 #ifdef 块的错误分支是:“#define MYDLL_API __declspec(dllimport)”,这样隐式链接到 DLL 的代码会自动导入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多