Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Making Dlls with classes in them

Author
Message
Kryogenik
15
Years of Service
User Offline
Joined: 22nd Sep 2009
Location: Heidelberg, Germany
Posted: 22nd Jul 2010 04:39
Hey guys, I have a question. I learned how to make dlls from a microsoft tutorial, using static __declspec, and then putting the function prototype after it in the header file, then putting the defintions in the .cpp. And with simple math stuff like the tutorial showed me, it works fine. But I heard that using this method would only make the .dll usable in microsoft visual c++ 2008 compiled code, which would kinda suck if I wanted to use the dll in DBPro or something. Plus, what do you do fo the class constructor and deconstructor, or do you not even need one? What do I need to do to make the .dll usable with exe's compiled with other programs? The whole thing is confusing me. I also heard that memory leaks can occur if you don't do something right. What causes a memory leak, and what exactly is a memory leak? Thanks in advance

cout<<"I'm learning C++, and this is all I know \n"
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 22nd Jul 2010 05:56 Edited at: 22nd Jul 2010 06:15
Wow! Lot's of bouncing around here - in the future, post only one question at a time (or at least keep them related)... Memory leaks - what they are, what causes them is totally different from DLL linkage...

Anyway, I'll see what I can do. So, you already know that in order to export a function (or class etc.) from a DLL, you need to export it. Furthermore, you already know that to use that function (or class etc.) you need to import it into your application. The following code snippet allows you to incorporate import/export functionality depending upon how you are #including the file:



Again, you already know the difference between the two from the tutorial, right? Anyway, this snippet essentially checks for a previous #define JTK_BUILD_DLL (implying the need to export) and JTK_USEAS_DLL (implying the need to import). Only #define JTK_USEAS_DLL and JTK_BUILD_DLL, or just JTK_USEAS_DLL or neither.

All this does is simplifies things for you by letting the pre-compiler determine which variety of function (or class) that you need.

Now, as for VS2008 specifics: Yes, there can be problems with this. When you want to export a function, you need to declare that function as extern "C" or you get the Microsoft compiler (and potentially version) specific variety. Not usually an ideal solution. Generally speaking, when I know I want to export several functions, I'll enclose the whole group as such:



NOTE the JTK_API (see above snippet for its definition). When compiling the DLL, JTK_API will declare them as exportable; whereas when compiling an application that uses the DLL, it declares them as importable. By enclosing them in the extern "C" block, the functions are declared with C linkage (as opposed to C++ linkage) which makes them more compiler independent. C++ linkage (the default) is compiler/version specific; hence, the reason you've been hearing such things.

In addition, different compilers (read: vendors) still have different naming conventions - even for so-called standards. Thus the reason for the #ifdef _MSC_VER / __BORLAND__ checks above. Even different versions of the same vendor's compilers may be different!

NOTE: I don't believe that a class can be "imported" by DBP even if it were declared as JTK_API (in the example above). I could be wrong on this point, however...

NOW: as for the memory leaks - I give a pretty decent description of what causes *some* (especially in regards to GDK) memory leaks (and why) here. A simplified description of a memory leak is this: memory was allocated (for some reason) by the operating system; but never released back to the operating system for later use...

I hope that helps you in understanding some of your questions - or at least gives you a bit more to go on...

JTK

PS: In case anyone is curious, no; the letters JTK are not my initials... :p - In reality, they are the initials for my Tool-Kit (Just-a-Tool-Kit)... Thought you'd like that bit of info!


EDIT: corrected error in logic description regarding JTK_USEAS_DLL/JTK_BUILD_DLL... Sorry for any inconveniances...
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 22nd Jul 2010 07:02
So what would you use dlls for? Is it just a way of I guess exporting your code to a different platform or language? Or how are they used in the game industry?
jamesL
14
Years of Service
User Offline
Joined: 31st May 2010
Location:
Posted: 28th Jul 2010 09:40
dlls can be used to make "librarys"

so if you come up with a bunch of classes and functions to do physics then you could compile them into a dll

you could then sell or give away your dll and then game makers wouldn't have to go through the trouble of making their own physics routines. They could just use yours.

there could be dlls for databases, searching and sorting, drawing, sound, all kinds of things
Unseen Machine
14
Years of Service
User Offline
Joined: 23rd Jul 2010
Location:
Posted: 1st Aug 2010 13:14
Hence the name .DLL = Dynamic Link Library. There are loads for C++ allready, a lot are free and vary as allready stated, from libraries of databse functions to sound playing functions to loading .x models.

If you make one, show me please...i would love to see.

Login to post a reply

Server time is: 2024-11-19 20:57:48
Your offset time is: 2024-11-19 20:57:48