DLL export and inheritance in C++

删除回忆录丶 提交于 2021-02-08 15:27:19

问题


I'm trying to export a class and its base class from a DLL like this:

#ifdef MY_EXPORTS
    #define DECLSPEC_TEST __declspec(dllexport)
#else
    #define DECLSPEC_TEST __declspec(dllimport)
#endif


class DECLSPEC_TEST BaseClass
{
  // stuff.
};

class DECLSPEC_TEST DerivedClass : public BaseClass
{
  // This class only has a constructor which initializes the class differently.

};

But I try to use this class in another DLL, I keep getting an error:

error LNK2019: unresolved external symbol 
"__declspec(dllimport) public: __thiscall DerivedClass::DerivedClass(void)"
 (__imp_??0DerivedClass@@QAE@XZ) referenced in function 
"public: __thiscall SomeOtherClass::SomeOtherClass(void)" (??0SomeOtherClass@@QAE@XZ)  

I also looked at my exporting DLL with PE Explorer and I can't see the derived class in the exports list.

When I try to use the base class in my other DLL it works fine.

What am I doing wrong?


回答1:


there are two ways to load the DLL. The first is to reference one or more symbols from the DLL (your classname, for example), supply an appropriate import .LIB and let the linker figure everything out.

The second is to explicitly load the DLL via LoadLibrary.

Either approach works fine for C-level function exports. You can either let the linker handle it or call GetProcAddress as you noted.

But when it comes to exported classes, typically only the first approach is used, i.e., implicitly link to the DLL.




回答2:


Ok, I don't know how to explain this but I put the implementation of the derived class' constructor in a CPP file instead of within the class definition and the error went away...
thanks everybody :)




回答3:


The linker is complaining that it can't find the constructor for DerivedClass. Try explicitly declaring it, defining it, and exporting it from your DLL:

Header file for DLL:

class DECLSPEC_TEST BaseClass
{
};

class DECLSPEC_TEST DerivedClass : public BaseClass
{
public:
    DerivedClass();
};

Source cpp file for DLL:

DECLSPEC_TEST DerivedClass::DerivedClass() { }



回答4:


When you compiled your DLL, you also got a .lib file. This is your import library. You will need to link to it in order to connect to link-time dependencies like these.




回答5:


Few things to check:

  1. Ensure you are linking with the EXACT .LIB file you are producing from DLL project. It happens, by mistake that we build both projects but LIB file in target project is obsolete.
  2. Ensure that associated CPP files, in DLL project, gets compiled.Using __declspec attribute on all methods is not required, since you are exporting entire class.
  3. Using Dependency Walker, ensure that DLL is actually having class and all its methods exported.
  4. Ensure that there is no 64-bit/32-bit mismatch among .LIB files in both projects



回答6:


The only missing thing here that I see is a semicolon ; after the class declarations. Other than that, the export should work fine.

One more thing to check: Did you declare the constructor in the derived class to be public?




回答7:


As far as I understand, in your DerivedClass you have declared a non-default constructor (i.e., different from DerivedClass::DerivedClass()).

In such cases the compiler will not produce a default constructor for you, so you have to define one, since it seems that for some reason you are using the default constructor of your DerivedClass (possibly in the implementation of DerivedClass, not necessarily in the code that uses the DLL).

It is not necessary that the default constructor be declared in the DLL interface, but it should be there.

If you are not willing to have a default constructor, a trick is the following:

declare a default constructor in the private part of DerivedClass definition.

In this way, any attempt to use it will produce a compiler error (instead of a linker error), so that you know where you are trying to use the default constructor and can fix that.



来源:https://stackoverflow.com/questions/6633471/dll-export-and-inheritance-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!