Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Visual C++ DLL from C++ class
    primarykey
    data
    text
    <p>I have the following C++ code to make dll (Visual Studio 2010).</p> <pre><code>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); }; </code></pre> <p>I have the __declspec, </p> <pre><code>class __declspec(dllexport) Circle </code></pre> <p>I could build a dll with the following command</p> <pre><code>CL.exe /c example.cxx link.exe /OUT:"example.dll" /DLL example.obj </code></pre> <p>When I tried to use the library, </p> <pre><code>Square* square; square-&gt;area() </code></pre> <p>I got the error messages. What's wrong or missing?</p> <pre> example_unittest.obj : error LNK2001: unresolved external symbol "public: virtual double __thiscall ... Square::area(void)" (?area@Square@@UAENXZ) </pre> <h2>ADDED</h2> <p>Following wengseng's answer, I modified the header file, and for DLL C++ code, I added </p> <pre><code>#define XYZLIBRARY_EXPORT </code></pre> <p>However, I still got errors.</p> <h2>SOLVED</h2> <p>For main program that links example.dll, I didn't link example.lib.</p> <pre><code>cl /MD /EHsc gtest_main.cc example_unittest.cc /I"./include" /link /libpath:"./lib" /libpath:"." gtest_md.lib example.lib /out:gtest_md_release.exe </code></pre> <p>With the addition, everything works fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload