不能使用隐式链接.dll中的类

Cant use a class from an implicitly linked .dll

本文关键字:dll 链接 不能      更新时间:2023-10-16

我在ShapeTester中使用Shape.dll中的类Shape时遇到问题.cpp(另一个.dll项目。

//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 的位置(

你的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