dll

Printer-friendly version

HOWTO Create and Deploy a Sample DLL using MinGW

A sample DLL

The DLL we will build will consist of a single source file "example_dll.cpp":

#include <stdio.h>
#include "example_dll.h"

__stdcall void hello(const char *s)
{
        printf("Hello %s\n", s);
}
int Double(int x)
{
        return 2 * x;
}
void CppFunc(void)
{
        puts("CppFunc");
}
void MyClass::func(void)
{
        puts("MyClass.func()");
}

The following header "example_dll.h" declares the interface of the DLL, and is used both when building the DLL and when building an executable that uses the DLL:

<pre>

  1. ifndef EXAMPLE_DLL_H

HOWTO Create an Import Library for a DLL using MinGW

Usually (read: for all DLLs created with MinGW and also a few others) MinGW links fine against a DLL. No import library is necessary (see sampleDLL).

However there are situations when it won't. Then one needs a so-called import library to help the linker. The rest of this page gives hints as to how to create such import libraries when there is only the DLL available.

MinGW comes with a handy tool which does most of the work for you:

dlltool.exe (or i586-mingw32-dlltool or something similar). Issue 'dlltool --help' to get a brief description of the available options.

MSVC and MinGW DLLs

TODO: Reformat to new wiki syntax.

!!! [Minimalist GNU for Windows | http://www.mingw.org]

!! MSVC and MinGW DLLs

Assume we have a testdll.h, testdll.c, and testmain.c. In the first case, we will compile testdll.c with MinGW, and let the MSVC-compiled testmain call it. You should use

gcc -shared -o testdll.dll testdll.c -Wl,--output-def,testdll.def,--out-implib,libtestdll.a

to produce the DLL and DEF files. MSVC cannot use the MinGW library, but since you have already the DEF file you may easily produce one by the Microsoft LIB tool:

<pre>