1

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;
}
user1945128
  • 13
  • 1
  • 4
  • 3
    did you provide function definition for `Interface_Get(char* dllfilename);`? – billz Jan 03 '13 at 09:33
  • 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 Jan 03 '13 at 09:34
  • 1
    `Foo *foo = 0;` what is this for? – Karthik T Jan 03 '13 at 09:40
  • 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_ May 20 '14 at 15:11

2 Answers2

2

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
Community
  • 1
  • 1
Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609
1

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();
}
billz
  • 43,318
  • 8
  • 77
  • 98
  • Angew - The Foo::Interface_Get is defined already in the dll. I am also linking it to a header that contains definitions of all functions. Karthik - This is so I can do this foo->Interface_Get(...); Instead of Foo::Interface_Get(...); – user1945128 Jan 03 '13 at 09:49
  • billz - Already tried that in my .cpp file. I get 'may not be redeclared outside it's class'. I've tried doing it in the header also but within the class, still doesn't work. – user1945128 Jan 03 '13 at 09:51
  • see my updated answer, you need to provide function definition which you haven't – billz Jan 03 '13 at 09:58
  • billz - I have done that in an included .cpp file that is part of the compiled .dll, I was hoping it would find that definition and then allow me to define a new function with my arguments in INT WINAPI.... – user1945128 Jan 03 '13 at 10:16
  • then it could be the issue as Luchian mentioned. – billz Jan 03 '13 at 10:17