A Moron's Guide to Creating and Using Your Own Cygwin or Mingw DLL's
when Compiling Cygwin or Mingw Programs


This tutorial explains how to create a DLL using Cygwin or Mingw, and then link the library routines in the DLL to your Cygwin or Mingw programs. The information is derived from the very informative Mingw documentation in the Compiling and Building section and applies equally well to Cygwin.

Our problem can be divided into three parts:

As an example of how to solve our problem, let us create a DLL called mydll.dll which has two functions, equ and func2. We first create a file, mydll.c, which contains the two functions written in C. There are two simple things we need to do to polish off mydll.c.

In our example, mydll.c looks as follows:

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

EXPORT int equ( char *str1, char *str2 )
{
  if ((str1 == NULL)&&(str2 == NULL)) return (1);
  if ((str1 == NULL)&&(str2 != NULL)) return (0);
  if ((str2 == NULL)&&(str1 != NULL)) return (0);
  // Above since strcmp seems to have trouble operating on null strings.
  if  (strcmp( str1, str2 ) == 0) return (1);
  else return (0);
}

EXPORT char *func2(char *s1 )
{
  
  int i,k,dun;
  char *pos[80], *def, s3[80];
  
  strcpy( s3, s1);
  ....
  ....
}

Mydll_dll.h is an improved version of the "dllfct.h" file shown in the Mingw documentation. Just as dllfct.h needed to be taylored to the particular functions in the DLL, so does mydll_dll.h. In the example here, mydll_dll.h looks as follows:

#ifdef __cplusplus
     #define cppfudge "C"
#else
     #define cppfudge
#endif

#ifdef BUILD_DLL
     // the dll exports
     #define EXPORT __declspec(dllexport)
#else
     // the exe imports
     #define EXPORT extern cppfudge  __declspec(dllimport)
#endif

EXPORT int equ( char *str1, char *str2 ) ;
EXPORT char *func2(char *s1 ) ;

The steps previously necessary to make the DLL, which were described in Colin Peter's tutorial.

 
gcc -DBUILD_DLL -c mydll.c
gcc --disable-stdcall-fixup -mdll -DBUILD_DLL -o junk.tmp -Wl,--base-file,base.tmp mydll.o
rm junk.tmp
dlltool --dllname mydll.dll --base-file base.tmp --output-exp temp.exp --input-def mydll.def
rm base.tmp
gcc --enable-stdcall-fixup -mdll -DBUILD_DLL -o mydll.dll mydll.o -Wl,temp.exp
rm temp.exp

According to an update of Colin Peters' tutorial, which at one time was posted at www12.canvas.ne.jp/peters/colin/win32/dll/make.html, the GNU compiler has progressed sufficiently so that it is no longer necessary to use dlltool to create a dll and import library.

All that is needed to create the dll is

 
gcc -DBUILD_DLL -c mydll.c
gcc -shared -o mydll.dll mydll.o mydll.def -Wl,--out-implib,libmydll.a

This creates both the the DLL and also an import library, mydll.a. Note that there are no spaces after the commas in "-Wl,--out-implib,libmydll.a" and there are no numbers, just the letter "el" after the "W".

With both the old, as well as the new, method for creating the DLL, you need a ".def" file in order to create a DLL. It may be created by hand in simple cases. Details are found in our other Moron guide.

Using your DLL

Once you have an import library for your DLL, you can copy your import library to /usr/lib/ for Cygwin or /Mingw/lib/ for Mingw and then compile and link with

      gcc progmain.c -o progmain -lmydll

Alternatively, if you do not have an import library, you still can enable your program progmain to use your DLL by compiling and linking the two as follows

      gcc progmain.c -o progmain mydll.dll

You will need to have the line

    #include "mydll_dll.h"

in all programs that link to your DLL. Mydll_dll.h must be in the program build directory, or elsewhere in the include path for the build.

With the headerfile shown in the Mingw documentation, progmain had to be a C program. With the headerfile shown here, progmain.c can just as easily be progmain.cpp, a C++ program.

Everything made easy

In summary, in order to build and use your own DLLs, you need a file containing the functions you want in your DLL, you need a dll.h header file, a dll.def file, and possibly an import library, dll.a.

If you wish, you may use my program dllmake, in which case all you need to do is create a file with the functions that you wish to export preceded by the word EXPORT (capitals important) and then type

                                    ./dllmake mydll

You do not even have to add the line "#include mydll_dll.h" to mydll.c, because dllmake will add that line to the file it uses for the compilation. Dllmake will create mydll.dll, and also will create the file mydll_dll.h, as well as an import library, mydll.a.

Download dllmake-0.4.


Check out our freeware.
Read our other Moron guide.

Emmes Technologies
Updated Mar 28, 2008

valid html 4.01!