0

I have a visual c++ console project named "C_test".

Another project is called "dll_project" which is dll project.

in "C_test" project, I have set the location of additional include directory for "dll_project".

I have these simple codes.

in "C_test" project's main.cpp.

#include "SampleClass.h"

int main()
{
    SampleClass *_parser    = new SampleClass();

    return 0;
}

And in "dll_project" SampleClass.h and SampleClass.cpp.

SampleClass.h

#pragma once
class SampleClass
{
public:
    SampleClass(void);
    ~SampleClass(void);
};

SampleClass.cpp

#include "SampleClass.h"


SampleClass::SampleClass(void)
{
}


SampleClass::~SampleClass(void)
{
}

When I try to debug or build, I get this error.

error LNK2019: unresolved external symbol "public: __thiscall SampleClass::SampleClass(void)" (??0SampleClass@@QAE@XZ) referenced in function _main C:\C_test\main.obj C_test

What have I done wrong?

Joshua Son
  • 1,839
  • 6
  • 31
  • 51
  • did you linked the `dll_project.lib` to `C_test`? – user1810087 Mar 05 '14 at 14:14
  • And don´t forget to delete new´ed things. – deviantfan Mar 05 '14 at 14:15
  • 2
    You forgot to export the class from the DLL. So your EXE project only sees the class declaration but not the implementation. Using __declspec(dllexport) in the DLL project, __declspec(dllimport) and linking to the DLL's import library in the EXE project is required. – Hans Passant Mar 05 '14 at 14:15
  • @user1810087 I am new to C++, but my dll_project is dynamic library project not static library. How can I export to dll_project.lib when it's dynamic library? – Joshua Son Mar 05 '14 at 14:17
  • even the dynamic lib generates a .lib which is *just* a lookup table. this should be linked with the program. the dll itself should be either in the same directory as the program or in the system directory of your os. also as Hans Passant mentioned, you forgot to export/import the class. [small howto](http://msdn.microsoft.com/en-us/library/ms235636.aspx). – user1810087 Mar 05 '14 at 14:23
  • @HansPassant Unfortunately, I am very new to c++. Would you like to show me some code snippet? – Joshua Son Mar 05 '14 at 14:23
  • @user1810087 Are you sure that dynamic library also generates a lib file? coz, I cannot find it in any of my project directory. – Joshua Son Mar 05 '14 at 14:25
  • This is already well covered by [the MSDN library](http://msdn.microsoft.com/en-us/library/ys435b3s.aspx), example included. – Hans Passant Mar 05 '14 at 14:25

0 Answers0