creating and using a DLL?

Jul 12, 2011 at 4:54pm
Okay so lets say in an example,
I have a.CPP file and a.H file like so,
a.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef A
//includes
namespace A
{
 class A
 {
  //variables and prototypes
 };
 namespace AB
 {
  class B
  {
   //variables and prototypes
  };
  class AB : A, B
  {
   //variables and prototypes
  };
};
};

#endif 

a.cpp
1
2
3
4
5
6
7
8
9
#include "a.h"
namespace A
{
 //A's functions and operators
 namespace AB
 {
 //B's and AB's functions and operators
 };
};


Do I need to do anything to get them to compile to a DLL Library?
And how would I call them from the library?

Thanks in advanced,
Bombshell
Jul 12, 2011 at 5:43pm
closed account (zb0S216C)
You may need to export what ever you'll be using externally of the DLL. You can export items with __declspec( dllexport )[1].

References:
[1]http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx

Wazzak
Jul 12, 2011 at 6:10pm
To call them from another application, you could load the DLL file using LoadLibrary() and get a pointer to the function using GetProcAddress(). Then you would have to surround the code in the DLL file in a extern "C" block to prevent name scrambling.

However, there is a simpler way to go about things. When you build your DLL, you should also get an import library (with a .lib or .a extension, I believe). You can link to this as if it were a static library to provide access to the content of the DLL files. However, you have to take what Framework said a little further. The DLL must have all its symbols marked as __declspec(dllexport), but the same symbols must be marked as __declspec(dllimport) in the project using the DLL. I answered a previous thread on this topic and I'll try and find it now so you can see the details.

[edit] Here it is: http://www.cplusplus.com/forum/beginner/45745/
Last edited on Jul 12, 2011 at 6:12pm
Jul 12, 2011 at 7:08pm
__declspec(dllexport) will cause name mangling. Also, extern "C" is not needed, and linking the .lib is completely overkill. All you need is a .def file that looks like this:
EXPORTS
	MyFunc		@1
	MyOtherFunc	@2
	AnotherFunc	@3
	SomeVariable	@4
	SomethingElse	@5

And then tell your linker to use the .def file in the project properties. If you use VC++ you can do it like this picture:
http://www.LB-Stuff.com/C++/DLL_export.png

A nice example of how to dynamically link a DLL can be found in the thread where I asked the same thing:
http://www.cplusplus.com/forum/windows/46375/#msg252003
Last edited on Jul 12, 2011 at 7:10pm
Jul 12, 2011 at 8:29pm
so I compile my .cpp and .h to .DLL and make a .def file to allow organised loading?
would this allow me to make a framework? allowing me to organize the classes and functions through namespaces for ease of use?
Slightly related, I'm a graphics programmer (rather new to C++ used to use C#) and my current goal is to create a number of classes and to simplify the loading, storing, modification and drawing of models and images.
This being the case I will have a large number of classes, structs (I use C# syntax of class for function heavy, struct for data storage) and functions. So organizing them via namespaces is rather important.

EDIT: MY MISTAKE!
I read up on def files a bit,
So I compile my cpp and h files using a .def file which will compile to DLL with a lib file.
I'd still like to know about if this would allow me to organize with namespaces

Thanks,
Bombshell
Last edited on Jul 12, 2011 at 9:02pm
Jul 12, 2011 at 11:08pm
Have you heard of SFML? ;)
Also, classes and structs and unions are all exactly the same in C++ except that classes start off with private access by default, structs and unions start public, and all data members in a union share the same memory.

If you don't want to dynamically link the DLL like I did then you would indeed need to statically link with the .lib file and the header file to use namespaces, etc.
Topic archived. No new replies allowed.