Is it possible to put a c++ class into a windows dll and use it outside of the dll? If it is possible, how?
Printable View
Is it possible to put a c++ class into a windows dll and use it outside of the dll? If it is possible, how?
It is possible by using __declspec(dllexport) or __declspec(dllimport) respectively. But a dll created with e.g. VC++ can only be used by VC++ again.
You must use implicit linking for this (list the .lib file that is generated during compilation of the DLL in the import library list of the project where you want to use the class).
Or just create a static library and use the pragma in the file or project that uses it, like this:
on MSVC++PHP Code:#pragma comment(lib, "library.lib")
Put that pragma into the class header file, that's the way MFC does it, it's quite useful.
but wouldnt that be a problem, i mean when you are compiling the the .lib? hmm, i do that too but I dont understand it very well, so to be safe, I started putting it in the .cpp of my main file. I guess I was right at first, I didnt see this in MFC or anywhere, I wanted a way to make sure a lib file was included automatically when you #include a header, so I searched google and found that MSVC has that pragma, and I was like WOWWWW... lol. But that tip didnt say where to put it, so I experimented and it was like everything worked but there where some weird things going on. So which way is the safe and correct way? The way MFC does it or the way I did it, and do you know of a pragma tutorial?
Thanks,
MoMad
Since you usually have a special symbol defined when you compile a dll (that turns all DLLAPI or whatever macros into __declspec(dllexport) instead of import etc.) you can say
#ifdef DLL_INTERNAL_COMPILE
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#pragma comment(lib, "mylib.lib")
#endif
Put it in some central header file that it is included by each and every file of your dll.
O cool, but I never use the pragma with the dll, I almost always use dynamic linking (LoadLibrary) when dealing with dynamic link libraries, I only use the pragma with the static link libraries.
#ifndef LIB_INTERNAL_COMPILE
#pragma comment(lib, "mylib.lib")
#endif
Huh? WHats LIB_INTERNAL_COMPILE??Quote:
Originally posted by CornedBee
#ifndef LIB_INTERNAL_COMPILE
#pragma comment(lib, "mylib.lib")
#endif
A symbol you define via a /d switch when compiling your static library. The name is up to you of course.