-1

I am trying to build a dll to go along with my program in c++. The dll will be a basic library with a bunch of inheritable classes and general utilities, which can then be used dynamically by multiple other applications that will accompany the final product(A Game). I threw something together, only to find that I am recieving I maddening error. It is the famous "error LNK2019: unresolved external symbol". First off, I already found the solution to this, and here is the link: Unresolved External Symbol- Error in guide?

The chosen answer works. If I put my class into only a header file, it compiles perfectly fine, runs, and is all nice and pretty. However, I want to keep the declaration and implementation separate, and if I try to move the implementation to a separate cpp I receive the LNK2019 error. This is due to some kind of inlining, I just want to know how I can bloody fix it before I start tearing my hair out.

Can anyone help me with this? Here is my class:

Header:

#ifndef MYDLLTESTCLASS_H
#define MYDLLTESTCLASS_H
class __declspec( dllexport ) MyDLLTestClass {
    public:
    MyDLLTestClass();
    void setX( int x );
    int getX();
    private:
    int x;
};
#endif // MYDLLTESTCLASS_H

CPP:

#include "MyDLLTestClass.h"

MyDLLTestClass::MyDLLTestClass() {

}

void MyDLLTestClass::setX( int x ) {
    this->x = x;
}

int MyDLLTestClass::getX() {
    return x;
}

Separate like above, the code wont compile. But if I throw the declaration and implementation together It works, so if I do this:

#ifndef MYDLLTESTCLASS_H
#define MYDLLTESTCLASS_H
class __declspec( dllexport ) MyDLLTestClass {
    public:
    MyDLLTestClass();
    void setX( int x );
    int getX();
    private:
    int x;
};


MyDLLTestClass::MyDLLTestClass() {

}

void MyDLLTestClass::setX( int x ) {
    this->x = x;
}

int MyDLLTestClass::getX() {
    return x;
}
#endif // MYDLLTESTCLASS_H

It will work. Again, I WANT the declaration and implementation separate.

Here is the build report when I use separate declaration and implementation.

http://pastebin.com/HMEpeEgn

Community
  • 1
  • 1
Krythic
  • 4,184
  • 5
  • 26
  • 67
  • Which symbol is unresolved? – Jonathan Potter Nov 22 '14 at 07:11
  • @JonathanPotter I think my comment got deleted somehow... I added a link to the build-output to the bottom of my post for you to look at. – Krythic Nov 22 '14 at 07:22
  • 1
    Your problem is the declspec on the class in the header file. You don't conditionally use `__declspec(dllimport)` for *consumers* of your DLL. There is a reason a default DLL project declares macros to handle this for you. Your error log has no issues linking the DLL; it is the *client* that can't find the symbol. – WhozCraig Nov 22 '14 at 07:59
  • @whozCraig Can I ask to see some pseudo code demonstrating this? – Krythic Nov 22 '14 at 22:25
  • @WhozCraig I solved my problem; you were way off. Solution will be added below. – Krythic Nov 22 '14 at 23:10

1 Answers1

0

This was actually far easier to solve than I realized; it turns out the it was a visual studio setting all along. First, you need to add the dll project to your current project(Which will be using it). Then you need to right click on you current project and Follow these two pictures:

enter image description here

And then add your dll, which might just need to be check-marked(Mine was simply missing the checkmark).

enter image description here

After that your project will compile perfectly fine! No need for nasty-ass #ifndef macros like every webpage has attempted to portray(which also didn't work mind you)

Krythic
  • 4,184
  • 5
  • 26
  • 67