Also notice the “ _cdecl” before the function name, which declares the calling convention for the function (see the Wikipedia article on x86 calling conventions if you’re not familiar with them). Notice the “ _declspec(dllexport)” attribute in the code below-this is the key to exporting functions from the DLL, and every function you would like exported from the DLL should be marked with this (any internal functions which don’t make up part of your API should be left as they are). The Inflexible and Inelegant Wayįirst we will create a DLL exporting only a very basic function which adds two integers and returns the result. The exporting and importing of functions is fairly straightforward, and just involves a little bit of code accompanied by the correct linker command line. Likewise, if an application bar.exe wants to make use of the function DoWork(), it must import that function from foo.dll (it is also possible to write code which loads a DLL once an application has started running, calls functions from that DLL, and then unloads the DLL when it’s no longer useful-this is how plugin / addon systems usually work). If you have a library foo.dll which contains a function DoWork(), the DLL must export that function in order for it to be used by applications. DLL BasicsĪ DLL is a type of shared library used on Microsoft Windows and OS/2, containing functions which can be reused in any application which wants to make use of them (actually, DLLs can contain a lot more than just functions, but this article is about creating libraries of functions). The code we produce here will be usable in MSVC as well, so you or your end users can target multiple compilers.
This article will show you the basics of creating DLLs with MinGW, and then linking to them in your applications. With MinGW a bit of manual work is needed, but it’s not that difficult when you know how it’s done.
Under MSVC it’s pretty simple, as Visual Studio will create a barebones project with everything you need.
#MAKE A DLL FILE HOW TO#
One question I often get asked is how to create Windows DLLs using MinGW.
Building Windows DLLs with MinGW Transmission Zero Building Windows DLLs with MinGW