Building NetHack with MinGW and GCC

NetHack! It’s simply the most fun you can have with letters! Well, I mean, unless you count reading.

Below is a list of steps to compile the open-source game NetHack from the C source code.

1. Download and install MinGW. MinGW is short for “Minimalist GNU for Windows”, and it incorporates “gcc”, which is short for the “GNU Compiler Collection”. While there’s a lot of documentation on the mingw.org website that’s intended to explain how to install and use it, in practice it’s extremely difficult to figure out how to even download it, much less install it. As of this writing, what I believe to be the latest installer is available at “http://sourceforge.net/projects/mingw/?source=dlp”.  (UPDATE 2013/09/06 – Upon attempting to run through these steps again, I found installing MinGW to be even more painful than I remembered.  The good news is that I eventually found something called “TDM-GCC“, by “John E.”. It appears to be a much simpler way to install MinGW, and I am totally into that.)

2. Download and install Notepad++. Notepad++ is a popular free text and code editor, with a lot of useful features. In this article, it will be used to view text files with Unix-formatted line breaks, and to find and replace text in many source code files at once. As of this writing, the latest version of the installer is available at “http://notepad-plus-plus.org/download/v6.2.2.html”.

3. Download and install 7-Zip, if necessary. 7-Zip is a popular free file compression/decompression utility that understands a lot of file formats, including the “.tgz” format that the NetHack source code is packaged with. As of this writing, the installer for the latest version is available at “http://www.7-zip.org/download.html”.

4. Download the NetHack source code. As of this writing, the source code is available as “nethack-343-src.tgz” at “http://www.nethack.org/v343/download-src.html”.

5. In any convenient directory, use a file decompression tool (7-Zip, perhaps) to extract the source code from the archive file “nethack-343-src.tgz”. You may have to do this twice, since uncompressing the “.tgz” format produces a “.tar” file, which must itself then be extracted to obtain the actual files.

6. In the newly uncompressed nethack-3.4.3 directory, navigate to the sys/winnt directory.

7. Execute the file “nhsetup.bat”. New directories named “binary” and “build” will be added to the nethack-3.4.3 root directory.

8. Open Notepad++.

9. In Notepad++, select the “Search – Find in Files” item from the top menu bar. The “Find in Files” dialog will appear.

10. On the Find in Files dialog, in the “Find what” box, enter the value “boolean”. In the “Replace with” box, enter the value “boolnh”. In the Directory box, enter the path of the newly extracted directory containing the nethack source code files. Activate the “Match Case” checkbox. Activate the “In all sub-folders” checkbox.

11. Still on the Find in Files dialog, click the “Replace in Files” button and then click OK on the confirmation dialog that appears. This will replace all occurrences of the text “boolean” in the NetHack source code with “boolnh”. (This step is necessary to work around a naming conflict that stems from the fact that the NetHack source code is, frankly, elderly.)

12. Open src/makefile.gcc in Notepad++. Comment out the line “DEBUG = Y”. Save the file.

13. Add the path of MinGW’s “bin” directory to the Windows “Path” environment variable. By default, this directory’s path is “C:\MinGW\bin”. (There are multiple ways to access the environment variables. One way is to enter “SystemPropertiesAdvanced.exe” at the Run prompt and click the Environment Variables button on the dialog that appears.)

14. In the root NetHack directory, create a new text file named NetHack-Build.bat, containing the following text.

echo About to attempt make...
del /Q binary\*.*
cd src
del /Q o\*.*
mingw32-make -f Makefile.gcc install
cd ..
echo Make attempt finished.
pause

15. Run Nethack-Build.bat. The source code will be compiled, and an executable called NetHack.exe will be created in the “binary” directory.

Notes

  • This tutorial only shows how to build the text-based version of the game, not the one that uses graphical tiles. The code for that version includes a bunch of stuff that seems to be meant to build the tile images from included text files. I wasn’t immediately able to figure it out, but if you care about graphics, you might want to investigate more closely.
  • Similarly, there may be a more elegant and efficient solution to all that stuff about renaming “boolean” to “boolnh”, but again, I couldn’t immediately figure it out. If the documentation for things like this were a little more idiot-friendly, this post (and indeed this blog) might not exist.
Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

3 Responses to Building NetHack with MinGW and GCC

  1. Billy Suter says:

    I tried compiling NH with your suggestions, but these errors pop up:

    In file included from ../include/config.h:370:0,
    from ../include/hack.h:9,
    from ../util/lev_yacc.c:33:
    ../util/lev_yacc.c: In function ‘yyparse’:
    ../include/global.h:61:24: error: expected ‘)’ before numeric constant
    #define FALSE ((boolnh)0)
    ^
    ../util/lev_yacc.c:1126:28: note: in expansion of macro ‘FALSE’
    init_lev.init_present = FALSE;
    ^
    ../include/global.h:61:24: warning: overflow in implicit constant conversion [-W
    overflow]
    #define FALSE ((boolnh)0)
    ^
    ../util/lev_yacc.c:1126:28: note: in expansion of macro ‘FALSE’
    init_lev.init_present = FALSE;
    ^
    ../include/global.h:60:23: error: expected ‘)’ before numeric constant
    #define TRUE ((boolnh)1)
    ^
    ../util/lev_yacc.c:1132:28: note: in expansion of macro ‘TRUE’
    init_lev.init_present = TRUE;
    ^
    ../include/global.h:60:23: warning: overflow in implicit constant conversion [-W
    overflow]
    #define TRUE ((boolnh)1)
    ^
    ../util/lev_yacc.c:1132:28: note: in expansion of macro ‘TRUE’
    init_lev.init_present = TRUE;
    ^
    mingw32-make: *** [o/lev_yacc.o] Error 1

    Do you have any idea what’s causing this?

    • Sorry… I just tried following these instructions again on a 64-bit Windows 7 machine, and they still seem to work for me. I do get a lot of warnings (all of which say “warning: cast from pointer to integer of different size”), but no actual errors. If it makes you feel any better, I couldn’t figure out how to install MinGW using my own instructions this time. I eventually gave up and used the “TDM-GCC” package instead (as per my update to step one above). So your MinGW-fu is far stronger than mine, for whatever that’s worth.

      Good luck, and let me know if you figure it out.

  2. ryanh says:

    For anyone who cares: Get version 2.0 of mingw from somewhere on the web. worked first go

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s