HOWTO: Export class from static library.

Started by
2 comments, last by HaywireGuy 18 years, 7 months ago
Hi people, I'm sorry I have tried searching the forum but did not find any answers to my question. So here I am, with the question... This is how my static library looks like, simple and there's no compilation or linker errors in it. _EXPORT has been defined only in the static lib project, not in the client (EXE) project.

#ifndef _EXPORT
#define _CLASS_TYPE __declspec(dllimport)
#else
#define _CLASS_TYPE __declspec(dllexport)
#endif
 
class _CLASS_TYPE CMyClass
{
    public:

        CMyClass();
        ~CMyClass();
};
 
void SomeFunction(void);

When I tried to use "SomeFunction" in the client, it links fine and things work well. There are linking problems if I were to do a "CMyClass a;", complaining about unresolved class constructor/destructor. I have checked both project settings, including threading mode (both Multithreaded Debug DLL). The only difference I noticed was static library built with "Use Standard Windows Libraries", while the client "Use MFC in a Shared DLL". Is that the cause of the problem? Thanks in advance for any help! [Edited by - HaywireGuy on August 28, 2005 3:03:42 AM]
Advertisement
What I do with my static lib(math)is simply create it as a static library project, build the project, take the .lib generated move it to a libs folder I have setup, this folder is setup as an additional place to look for libs in the IDE and I simply #include the headers in the .exe project and it works fine. I think __declspec(dll...) is for importing/exporting classes, etc to a DLL... if this is being built as a static lib it should be fine if your linker is finding the .lib you generated when building the .exe. Im thinking your last sentence could cause problems, try making them the same.
Thanks NumberXaero, yah I agree the MFC thing looks very suspicious. In that
case I might need to make a new project with MFC settings, if that's necessary.
I'll give that a try tomorrow and keep you posted. Thanks again.
Yes! I got it to work, with two changes:

1. Change "Use Standard Windows Libraries" to "Use MFC in a Shared DLL".
2. Remove __declspec(dllimport) and __declspec(dllexport) because this is a static library.


Thanks again NumberXaero!

This topic is closed to new replies.

Advertisement