Author Topic: Object Library  (Read 3595 times)

Stucov

  • Guest
Object Library
« on: February 24, 2007, 08:43:57 pm »
Hey,
is it possible (and if yes how) to make an object library in Code::Blocks.

Or is there a way to make a class into a library.

thank you

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Object Library
« Reply #1 on: February 25, 2007, 03:22:13 pm »
I'm not sure what exactly you mean.

If you are talking of "object library" as in COM (or similar) then yes of course this is possible. You only need to do the proper raindance for Windows.
I've never done this, but I see no reason why you should be unable to do so with Code::Blocks - there isn't really anything special the IDE has to do, it's just an API you have to use properly (look at MSDN how to do it).

If you are talking of "a way to make a class into a library" and "object library" as in "can I put a class into a library and link against that one?" then yes, this sure is possible too, and very easy.
Again, this does not demand anything particularly special from the IDE. You just make a target for your library, and one for your program that uses it, and that's it (or, you can make two separate projects).

There are generally two pitfalls to watch for when talking of libraries and objects.
The first one is that "objects" usually means "C++" and you have to watch out not to mix C and C++ programs and libraries, this will end miserably. In that case you'll have to make sure the same calling conventions are used everywhere, for example using extern "C" and wrapper functions.
The second one is you cannot mix programs and libraries made with different C++ compilers (even different major versions of the same compiler, or different platforms, such as Intel32 and AMD64), as they have different calling conventions, as well. There is no workaround for this.

A simple example of how to create a library containing a class is attached. The main program creates an object and calls a member function. The object lives in a dynamic library and prints a line of text.
At least with gcc, this works without any additional magic incantations (should work on every other decent compiler too, but I haven't tested).

[attachment deleted by admin]
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Alturin

  • Guest
Re: Object Library
« Reply #2 on: February 25, 2007, 04:26:47 pm »
Thank you for this easy to understand example! :)
I wondered how C::B did all that stuff with DLL's and whatnot, your example made it clear ^_^.

Thanks!

Stucov

  • Guest
Re: Object Library
« Reply #3 on: February 25, 2007, 08:11:06 pm »
Thanks so much, the example provided was very useful, and simple.
Now I have one more fallow up question:
 Is it possible to have the library loaded at run time? If yes how?

I presume you can still use LoadLibrary and GetProcAddress ( and what will u use to declare a class and use its member).

this is an example of what i want to do:

Code
int main()
{
HInstance hDll;
/* */
hDll = LoadLibrary("my.dll");

//declare object contained in library
MyObj obj; //<--- how will you declare this if its in the library

obj.Print("HEY"); //call a member function of MyObj
/**/
}
Thanks. You have been extremely helpful
:D

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Object Library
« Reply #4 on: February 26, 2007, 10:44:09 am »
I presume you can still use LoadLibrary and GetProcAddress ( and what will u use to declare a class and use its member).
In general yes, although it gets a lot more complicated.

You'll have to properly dllexport your class (via __declspec or __attribute__ or a respective macro from windows.h), you have to account for name mangling, and you have to create your objects on the heap.
GetProcAddress returns a pointer to a symbol (or null if it does not find anything), not more, not less. It does not know about name mangling or classes (nor care about), and it does not allocate anything. Simply tossing an object onto the stack no longer works now.

There many solutions to these problems (none of them is entirely trivial or pretty), among them initialising pointers in the DLL's init procedure, using C wrapper functions, using static factory objects, or passing around constructor/destructor pointers (this is done in Code::Blocks).

But, it can be done :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."