Jump to content
  • Advertisement
Sign in to follow this  
trentfaris242

Derived Class Constructor from DLL

This topic is 990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all,

 

I'm new to creating DLL's in C++. My project is to create a game engine API (not a game) porting some code from another project I did, and then building a small demo on top of this API. Anyways, I'm having trouble with polymorphism where the base class is located in the DLL and the derived class in the implementing program. Let me show some code to explain.

class Base {
public:
    Base(const int x);

protected:
    int x;
};

Base::Base(const int x) {
    this->x = x;
}

class Derived : public Base {
public:
    Derived(const int x);
};

Derived::Derived(const int x) : Base(x) {}

This is pretty standard polymorphism, as I understand it. However, in this case the Base class is located in a DLL, and the program implementing the DLL creates the Derived class. This behavior gives me the classic "LNK2019 ..." linker error. Am I missing some piece of information about polymorphism across DLLs?

Share this post


Link to post
Share on other sites
Advertisement
This won't work that easily. Take a look at this. There are other ways to deal with the issues involved but especially if classes are involved this is probably closest to what you need.

Edit: I'd also like to point out that working with DLLs, especially if C++ is involved, has a lot of gotchas and restrictions. I would not do it unless I had a very good reason to do it. Edited by BitMaster

Share this post


Link to post
Share on other sites

This won't work that easily. Take a look at this. There are other ways to deal with the issues involved but especially if classes are involved this is probably closest to what you need.

Edit: I'd also like to point out that working with DLLs, especially if C++ is involved, has a lot of gotchas and restrictions. I would not do it unless I had a very good reason to do it.

This was exactly the problem. I was forgetting to export the class so that the linker could access it.

 

Don't worry, this project is completely academic with the purpose of simply trying to learn more. I'm not trying to make a product to release or anything like that. Thanks for the help!

Edited by trentfaris242

Share this post


Link to post
Share on other sites
While not an issue for your academic project, keep in mind that C++ code in DLLs can only be used by clients built with the exact same compiler and version of the compiler due to C++ not having a defined binary interface. I.e. you can't use C++ objects exported by a DLL compiled in MSVC2012 in a program compiled with Clang - or even a program compiled in MSVC2013.

If you need to support such a situation, you'll have to look into making a pure C interface to your DLL, potentially wrapping it in COM or something similar if you want object-oriented behavior. smile.png

Share this post


Link to post
Share on other sites

While not an issue for your academic project, keep in mind that C++ code in DLLs can only be used by clients built with the exact same compiler and version of the compiler due to C++ not having a defined binary interface. I.e. you can't use C++ objects exported by a DLL compiled in MSVC2012 in a program compiled with Clang - or even a program compiled in MSVC2013.If you need to support such a situation, you'll have to look into making a pure C interface to your DLL, potentially wrapping it in COM or something similar if you want object-oriented behavior. :)


Thanks for the advice! I may have to research this some more, but my intentions for the project may never lead me there.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!