How to compile with mingw
- #How to compile with mingw how to#
- #How to compile with mingw .dll#
- #How to compile with mingw manual#
_declspec(dllexport) int _cdecl Add(int a, int b)
* add_basic.cĭemonstrates creating a DLL with an exported function, the inflexible way. In the next section we will be hiding these details using preprocessor definitions. Note that this first example is deliberately inflexible and inelegant in order to show exactly whatâs going on behind the scenes. The default calling convention for C functions in MinGW is cdecl, but itâs a good idea to always explicitly state the calling convention in case some other compiler has a different defaultâif this happens, your application will likely misbehave or crash as a result of calling one of these functions. 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.
#How to compile with mingw .dll#
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.
#How to compile with mingw manual#
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.
#How to compile with mingw 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