954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

DLL Easy Way To Make

Well I want to make a DLL but all of the tutorials online seem to focus tword GUI programing or go a little too fast. Can anyone explain it? As far as I have seen it's syntax resembles resource files. And what makes it a 'Dynamic Link Libray' as in what makes it dynamic?
Oh I want to use this for console app.

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
 

First of all, this thread should answer all your questions about what a DLL is and what is meant by "dynamic-link".

Now, for a simple example of a DLL, here is the simplest example I can think of, the "hello world" program:

In hello_world.h:

#ifndef HELLO_WORLD_H
#define HELLO_WORLD_H

#ifdef COMPILING_DLL
#define DLL_FUNCTION extern "C" __declspec(dllexport)
#else
#define DLL_FUNCTION extern "C" __declspec(dllimport)
#endif

DLL_FUNCTION void printHelloWorld();

#endif


In hello_world.cpp:

#define COMPILING_DLL

#include "hello_world.h"
#include <iostream>

DLL_FUNCTION void printHelloWorld() {
  std::cout << "Hello World!" << std::endl;
};


In main.cpp:

#include "hello_world.h"

int main() {
  printHelloWorld();
  return 0;
};


Now, you must compile the "hello_world.cpp" as a DLL and then compile the "main.cpp" as an executable, linking it with the import library of the DLL.

If you are using MinGW GCC, then you can do this:

g++ hello_world.cpp -shared -o hello_world.dll --out-implib libhello_world.a
g++ main.c -o main.exe -lhello_world


If you are using MSVC, then you should just create one "DLL" project with the "hello_world.cpp" file as the source file. And then, create one executable project with the main.cpp file. You need to first compile the DLL project (as "hello_world.dll"), then add the "hello_world.lib" to your link-libraries in your executable's project settings (build options), and then compile the executable. Or, equivalently in command-line:

cl -o hello_world.dll hello_world.cpp /link /DLL
cl -o main.exe main.cpp hello_world.lib


I'm pretty sure the above will work.

mike_2000_17
Posting Virtuoso
Moderator
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

One last tiny question: Does

using namespace std;


work in a DLL? I have heard that there are certin conditions where it can't be used.

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
 

It works anywhere, but it _should_ not be used in a header file. The motivation is essentially that you don't want it to have repercussions on the code that includes that header file (i.e. the user of the header file might not want to import the std namespace, so you should never do it in a header file). So, the guideline is that if you want to use "using namespace std;" in your code, you have to restrict it only to cpp files, and thus, in the header files, you have to use the full qualifications like "std::string" or "std::cout".

mike_2000_17
Posting Virtuoso
Moderator
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: