You can not hide the class declaration from the application program, but you can put the code that implement the class methods into a library so that the implementation code is hidden from the application program.
There are two kinds of libraries: static and dynamic. Static libraries have *.lib extension and are linked into the application program when the program is compiled and linked. This takes up the most disk space because each application program has a complete copy of the code it uses in the library.
Dynamic libraries on MS-Windows have *.DLL extension. When you compile a DLL the compiler will also generate a *.lib file that only contains the information compilers need to resolve function and data address while compiling the application program. There is only one copy of the code in memory regardless of how many application programs use it, so DLLs require the least amount of memory.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
So it seems like I should use a DLL, but how do I use a DLL?
That's operating system specific...Which operating system are we talking about here?
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
So it seems like I should use a DLL, but how do I use a DLL?
Both static libraries and DLLs are used alike from the application program point of view. They both link to a *.lib file. To use a DLL all you have to do is copy it to one of the folders in the PATH environment variable or in the folder where the application *.exe is located.
If you do not have the *.lib file for a given DLL there is another way to link which does not require the *.lib file. Check out LoadLibrary () and GetProcAddress ()
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
So how exactly would I write a dll and implementation file for the three blocks of code in my first post in this thread?
For exporting classes, it is better to create and destroy objects in DLL itself to avoid compiler crash (if main app is compiled differently from DLL(s)). So export method for creating object and destroying objects in DLL as C-style.
something like:
class Library{
//all methods goes here
}
extern "C"{
Library* createObject(){
return new Library();
}
destroyObject(Library* lib){
delete lib;
lib=NULL;
}
}
Check MSDN entry on making DLLs
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
Exporting a class is something that is pretty hard to do correctly. I don't recommend you try it. In general, I have not seen anyone do it on Windows (in *nix systems it's a different story). Generally, the idea is to export a factory function (and the corresponding destroying function) to handle the create/destroy of the object, and then make all member functions 'virtual' such that you get all the member functions exported for free. But, even when doing that, you still need to worry about a lot of ABI issues (ABI: Application Binary Interface). This is not an easy task (it literally took me 3 years to figure it out for my own purposes, as a little side project, of course).
Read this article , it's a pretty good start to understanding the issues involved.
The easiest solution, really, is to use only a C-style interface for your DLL. With that, your code could look like this:
In the test.h for both the DLL and the application:
#ifdef WIN32
#ifdef COMPILING_THE_LIBRARY
#define DLLFUNCTION __declspec(dllexport) __stdcall
#else
#define DLLFUNCTION __declspec(dllimport) __stdcall
#endif
#else //for *nix systems (no need for anything special)
#define DLLFUNCTION
#endif
class test; //forward-declaration.
extern "C" {
test* DLLFUNCTION CreateTest(int aA);
void DLLFUNCTION DestroyTest(test* t);
int DLLFUNCTION TestNum(Test* t);
};
In the test.cpp for the DLL:
#include "test.h"
class test
{
private:
int a;
public:
test(int a);
int num();
};
test::test(int aA) : a(aA) { }
int test::num()
{
return a;
};
extern "C" {
test* DLLFUNCTION CreateTest(int aA) {
return new test(aA);
};
void DLLFUNCTION DestroyTest(test* t) {
delete t;
};
int DLLFUNCTION TestNum(Test* t);
return t->num();
};
};
And, in your application:
#include "test.h"
#include <iostream>
using namespace std;
int main()
{
test* tst = CreateTest(5);
cout << TestNum(tst);
DestroyTest(tst);
return 0;
}
mike_2000_17
Posting Virtuoso
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457