代码之家  ›  专栏  ›  技术社区  ›  Alvaromon

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

  •  0
  • Alvaromon  · 技术社区  · 6 年前

    我上课有困难 Shape 从shapetester.cpp中的shape.dll(另一个.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的位置)

    属性& gt;配置属性& g/c+c++gt;附加目录(指向形状h的位置)

    1 回复  |  直到 6 年前
        1
  •  1
  •   1201ProgramAlarm    6 年前

    你的 Shape 课程应该在 #ifdef 封锁,不在里面。代码本身不会声明 形状 除非 SHAPE_EXPORT 符号已定义。

    你想做的是

    #ifdef SHAPE_EXPORTS
    #define SHAPE_API __declspec(dllexport)
    #else
    #define SHAPE_API __declspec(dllimport)
    #endif
    
    class SHAPE_API Shape
    // etc