I was wondering how to have a class in a dll and then load the dll or just link to it through the lib file created. When I just use functions, everything works OK because MSVC++ gives me a lib file I can include in main exe, but when I use a class, it doesn''t work this way. I get no lib file. What''s going on?
I have my functions like this:
If you are using MSVC++ create a new dll project. In the wizard select the option “Export some symbols”. This will create some example code that exports a variable a function and a class. Hope this helps
TheBartman''s solution will work fine, with one problem: you will only be able to use the class with the same compiler that created the DLL. If you are the only person likely to use it, that shouldn''t be a problem. However, if you want other compilers to be able to use your class, then you have to do the following:
To export the following class:
class Stuff { public: void NumberList(); void LetterList(); };
Do the following: (1) Create the following class: class StuffInterface { public: virtual void NumberList() = 0; virtual void LetterList() = 0; };
(2) Then write Stuff, making sure it is derived from StuffInterface.
(3) Create a function: extern "C" __declspec(dllexport) StuffInterface* CreateStuff() { return new Stuff; }
Now, when you want to use Stuff, instead use StuffInterface*
Reason: The compiler gives any C++ function, including member functions, a horrific decorated name, specific to the compiler you are using(E.g: Stuff::NumberList will become something like ?NumberList@Stuff@@FJFA3FZ ). Any function that you want to use from another compiler must be declared as extern "C". Member functions cannot be declared in this way. However, pure virtual functions in abstract classes are only referenced as in offset into the virtual function table. Hence, the name is immaterial.
sbennett is correct, my solution may not work on all compilers, but I assumed you were using MSVC++. If you aren''t using Visual C++, then I might not be able to help you because I only know MSVC++. If you still need help, I will help you out just email me.
Michael Bartman Dark Omen Studios Lead Engine Programmer
Michael BartmanDark Omen StudiosLead Engine Programmer