Making Visual C++ DLL from C++ class

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2011-01-13T02:04:43Z Indexed on 2011/01/13 2:53 UTC
Read the original article Hit count: 234

I have the following C++ code to make dll (Visual Studio 2010).

class Shape {
public:
  Shape() {
    nshapes++;
  }
  virtual ~Shape() {
    nshapes--;
  };
  double  x, y;   
  void    move(double dx, double dy);
  virtual double area(void) = 0;
  virtual double perimeter(void) = 0;
  static  int nshapes;
};

class __declspec(dllexport) Circle : public Shape {
private:
  double radius;
public:
  Circle(double r) : radius(r) { };
  virtual double area(void);
  virtual double perimeter(void);
};

class __declspec(dllexport) Square : public Shape {
private:
  double width;
public:
  Square(double w) : width(w) { };
  virtual double area(void);
  virtual double perimeter(void);
};

I have the __declspec,

class __declspec(dllexport) Circle

I could build a dll with the following command

CL.exe /c example.cxx
link.exe /OUT:"example.dll" /DLL example.obj 

When I tried to use the library,

Square* square; square->area()

I got the error messages. What's wrong or missing?

example_unittest.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall
...
Square::area(void)" (?area@Square@@UAENXZ)

ADDED

Following wengseng's answer, I modified the header file, and for DLL C++ code, I added

#define XYZLIBRARY_EXPORT

However, I still got errors.

example_unittest.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __th
iscall Circle::Circle(double)" (__imp_??0Circle@@QAE@N@Z) referenced in function "protected: virtual
 void __thiscall TestOne::SetUp(void)" (?SetUp@TestOne@@MAEXXZ)
example_unittest.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __th
iscall Square::Square(double)" (__imp_??0Square@@QAE@N@Z) referenced in function "protected: virtual
 void __thiscall TestOne::SetUp(void)" (?SetUp@TestOne@@MAEXXZ)
example_unittest.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall
Square::area(void)" (?area@Square@@UAENXZ)
example_unittest.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall
Square::perimeter(void)" (?perimeter@Square@@UAENXZ)
example_unittest.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall
Circle::area(void)" (?area@Circle@@UAENXZ)
example_unittest.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall
Circle::perimeter(void)" (?perimeter@Circle@@UAENXZ)

© Stack Overflow or respective owner

Related posts about c++

Related posts about Windows