我在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的位置)
答案 0 :(得分:1)
您的Shape
课程应位于#ifdef
区域之外,而不在其中。除非定义了Shape
符号,否则代码不会声明SHAPE_EXPORT
类。
你想做的是
#ifdef SHAPE_EXPORTS
#define SHAPE_API __declspec(dllexport)
#else
#define SHAPE_API __declspec(dllimport)
#endif
class SHAPE_API Shape
// etc