Archive for August, 2011

Setting up MinGW for DirectX

Set up MinGW

This article will show you how to set up a MinGW DirectX development environment. First, you need to get the latest and greatest MinGW development environment. The easiest way to do this is to download and run the latest mingw-get-inst GUI installer. Select the directory to install MinGW. Make sure that this path contains no spaces. Select the C++ and MSYS optional components.

This will download and run the Command line installer. You will see a console window open while it downloads and installs the selected components. Once the installation completes you must add the MinGW bin/ directory to your path, for example C:\MinGW\bin.

Start the MSYS shell by running C:\MinGW\MSYS1.0\msys.bat. In this MinGW shell, run the /postinstall/pi.sh script to establish bindings between the MinGW and MSYS installations. Just say yes.

Other packages may be installed manually following the manual install instructions found here.

Set up the DirextX SDK

Download the latest DirectX SDK. Microsoft likes to keep moving things around but as of this writing it can be found at this link http://msdn.microsoft.com/en-us/directx/. Run the installer and install to a directory without spaces, for example C:\DirectXSDK.

The latest DirectX SDK is installed but it is not yet visible from within the MSYS environment. In the root directory /, create an empty /dxsdk subdirectory. This will be used as a mount point for the DX SDK. Edit the file /etc/fstab and add the line “C:/DirectXSDK /dxsdk“. The hard drive path and the mount point may be separated by any number of spaces or tab characters. Go to /dxsdk and list the directory contents. If it is empty you probably have a typo in fstab. Also, older MinGW versions did not automatically reload the mount points when fstab was modified. You may need to exit your shell and restart it.

Build and run the test application

I borrowed the following test application from DirectX Tutorial.com. If everything was set up properly it should build and run with no modifications.

dxtest.cpp

Build the DirectX test application with the following command:

g++ dxtest.cpp -o dxtest.exe -I/dxsdk/include -L/dxsdk/lib -DUNICODE -ld3d9

The options are:

  • -o dxtest.exe – specifies the output file
  • -I/dxsdk/include – specifies additional include directories to search
  • -L/dxsdk/lib – specifies additional library directories to search
  • -DUNICODE – defines the UNICODE preprocessor symbol for unicode strings
  • -ld3d9 – link to the d3d9.dll

You can run the DirectX test application by typing:

./dxtest.exe

Note that if you try to run this program outside the MinGW environment, for example double clicking it in explorer, you may get a dialog box complaining about not being able to find libgcc_s_dw2-1.dll. This is probably because you didn’t close explorer and open it again after you added MinGW to your path. Ensure that C:\MinGW\bin is in your path. Explorer “should” pick up the change immediately but for some reason it doesn’t always. Close and reopen your explorer window.

This brings up an interesting point however. Our application is dependent on a dll shipped with the MinGW environment. If we are to redistribute our executable, we must also redistribute the dll or our users will not be able to run our application. One way to fix this is to link the gcc run time statically:

g++ dxtest.cpp -o dxtest.exe -I/dxsdk/include -L/dxsdk/lib -DUNICODE -ld3d9 
	-static-libgcc

This does however have the drawback that it has the potential to dramatically increase the size of our executable. Choose what works best for your situation.

Also note, now that we are able to run our application outside the MinGW shell, a console window opens up along with our application. This is ugly and not something we really want. To get rid of this console window we need to specify that we are building a windows application using the -mwindows option:

g++ dxtest.cpp -o dxtest.exe -I/dxsdk/include -L/dxsdk/lib -DUNICODE -ld3d9 
	-static-libgcc -mwindows

Thats it! Have fun with DirectX and MinGW! These are the exact steps I’ve taken and they worked for me today. Tomorrow a new version of something might break things. Let me know what issues you uncover and I’ll try to keep this up to date.

Useful resources:

Tags: , , , ,

Thursday, August 11th, 2011 Cpp, DirectX, Graphics, Programming No Comments