1
votes

I've spent the last 2 days searching for and implementing the answers from similar questions into my code with little success. I have an API that is an external .dll (windows) and I have the header file included into my .cpp file to reference the API.

However I have this issue that no matter what I do, I always get an unresolved external symbol that references this line in my .h file. Yes, I have used Google and modified the answers I found into my code, with no success.

Foo.h

Class Foo {
    public:
        static Foo* Interface_Get(char* dllfilename);

Foo.cpp

// I declare this just underneath the #include "Foo.h" header
Foo *foo = 0;

Inside my main function I declare it as this (along with some other functions that are fine).

//This has already been created and both Header and .dll are in the same directory.
Foo::Interface_Get("bar.dll"); 

And I get this error

error LNK2019: unresolved external symbol 
    "public: static class Foo * __cdecl Foo::Interface_Get(char *)"

I've tried everything I know (This is my first .dll creation experience) I have a feeling I am missing something painfully obvious, but for the life of me I cannot see it.

Entire Foo.cpp

#include "Foo.h"
Foo* Foo::Interface_Get(char* dllfilename); //May not be redeclared outside class error

Foo* foo = 0;

bool Frame()
{
if (foo->Key_Down(DIK_ESCAPE))
    return false;   
return true;
}


INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
foo->Interface_Get("bar.dll");

foo->System_SetState(grSTATE_FRAME, Frame);

foo->System_SetState(grSTATE_WINDOWED, true);

foo->System_SetState(grSTATE_KEYBOARD, true);

foo->System_Initiate();

foo->System_Start();

foo->System_Shutdown();

foo->Inferface_Release();

return 0;
}
2
did you provide function definition for Interface_Get(char* dllfilename);? - billz
Is Foo::Interface_Get your code, or is it implemented in the DLL? If it's yours, is it defined in your program? If it's in the DLL, do you link the DLL and does it export the function? - Angew is no longer proud of SO
Foo *foo = 0; what is this for? - Karthik T
Adding this for the sake of completeness, since it can help somebody. I had a similar problem and I knew I did something stupid. It turned out I forgot to add ClassName:: before name of the method :( - TT_

2 Answers

2
votes

This question explains common problems, and in your case it's (probably) a combination of:

  • (possibly) forgetting to implement the function
  • forgetting __declspec(dllexport)
  • forgetting to link against the library
1
votes

You need to provide function definition for Interface_Get(char* dllfilename); if you haven't done that.

This only redeclares function again, you need to provide function like below format with {}

Foo* Foo::Interface_Get(char* dllfilename); //May not be redeclared outside class error

Foo.cpp

Foo* Foo::Interface_Get(char* dllfilename)
{
  //....
  return new Foo();
}