GMS 2 Getting started with C++ DLL Extension creation in Eclipse

Bawat

Member
With the aim of getting improved performance, I set out on the odyssey to create a .DLL extension in C++.
With no up to date resources on how to get this set up properly, and little memory of C++ it took me 2 days to get GMS2 and the DLL to communicate.
The two goals of this guide and it's comment section are to save people time in setting up an environment to reliably build .DLL extensions for GMS2, and to provide links that may be of benefit to DLL creation.

Availible from https://osdn.net/projects/mingw/releases/
Make sure you download the mingw-get-setup.exe as it's a bit hidden.
MinGWGCCDownload.PNG

I wanted to use Cygwin GCC, but as this post breifly mentions, I also couldn't get Cygwin to work.
If you're feeling up to the challenge, you would need to install Cygwin rather than Cygwin64, as it seems GMS2 only supports 32 bit DLLs - and Cygwin64 can not compile 32bit. If you already have 64 installed, remember to change your Cygwin path Environment Variable to the 32 bit version.

You should now have the MinGW Installation Manager open. Check all the packages inside the Basic Setup section.
MinGWGCCBasicSetup.PNG

I'm going to use Eclipse for this tutorial. It's an IDE I'm familiar with and I prefer it over Visual Studio.
I found 2 posts from people who had problems with loading GM DLLs made inside of Visual Studio, that after switching to Code::Blocks were able to get their code to work.
Following Section 1 Step 1 of this guide.

Two ways to install CDT, depending on whether you have previously installed an Eclipse:

  1. If you have already installed "Eclipse for Java Developers" or other Eclipse packages, you could install the CDT plug-in as follows:
    Launch Eclipse ⇒ Help ⇒ Install New Software ⇒ In "Work with" field, pull down the drop-down menu and select "Kepler - http://download.eclipse.org/releases/kepler" (or juno for Eclipse 4.2; or helios for Eclipse 3.7).
    In "Name" box, expand "Programming Language" node ⇒ Check "C/C++ Development Tools" ⇒ "Next" ⇒ ... ⇒ "Finish".
  2. If you have not install any Eclipse package, you could download "Eclipse IDE for C/C++ Developers" from http://www.eclipse.org/downloads, and unzip the downloaded file into a directory of your choice.

This part is surprisingly simple. Lonewolff has a fantastic tutorial for this using Visual Studio.
Right click in Project Explorer -> New -> Project... -> Inside the C/C++ folder -> C++ Project
Click Next
EclipseCreateProject.PNG
Specify the Project Name
Specify the Project Type as Inside the Shared Library folder -> Empty Project
Specify the Toolchain as MinGW GCC

Add a new .cpp file into the project. No header file is needed.
C++:
#define fn_export extern "C" __declspec (dllexport)

fn_export double test_your_might(){
    return 3;
}
__declspec (dllexport) is used to expose functions to the outside by prepending them with fn_export

While trying to build using Cygwin or Cygwin64, GMS2 would throw a LoadLibraryA failed with error 193 when trying to load the DLL. It would also unexpectedly return a 0 from the test_your_might() function.
While trying to build using MinGW, GMS2 would throw a LoadLibraryA failed with error 126 when trying to load the DLL. It would also unexpectedly return a 0 from the test_your_might() function.

Taking a look at
through the Wayback Machine reveals that
193 -> ERROR_BAD_EXE_FORMAT %1 is not a valid Win32 application.
126 -> ERROR_MOD_NOT_FOUND The specified module could not be found.

Looking at the .DLL produced by MinGW through this specific program shows that libgcc_s_dw2-1.dll is missing as a MinGW fault.
To build the .DLL with libgcc_s_dw2-1.dll included, we need to pass the -static-libgcc -static-libstdc++ arguments to the linker in Eclipse.

Right click on the project -> Properties -> Unfold the C/C++ Build spoiler -> Settings -> Tool Setting tab -> MinGW C++ Linker -> Miscellaneous
In the Linker Flags textbox add -static-libgcc -static-libstdc++.
AddingWithExtraLib.PNG

First Clean, then Build!
You can build as Release or Debug, both work.
A folder should appear in your project with the .DLL inside of it.

First of all, I would advise copying your .DLL out of the Eclipse project to somewhere else before use. Otherwise if Eclipse wants to Clean and finds the file in-use, it won't be able to delete it, and you'll have to close and reopen Eclipse and Game Maker.
I would also like to point out, each time you update your .DLL that you will need to re-import it into GMS2. Simply building over the old file, or pasting over the old file will not propagate the changes into GMS2.
Re-importing can easily be done via the Extension Dialog Window by right clicking on the .DLL resource -> Add file -> And importing a file with the same name as the existing .DLL.
Game Maker will ask if you want to overwrite the existing .DLL, say yes, and all your defined DLL Functions will still be there.

Right click on Extensions and create a new extension.
Follow Lonewolff's tutorial from this point onwards.

These are the settings for this example.
1586233395978.png
If the method name you want to use in GML is the same as the method name in C++, you can place the method name in the Name: field.
If the method name in the .DLL is test_your_might() but you want to use it in GML as might_test(), place test_your_might into the External Name: field and might_test in the Name: field.

If you've now managed to get a 3 as output, congratulations!
My next steps will be to read the following resources I came across to learn more about DLLs in GMS2

For those of you who weren't so lucky, you've probably found a 0. This can happen when GMS2 can't match the method name you gave it with anything in the DLL.
Check the GMS2 Console for clues like LoadLibraryA failed with error 193 and see if my debugging notes inside "Building the .DLL in Eclipse" can help.
I have also attached my generated .DLL and source code in a comment below, to help you debug.
 
Last edited:
Top