Unresolved external symbol in a DLL
-
Hello everyone, I am developing a DLL and making another console application to link with the DLL. The DLL expose type CFoo, create an instance and the application consumes it. There is error like this. The source code of DLL and main are listed below, any ideas?
1>main.obj : error LNK2001: unresolved external symbol "class CFoo foo" (?foo@@3VCFoo@@A)
foo.h (defines exposed type in the DLL)
class __declspec (dllexport) CFoo
{
public:CFoo(); int foo1(); int foo2();
};
dllmain.cpp (defines type implementation inside dll)
#include "foo.h"
CFoo::CFoo()
{
return;
}int CFoo::foo1()
{
return 100;
}int CFoo::foo2()
{
return 200;
}CFoo foo;
main.cpp (the console application which dynamically links with the DLL using import library)
#include "foo.h"
extern CFoo foo;
int main()
{
int i = foo.foo1();
i = foo.foo2();return 0;
}
thanks in advance, George
-
Hello everyone, I am developing a DLL and making another console application to link with the DLL. The DLL expose type CFoo, create an instance and the application consumes it. There is error like this. The source code of DLL and main are listed below, any ideas?
1>main.obj : error LNK2001: unresolved external symbol "class CFoo foo" (?foo@@3VCFoo@@A)
foo.h (defines exposed type in the DLL)
class __declspec (dllexport) CFoo
{
public:CFoo(); int foo1(); int foo2();
};
dllmain.cpp (defines type implementation inside dll)
#include "foo.h"
CFoo::CFoo()
{
return;
}int CFoo::foo1()
{
return 100;
}int CFoo::foo2()
{
return 200;
}CFoo foo;
main.cpp (the console application which dynamically links with the DLL using import library)
#include "foo.h"
extern CFoo foo;
int main()
{
int i = foo.foo1();
i = foo.foo2();return 0;
}
thanks in advance, George
-
Hello everyone, I am developing a DLL and making another console application to link with the DLL. The DLL expose type CFoo, create an instance and the application consumes it. There is error like this. The source code of DLL and main are listed below, any ideas?
1>main.obj : error LNK2001: unresolved external symbol "class CFoo foo" (?foo@@3VCFoo@@A)
foo.h (defines exposed type in the DLL)
class __declspec (dllexport) CFoo
{
public:CFoo(); int foo1(); int foo2();
};
dllmain.cpp (defines type implementation inside dll)
#include "foo.h"
CFoo::CFoo()
{
return;
}int CFoo::foo1()
{
return 100;
}int CFoo::foo2()
{
return 200;
}CFoo foo;
main.cpp (the console application which dynamically links with the DLL using import library)
#include "foo.h"
extern CFoo foo;
int main()
{
int i = foo.foo1();
i = foo.foo2();return 0;
}
thanks in advance, George
George_George wrote:
CFoo foo;
This is not enough, you have to export the foo variable, like __declspec (dllexport) CFoo foo;
George_George wrote:
extern CFoo foo;
Also in the exe, you have to import the variable __declspec (dllimport) CFoo foo;
-
I am confused, nbugalia! In my design, I want to create object instance in DLL and access the object instance from the hosting executable from extern reference. Your code change impacts my design... :-) Any comments? regards, George
-
George_George wrote:
CFoo foo;
This is not enough, you have to export the foo variable, like __declspec (dllexport) CFoo foo;
George_George wrote:
extern CFoo foo;
Also in the exe, you have to import the variable __declspec (dllimport) CFoo foo;
Thanks nave, I have tested your solution works. My question is, if I create a global variable inside a DLL, then in the hosting executable, could I use extern to refer to the variable and use it? regards, George
-
I am confused, nbugalia! In my design, I want to create object instance in DLL and access the object instance from the hosting executable from extern reference. Your code change impacts my design... :-) Any comments? regards, George
Here is the Dll file code - foo.h
class __declspec (dllexport) CFoo
{
public:
CFoo();
int foo1();
int foo2();
};**__declspec (dllexport) CFoo foo;re>
Foo.cpp
#include "Foo.h"
CFoo::CFoo()
{
return;
}
int CFoo::foo1()
{
return 100;
}
int CFoo::foo2()
{
return 200;
}and here is the console based application code -
Foo.hclass __declspec (dllimport) CFoo
{
public:
CFoo();
int foo1();
int foo2();
};__declspec (dllimport) CFoo foo;
test.cpp
#include "foo.h"
int main()
{
int i = foo.foo1();
i = foo.foo2();
return 0;
}Don't forget to set the dependency to Foo.lib file in the test project settings.
Hope this will help. :-D** -
Thanks nave, I have tested your solution works. My question is, if I create a global variable inside a DLL, then in the hosting executable, could I use extern to refer to the variable and use it? regards, George
-
Here is the Dll file code - foo.h
class __declspec (dllexport) CFoo
{
public:
CFoo();
int foo1();
int foo2();
};**__declspec (dllexport) CFoo foo;re>
Foo.cpp
#include "Foo.h"
CFoo::CFoo()
{
return;
}
int CFoo::foo1()
{
return 100;
}
int CFoo::foo2()
{
return 200;
}and here is the console based application code -
Foo.hclass __declspec (dllimport) CFoo
{
public:
CFoo();
int foo1();
int foo2();
};__declspec (dllimport) CFoo foo;
test.cpp
#include "foo.h"
int main()
{
int i = foo.foo1();
i = foo.foo2();
return 0;
}Don't forget to set the dependency to Foo.lib file in the test project settings.
Hope this will help. :-D**Cool, nbugalia! I want to confirm with you that we could expose, function/class/global variable from a DLL, correct? regards, George
-
George_George wrote:
could I use extern to refer to the variable and use it?
No. you should export it to use the variable in the hosting executable..
Thanks nave, So, from learning from you, it is legal to expose both function/global variable/class from a DLL? (previously, I think only expose function is allowed.) regards, George
-
Thanks nave, So, from learning from you, it is legal to expose both function/global variable/class from a DLL? (previously, I think only expose function is allowed.) regards, George
-
George_George wrote:
So, from learning from you, it is legal to expose both function/global variable/class from a DLL?
Yes its legal to expose both functions and variables.
Thanks nave, And also legal for exposing class? regards, George
-
Thanks nave, And also legal for exposing class? regards, George
-
Hello everyone, I am developing a DLL and making another console application to link with the DLL. The DLL expose type CFoo, create an instance and the application consumes it. There is error like this. The source code of DLL and main are listed below, any ideas?
1>main.obj : error LNK2001: unresolved external symbol "class CFoo foo" (?foo@@3VCFoo@@A)
foo.h (defines exposed type in the DLL)
class __declspec (dllexport) CFoo
{
public:CFoo(); int foo1(); int foo2();
};
dllmain.cpp (defines type implementation inside dll)
#include "foo.h"
CFoo::CFoo()
{
return;
}int CFoo::foo1()
{
return 100;
}int CFoo::foo2()
{
return 200;
}CFoo foo;
main.cpp (the console application which dynamically links with the DLL using import library)
#include "foo.h"
extern CFoo foo;
int main()
{
int i = foo.foo1();
i = foo.foo2();return 0;
}
thanks in advance, George
You've got two issues:
- In
main.cpp
, you want to declareCFoo
as__declspec(dllimport)
. The way I do that is to put something like this infoo.h
:#if !defined(FOO_DLL) #define FOO_EXP __declspec(dllimport) #else #define FOO_EXP __declspec(dllexport) #endif class FOO_EXP CFoo { ... };
You would defineFOO_DLL
when building the DLL using, for example,#define FOO_DLL
in every source file in the DLL before#includ
ingfoo.h
. You need do nothing inmain.cpp>/code>, as that's what the default case caters for.
- As other replies have said, you would need to export
CFoo foo;
from the DLL.
- In
-
Of course classes also :cool:
Thanks Naveen! I have proved we could expose both class and global variable by DLL and using your mentioned elegant way. :-) But I am not 100% confident. Here is my code, could you have a code review please? DLL Part:
// foo.h
#ifdef _DLLPROJ_
#define DLLEXP __declspec (dllexport)
#else
#define DLLEXP __declspec (dllimport)
#endifDLLEXP class CFoo
{
public:DLLEXP int foo1(); DLLEXP int foo2();
};
DLLEXP class CFooImpl : CFoo
{
public:DLLEXP CFooImpl(); DLLEXP int foo1(); DLLEXP int foo2();
};
// dllmain.cpp
#include "foo.h"
CFooImpl::CFooImpl()
{
return;
}int CFooImpl::foo1()
{
return 100;
}int CFooImpl::foo2()
{
return 200;
}DLLEXP CFooImpl foo;
Hosting client part
// main.cpp
#include "foo.h"
DLLEXP CFooImpl foo;
int main()
{
CFooImpl foo1;int i = foo.foo1(); i = foo.foo2(); return 0;
}
regards, George
-
You've got two issues:
- In
main.cpp
, you want to declareCFoo
as__declspec(dllimport)
. The way I do that is to put something like this infoo.h
:#if !defined(FOO_DLL) #define FOO_EXP __declspec(dllimport) #else #define FOO_EXP __declspec(dllexport) #endif class FOO_EXP CFoo { ... };
You would defineFOO_DLL
when building the DLL using, for example,#define FOO_DLL
in every source file in the DLL before#includ
ingfoo.h
. You need do nothing inmain.cpp>/code>, as that's what the default case caters for.
- As other replies have said, you would need to export
CFoo foo;
from the DLL.
Thanks Stuart, I have correct my code here, could you help to do a code review please? :-) http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2846175[^] regards, George
- In
-
Thanks Naveen! I have proved we could expose both class and global variable by DLL and using your mentioned elegant way. :-) But I am not 100% confident. Here is my code, could you have a code review please? DLL Part:
// foo.h
#ifdef _DLLPROJ_
#define DLLEXP __declspec (dllexport)
#else
#define DLLEXP __declspec (dllimport)
#endifDLLEXP class CFoo
{
public:DLLEXP int foo1(); DLLEXP int foo2();
};
DLLEXP class CFooImpl : CFoo
{
public:DLLEXP CFooImpl(); DLLEXP int foo1(); DLLEXP int foo2();
};
// dllmain.cpp
#include "foo.h"
CFooImpl::CFooImpl()
{
return;
}int CFooImpl::foo1()
{
return 100;
}int CFooImpl::foo2()
{
return 200;
}DLLEXP CFooImpl foo;
Hosting client part
// main.cpp
#include "foo.h"
DLLEXP CFooImpl foo;
int main()
{
CFooImpl foo1;int i = foo.foo1(); i = foo.foo2(); return 0;
}
regards, George
-
Thanks Naveen! I have proved we could expose both class and global variable by DLL and using your mentioned elegant way. :-) But I am not 100% confident. Here is my code, could you have a code review please? DLL Part:
// foo.h
#ifdef _DLLPROJ_
#define DLLEXP __declspec (dllexport)
#else
#define DLLEXP __declspec (dllimport)
#endifDLLEXP class CFoo
{
public:DLLEXP int foo1(); DLLEXP int foo2();
};
DLLEXP class CFooImpl : CFoo
{
public:DLLEXP CFooImpl(); DLLEXP int foo1(); DLLEXP int foo2();
};
// dllmain.cpp
#include "foo.h"
CFooImpl::CFooImpl()
{
return;
}int CFooImpl::foo1()
{
return 100;
}int CFooImpl::foo2()
{
return 200;
}DLLEXP CFooImpl foo;
Hosting client part
// main.cpp
#include "foo.h"
DLLEXP CFooImpl foo;
int main()
{
CFooImpl foo1;int i = foo.foo1(); i = foo.foo2(); return 0;
}
regards, George
Aside from the issue Naveen showed you, IIRC you don't need to DLLEXP the class and the classes methods - one or the other, that's all that's needed really. I tend to DLLEXP the methods, as you have better control over what precisely is exported.
-
I have noted the below problem
George_George wrote:
DLLEXP class CFoo
This should be modified as
class DLLEXP CFoo
everthing else seems ok.I think they should be the same? It compiles and runs well in VS. :-) regards, George
-
Aside from the issue Naveen showed you, IIRC you don't need to DLLEXP the class and the classes methods - one or the other, that's all that's needed really. I tend to DLLEXP the methods, as you have better control over what precisely is exported.
Hi Stuart, I have tried that if I only mark dllexport on class and not on methods, the methods could not be found during link process. So, I think we have to mark both. Could you have a try please? regards, George
-
Hi Stuart, I have tried that if I only mark dllexport on class and not on methods, the methods could not be found during link process. So, I think we have to mark both. Could you have a try please? regards, George
I have an awful lot of deployed code that uses this mechanism - here's a sample:
#if!defined(COMMONLIB_API) #ifdef COMMONLIB_EXPORTS #define COMMONLIB_API __declspec(dllexport) #else #define COMMONLIB_API __declspec(dllimport) #endif #endif namespace MyAppLibrary { class MessageReporter { public: //ctor COMMONLIB_API MessageReporter(); //Add new reporting function COMMONLIB_API static void AddReportFunction(ReportFnPtr func); //Set MessageReporter Level on or off COMMONLIB_API static void SetReporterLevel(Level lev, bool on); //Set all levels to on COMMONLIB_API static void AllLevelsOn() {repLevels=~0;} //Set all levels to off COMMONLIB_API static void AllLevelsOff() {repLevels=0;} }; };
Is it possible you haven't exported necessary constructors?