-
May 10th, 2004, 04:19 AM
#1
how to export an class in a regular DLL?
I have a regular dll using MFC as shared library and it exports some API functions.
Now i want it to export an class. But i met some troubles.
It seems that the constructor and destructor of the exported class cannot coexist with the virtual functions.
If both of them exist in the class, the compiler generate an link error says:
error LNK2019: unresolved external symbol "__declspec(dllimport) const CXXXX::`vftable'" (__imp_??_7CXXXX@@6B@) referenced in function "public: __thiscall CXXXX::CXXXX(void)" (??0CXXXX@@QAE@XZ)
if i delete the constructor and the destructor, it's OK.
or if i delete the virtual functions. it's OK, too.
what's the problem? how to solve it?
thank you!!!
-
May 10th, 2004, 04:42 AM
#2
Is it that you're just compiling the dll or is it linked with someother exe project where you;re using that dll? If so, import it in the exe project and export it in dll project.
you know what i mean?
-
May 10th, 2004, 05:10 AM
#3
ya, i know what u mean..
but the problem is not like what u'r talking about.
the problem occurs when the DLL is compiled..
-
May 10th, 2004, 05:44 AM
#4
"delete construct destruct func
or delete virtual func" is ok?
then a c++ class can be exported with using regular dll?
i used to think only the ext dll can export a c++ class;
there's some problem of sth like func name trans or linking
when using other methods ?
am i wrong?
Last edited by FirstPerson; May 10th, 2004 at 05:47 AM.
-
May 10th, 2004, 06:02 AM
#5
If both of them exist in the class, the compiler generate an link error says:
error LNK2019: unresolved external symbol "__declspec(dllimport) const CXXXX::`vftable'" (__imp_??_7CXXXX@@6B@) referenced in function "public: __thiscall CXXXX::CXXXX(void)" (??0CXXXX@@QAE@XZ)
if you're compiling dll, from the above error, you seem to be importing the class. shouldn't you be exporting. i'm sorry if i'm barking at the wrong tree.
-
May 10th, 2004, 06:15 AM
#6
FirstPerson, please see the MSDN at
TN011: Using MFC as Part of a DLL
here the words from that note.
Regular DLLs assume interfaces between the application and the DLL are specified in normal C-like functions or explicitly exported classes. MFC class interfaces cannot be exported.
if you're compiling dll, from the above error, you seem to be importing the class. shouldn't you be exporting. i'm sorry if i'm barking at the wrong tree.
But the class is in this DLL. it's very simple to test what i described. Create a regular DLL which VC provides the simplese one. and add the following class:
Code:
.h file
class AFX_EXT_CLASS CTest{
CTest();
virtual void f();
}
.cpp file
CTest::CTest()
{
}
void CTest::f()
{
}
-
May 10th, 2004, 06:18 AM
#7
Well I am not sure if u link dynamically (not even using the lib) u can export a class just by export keyword. The way I do when i have to export a class outof a DLL (the dll will linked dynamically) I export two functions
1. First that returns a pointer to the class Obj after newing it.
2. And another function that takes the pointer to that class and deletes it.
And I declare all the functions of that class to be virtual. Another advantage is that memory allocation/deleting is handled within the same module.
Hope this helps,
Usman.
-
May 10th, 2004, 07:23 AM
#8
But the class is in this DLL. it's very simple to test what i described. Create a regular DLL which VC provides the simplese one. and add the following class:
Well, it doesn't matter where the class is. It all depends on where the header file is being compiled from.
if you also have another project in the same workspace as the dll is, then make sure that you've not forgoten to make your main project a dependent of dll. See "Project->Dependencies" menu.
If you have just DLL in your work space and you're trying to compile just that, then probably you might not have _AFXEXT #defined. If so, it would have AFX_EXT_CLASS would haev meant export (not import). If this is the case, add _AFXEXT in Pre-processor definitions of C++ tab in projects settings.
-
May 10th, 2004, 07:39 AM
#9
thanks, Indian_Techie..
u're right.
AFX_EXT_CLASS was defined as export only if _AFX_EXT was defined..
ok, i will use
__declspec(dllexport)
directly..
thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|