Difference between revisions of "Compiling"

From NetHackWiki
Jump to navigation Jump to search
(On Mac OS X: merge duplicated instructions for snow-leopard back into main OSX instructions, using a couple of conditional steps to account for the differences.)
m (I updated the instructions for building 3.7 on Windows.)
 
(32 intermediate revisions by 18 users not shown)
Line 1: Line 1:
This page contains help on '''compiling''' NetHack from source.
+
This page contains help on '''compiling''' NetHack 3.6, either the officially released version (3.6.0) or the version from the development sources (as of 2016 October).  Compiling is necessary if you want to try out the dev team's latest changes when they haven't released a compiled version yet, or if you want to apply a [[patch]], e.g. from the [http://bilious.alt.org/ bilious database] or make changes of your own.
  
==On Windows==
+
Information about compiling NetHack 3.4.3 can still be found in [https://nethackwiki.com/mediawiki/index.php?title=Compiling&oldid=99565 an old version of this article, q.v.].
  
This was tested on:
+
==Obtaining the officially released sources==
- Win2k with MinGW-5.0.3,
 
- WinXP with [http://download408.mediafire.com/0cnai0w5i85g/ysnyqpdienpjb0z/MinGW-5.1.1.exe MinGW-5.1.1],
 
  
*Download NetHack's sources from http://nethack.org/v343/download-src.html
+
The official released NetHack source can be found here:
 +
http://nethack.org/v360/download-src.html
  
*Download MinGW from http://sourceforge.net/project/showfiles.php?group_id=2435 (You want the "Automated MinGW Installer")
+
Once you have obtained the tarball, you will need to extract it, either using standard tools (<tt>tar -xvf nethack-360-src.tgz</tt>) or some other archive extractor that can handle the tar and gzip formats.
  
*Install MinGW. If you use the 'minimal' installation, remember to also select the optional 'MinGW make' -component, so it gets installed. Use, say, <tt>C:\mingw\</tt> as the path to install to.
+
Note that if you use a graphical archive extractor like 7zip, you may have to extract twice: once to decompress the gzip and obtain the plain tar file, and a second time to extract the actual contents.
  
*Unpack the NetHack source package somewhere, say, <tt>c:\nh343\</tt>. After unpacking, that directory should contain several subdirectories, such as <tt>src</tt>, <tt>include</tt>, <tt>win</tt> and <tt>sys</tt>. You will need WinZip or some other utility that can uncompress the package.
+
==Obtaining the development sources==
  
*Go to <tt>c:\nh343\sys\winnt\</tt> and run <tt>nhsetup.bat</tt>
+
The development source repository is available at http://git.code.sf.net/p/nethack/NetHack
  
*If you want to run the graphical windows GUI, edit <tt>c:\nh343\src\makefile.gcc</tt> and change the line
+
If you have git installed, you can retrieve a copy of the repository using either of the following commands:
#GRAPHICAL=Y
 
to
 
GRAPHICAL=Y
 
  
NOTE: The latest MinGW (5.1.3 as of writing) has issues compiling the GUI version of the program. To remedy this, copy rpcndr.h from <tt>c:\MinGW\include\</tt> to <tt>c:\nh343\include\</tt>. Edit <tt>c:\nh343\include\rpcndr.h</tt> by removing or commenting out line 52: "typedef unsigned char boolean;". This should allow the compile to commence with minimal errors.
+
<tt>git clone https://github.com/NetHack/NetHack</tt>
  
*Go to <tt>c:\nh343\</tt> and create a new file called <tt>'install.bat'</tt> with the following contents:
+
alternately:
PATH=%PATH%;c:\mingw\bin
 
cd src
 
mingw32-make -f makefile.gcc install
 
  
*Run the <tt>install.bat</tt> you just created. This takes about thirty seconds.
+
<tt>git clone http://git.code.sf.net/p/nethack/NetHack</tt>
  
*The NetHack binary should now be in <tt>c:\nh343\binary\</tt>
+
(It doesn't matter which repository you use; they're both mirrors of the public portion of the dev team's repository.)
  
*If you want to patch your NetHack, you should next see the article on [[patching]].
+
This will create a subdirectory (a.k.a., a "folder") called 'NetHack', in the current directory, and the new subdirectory, 'NetHack', will be a git repository, containing a working copy of the latest public development source for what will eventually become NetHack 3.6.1.
  
=== Cygwin ===
+
==Preparing to compile==
Useful instructions for compiling under [http://cygwin.org/ Cygwin] can be found at this [http://planetexpress.homedns.org/nethack/compile_cygwin.txt external page]. Instructions for compiling under Cygwin and MinGW/MSYS (so the game won't need Cygwin to run) can also be found [http://www.crawfords.de/nh-cygwin-mingw-howto/ here].
 
  
==On Linux==
+
===Gathering build tools===
  
===TTY===
+
In order to build NetHack, you will neet at minimum the following tools:
 +
* a C compiler (typically 'gcc' or 'clang')
 +
* 'make' (typically Gnu 'make', and note in particular that Microsoft's 'nmake' won't work)
 +
* 'flex' and 'bison', or some other suitable lex/yacc implementation
 +
* common libraries (including development headers); these should be includeded if you have a complete working build environment (e.g., if you have installed build-essential on Debian-derived systems, you should have them).  I don't know a complete list of all the libraries that are required, but I believe the list includes standard POSIX regular expression libraries, termcap/terminfo, and of course the standard C library.  Beyond that, I'm not sure.
 +
* any libraries (including development headers) needed for the specific ports you wish to build; if this is the first time you are building NetHack, it is recommended that you start by building the Unix TTY port first, as it is the easiest and has no additional requirements of its own beyond the ones that all the ports require.
  
The following compiles the TTY-version of NetHack, with all the default settings.
+
Note that you will need copies of all these on the platform for which you are building NetHack.  Cross-compiling NetHack, if even possible, is much more complicated and far beyond the scope of this article.
  
*Download NetHack's sources from http://nethack.org/v343/download-src.html
+
===Selecting a hints file===
  
*Make sure you have the development packages for ncurses installed. On debian these are called libncurses5-dev. You will also need make, gcc, yacc (or something similar, for example bison), and lex (or something similar, such as flex).
+
The new build system introduced in NetHack 3.6.0 is based on "hints files", which inform the rest of the build system about various things which might differ from one system to another, and which you might wish to customize when building, even if you are making no changes to the actual game.  Things like which directory to install the game's binaries into, which directory to use for storing the game data, which flags to pass to the compiler, and even basic facts like what the compiler on your system is called, are all specified in the hints file. A hints file is simply a plain text file, formatted like a shell script, that contains hints for the NetHack build system, in the form of setting variables.
  
*Open a terminal, and <tt>cd</tt> to the directory where you downloaded the source package to.
+
You can find a collection of existing hints files in the sys/unix/hints/ directory.  You may find that one of them is closer to what you want than others.  For example, the 'linux' hints file is designed for a single-user install in ~/nh/install; whereas, the 'unix' hints file is designed for a traditional system-wide install in /usr.  Look through the existing hints files and see if you can find one that is a close fit for your needs.
  
*Unpack the NetHack sources and move to the NetHack source root directory:
+
===Modifying a hints file===
tar xvzf nethack-343-src.tgz
 
cd nethack-3.4.3
 
  
*Execute the script that creates symbolic links to the Makefiles in the correct places:
+
If you need to combine or modify a hints file, here are some useful things to know:
  sh sys/unix/setup.sh x
+
* Lines starting with # are comments.  You can thus prepend # to an existing line to comment it out, rendering it ineffective but leaving it there for reference.
 +
* There are four main directories that need to be specified: 'HACKDIR', 'SHELLDIR', 'INSTDIR', and 'VARDIR'.  These are often based on a 'PREFIX' directory, which is not itself used directly for anything, but the others are typically located inside of it.
 +
* The CHOWN and CHGRP variables should contain the commands used to change ownership and group-ownership of files, respectively.  Setting it to a command that doesn't do anything (such as 'yes' or 'true', as the stock 'linux' hints file does) will result in permissions not being changed, which is typically not what you want except possibly for a single-user install.  If you are modifying the 'linux' hints file to build on a Linux system, you will probably want to change these lines.
 +
* The WINSRC, WINOBJ, and WINLIB lines control which window port you will build. If this is your first time compiling NetHack, it is recommended to start with the TTY port, which is easiest to build successfully.
 +
* In 3.7.0, hints/linux tends not to compile, but hints/linux-debug works fine. VARDIR seems to need '/var' to be at the end of the path you choose, or else the game won't start because it can't find the scoreboard. You may also want to remove 'CFLAGS+=-DPANICLOG_FMT2' because it generates tons of warnings.
  
*Edit <tt>include/unixconf.h</tt> by changing
+
==Running the build setup script==
/* #define LINUX */
 
to
 
#define LINUX
 
  
*Edit <tt>src/Makefile</tt> by commenting the following like so
+
Once you have selected and possibly modified a hints file, you are almost ready to run the setup script. Run "make fetch-lua".
#WINTTYLIB = -ltermlib
 
and uncommenting this line so it looks like
 
  WINTTYLIB = -lncurses
 
  
*If you're using something else than yacc and lex, edit <tt>util/Makefile</tt> so that <tt>YACC</tt> and <tt>LEX</tt> defines are correct.
+
Then on most platforms, setup is done with the following command:
 +
<tt>sys/unix/setup.sh sys/unix/hints/whichever_hints_file_you_are_using</tt>
  
*If you are using cygwin, edit <tt>include/config.h</tt> and uncomment the following like so
+
On non-Unix platforms, the setup process may be slightly different. See the documentation for your platform. The setup script should copy the needed build-system files (e.g., makefiles) into place and modify them as needed.
#define STRNCMPI        /* compiler/library has the strncmpi function */
 
  
*Typing <tt>make</tt> in the NetHack source root dir should compile the binary executable. Typing <tt>make all</tt> will compile everything, including [[special levels]].
+
===Windows===
  
*To install NetHack to <tt>/usr/games/</tt>, type <tt>make install</tt> while you're logged in as root. If you want to change the installation directory, you need to edit the top Makefile.
+
For Nethack 3.6 on Windows, it's a good idea to follow the instructions inside <tt>sys\winnt\Install.nt</tt>. The following assumes that you have your compiler and coreutils from projects such as MinGW and GnuWin32.
  
*For more information about compiling NetHack, see the <tt>sys/unix/Install.unx</tt> text file.
+
* Navigate to <tt>sys\winnt</tt> and run <tt>nhsetup.bat</tt>.
  
*If you want to patch your NetHack, you should next see the article on [[patching]].
+
* Navigate to <tt>src</tt> and edit <tt>Makefile.gcc</tt>.
  
===X11===
+
* Inside the Makefile, you're going to want to edit or check the following things:
 +
** Do you want the outputted binary to belong in <tt>..\binary</tt>, or somewhere else?
 +
** Are you compiling for a specific architecture (x86 or x64)? If so, edit <tt>TARGET_CPU</tt>.
 +
** Do you want Curses window port support? If so, uncomment <tt>ADD_CURSES=Y</tt> and make sure that you've cloned the PDCurses repository from https://github.com/wmcbrine/PDCurses.
 +
** TODO: (Test this, I only wanted the terminal interface) Do you want Qt support? You're going to want to change this option and specify the location of the Qt directory.
 +
** On Line 484, are the compiler macros set up correctly? Mine were not, as Chocolatey links the compilers to standard <tt>gcc</tt> filenames instead of <tt>mingw32-gcc.exe</tt>. Comment out the <tt>#ifndef</tt> directive and specify the compilers.
  
The following compiles the X11 windowport with default settings. First follow the instructions in the [[#TTY|TTY section]], then continue this.
+
* Make with your <tt>make</tt> program. For me, the invocation was <tt>mingw32-make -f Makefile.gcc install</tt>. Note that if you don't specify the <tt>-f Makefile.gcc</tt> option, you'll get an error, as the standard <tt>Makefile</tt> is for Visual Studio's <tt>nmake</tt> program.
 +
{{upcoming|NetHack 3.7.0|Nethack 3.7 has good support for building, running, and debugging in Visual Studio 2017, 2019, and 2022, including a working solution file. Tools such as MinGW and GnuWin32 are not required. See <tt>sys\windows\Install.windows</tt> for instructions.}}
  
*Make sure you have the X11 Athena Widget libraries and headers installed. On Debian the packages are <tt>libxaw</tt>, <tt>libxaw-dev</tt> and <tt>libxaw-headers</tt>.
+
==Compiling==
  
*Edit <tt>include/config.h</tt> by changing
+
At this point, you may be able to just run the 'make' command and get a working build.
/* #define X11_GRAPHICS */
 
to
 
#define X11_GRAPHICS
 
  
and
+
==Installing==
/* # define USE_XPM */
 
to
 
# define USE_XPM
 
  
*Edit <tt>src/Makefile</tt>:
+
If the build goes well, you can (as root/superuser if necessary, or with sudo) use the 'make install' command to install your newly compiled NetHack.
**add <tt>$(WINX11SRC)</tt> to <tt>WINSRC</tt>
 
**add <tt>$(WINX11OBJ)</tt> to <tt>WINOBJ</tt>
 
**add <tt>$(WINX11LIB)</tt> to <tt>WINLIB</tt>
 
**Change <tt>WINX11LIB</tt> so that it has <tt>-lXaw -lXmu -lXext -lXt -lXpm -lX11 -lm</tt>
 
**At least on Debian, <tt>WINX11LIB</tt> also needs <tt>-L/usr/X11R6/lib</tt>
 
  
*Edit <tt>Makefile</tt> so that <tt>VARDATND</tt> includes <tt>x11tiles NetHack.ad pet_mark.xbm rip.xpm</tt>
+
==Creating the system config file==
  
*Type <tt>make</tt> in the NetHack source root dir.
+
When you try to run your new build, it will complain that there is no syscf file and refuse to do anything until you create that file.  The syscf file is allowed to be empty; it just has to exist.  Simply create an empty file named sysconf in your HACKDIR and nethack will run normally.
  
*To install NetHack to <tt>/usr/games/</tt>, type <tt>make install</tt> while you're logged in as root. If you want to change the installation directory, you need to edit the top Makefile.
+
Depending on which hints file you used, some permissions may need to be changed before the game can be run as a regular user.
  
*For more information about compiling NetHack, see the <tt>sys/unix/Install.unx</tt> text file. For X11 compilation, see <tt>win/X11/Install.X11</tt>
 
 
*If you want to patch your NetHack, you should next see the article on [[patching]].
 
 
*If you want to use tiles on X11, or change the window colors, see the article on [[X11 configuration]].
 
 
==On Mac OS X==
 
 
These are the instructions for compiling a TTY port of NetHack with default settings on Mac OS X. You will need to have the Xcode developer tools installed to be able to compile NetHack (check the CDs that came with your copy of Mac OS X). These instructions assume that you have a minimal working knowledge of C and Terminal.
 
 
This has worked on at least OSX 10.6.1, 10.6.8.
 
 
#Download NetHack's sources from http://nethack.org/v343/download-src.html and unpack them.
 
#Open Terminal and <tt>cd</tt> to the <tt>sys/unix</tt> directory in the source folder. Run <tt>sh setup.sh</tt>. New files named <tt>Makefile</tt> will now be present in the <tt>/src</tt> and root folders. Return to the root folder.
 
#If you are installing "vanilla" Nethack, read about the problems described at [[Pkgsrc#patch-ac_.28system.h.29|this page on the Nethack wiki]] and install patch-ac. (Do not apply this patch on Snow Leopard, it is not needed and breaks the compilation.)
 
#Edit the file <tt>include/unixconf.h</tt> to make three changes (Do not do this on Snow Leopard):
 
#*Uncomment the BSD <tt>#define</tt> on line 22,
 
#*Comment out the <tt>#define</tt>s for SYSV on line 28, and
 
#*Comment out the <tt>#define</tt>s for SVR4 on line 35.
 
#In the file <tt>win/tty/termcap.c</tt>, change line 838 from <tt>#ifndef LINUX</tt> to <tt>#if !defined(LINUX) &amp;&amp; !defined(__APPLE__)</tt>
 
#Make three changes to <tt>src/Makefile</tt>, which was created in step 2:
 
#*Uncomment line 233
 
#*Comment out line 234
 
#*On line 154, change <tt>CFLAGS = -O -I../include</tt> to <tt>CFLAGS = -O2 -I../include</tt>. (This prevents a fatal compilation error (<tt> mkmaze.c:281: error: unable to find a register to spill in class 'Q_REGS'</tt>) in <tt>mkmaze.c</tt>.)
 
#To prevent errors on startup and exit ("No such file or directory. Exec to compress save/501XXX failed"), edit include/config.h, line 172, change to specify the actual location of 'gzip' (find it using 'where gzip'. I don't know if it exists on stock OSX, maybe you have to install gzip first):
 
#* #define COMPRESS "/usr/bin/gzip"
 
#<tt>cd</tt> to the <tt>src</tt> directory and run <tt>make</tt> to compile the bulk of NetHack. This takes a minute or two. If you needed to install the [[pkgsrc]] patch at step 3, you will receive an error here about a conflicting definition of <tt>srandom</tt>.
 
#Edit the <tt>Makefile</tt> in the root source directory to fix the game permissions. Here is one solution:
 
#* Change '<tt>games</tt>' on line 20 to '<tt>root</tt>',
 
#* Change '<tt>games</tt>' on line 21 to '<tt>staff</tt>',
 
#* Change the value of <tt>GAMEPERM</tt> on line 25 to <tt>0755</tt>
 
#* Change the value of <tt>FILEPERM</tt> on line 26 to <tt>0664</tt>, and
 
#* Change the value of <tt>DIRPERM</tt> on line 28 to <tt>0775</tt>.
 
#In Terminal, at the top source directory, run <tt>make all</tt> to create the auxiliary files, and then run <tt>sudo make install</tt> to install the game. This installs the executable to /usr/games/nethack, and your save/score/bones files are created in /usr/games/lib/nethackdir.
 
#To install the manpages, edit <tt>doc/Makefile</tt> by changing the path on line 44 to wherever you wish to install them (create the directory if necessary), and then run <tt>make install</tt> (possibly with <tt>sudo</tt>) from the <tt>doc</tt> directory.
 
#Different versions of Mac OS X may have different issues with file permission settings on various components of /usr/games/lib/nethackdir. The following works for Snow Leopard:
 
#* $ sudo chmod 775 /usr/games/lib/nethackdir
 
#* $ sudo chmod 775 /usr/games/lib/nethackdir/save/
 
#* $ sudo chmod 775 /usr/games/lib/nethackdir/perm
 
#* $ sudo chmod 775 /usr/games/lib/nethackdir/record
 
#Play! For example, open the terminal version by typing <tt>open src/nethack</tt> from the top source directory in Terminal. Different versions of Mac OS X may have different issues with file permission settings on various components of <tt>/usr/games/lib/nethackdir</tt>.
 
 
 
===See Also===
 
* [https://github.com/mxcl/homebrew/blob/master/Library/Formula/nethack.rb Automatic download, compiling, and installation using an alternative package manager called Homebrew]
 
* [https://github.com/dirkz/Nethack-Vanilla-Lion-3.4.3 NetHack Vanilla 3.4.3 TTY version for OSX Lion]
 
 
== On iPhone/iPod Touch ==
 
 
This is a tricky setup. The iPhone/iPod Touch (as of firmware 2.1), due to distribution terms, terminal sandboxing, and other odds and ends associated with a highly proprietary product, tends make things difficult.
 
 
So, we'll assume that you're using a jailbroken iPhone or iPod Touch for this procedure. We'll also assume that you're looking to play the game locally on the device on the go, as anyone can use the free terminal and telnet software to connect to [[NAO]] or equivalent.
 
 
We additionally assume that you're compiling for terminal mode; for tiles or native window manager support, far more work will be required in the future.
 
 
For compiling on the device itself, you will need:
 
* MobileTerminal or OpenSSH
 
* GCC, Make, and/or Toolchain 2.0
 
* Bison
 
* Flex
 
 
All of which may be found in Cydia's package repositories or, if you do not have Cydia, various places online.
 
 
Due to the rapid state of flux associated with commercial offerings, this list shall not be comprehensive. However, it WILL cover the highlights:
 
 
* SCP or otherwise download the latest sources onto your device, and perform the necessary <tt>tar -xzf</tt>. We use <tt>/usr/games</tt> as a candidate location, and <tt>/usr/games/nethack-3.4.3</tt> as our top level directory for the remaining steps.
 
* Browse into the <tt>sys/unix</tt> directory and read the associated documentation, to get a good feel for what you'll be doing with your system.
 
* (Optional) Alter settings in <tt>include/config.h</tt> such as compression options, wizard mode, etc. No changes need be made in here, though changing compression options will suppress a rather annoying warning.
 
* Return to <tt>sys/unix</tt>. Open <tt>Makefile.top</tt> and alter <tt>PREFIX, GAME, GAMEUID, GAMEGRP, GAMEDIR, VARDIR, SHELLDIR,</tt> and anything else you feel is necessary.
 
 
For convenience, here are tested values:
 
 
<pre>
 
PREFIX  = /usr/games
 
GAME    = nethack
 
# GAME    = nethack.prg
 
GAMEUID  = mobile
 
GAMEGRP  = wheel
 
...
 
GAMEDIR  = /usr/games/lib/$(GAME)dir
 
VARDIR  = $(GAMEDIR)
 
SHELLDIR = /bin
 
</pre>
 
 
* For the remaining makefiles in <tt>sys/unix</tt>, add <tt>CC = gcc</tt> or your compiler to the top of the file. In this example, <tt>CC = arm-apple-darwin9-gcc</tt> is used. Only <tt>Makefile.src</tt> and <tt>Makefile.utl</tt> ''should'' be required for this step.
 
 
<pre>Note: It is suggested, but not currently required, to also include items like:
 
 
CFLAGS = -O -I../include -I/var/include
 
LFLAGS = -L"/usr/lib" -F"/System/Library/Frameworks" -F"/System/Library/PrivateFrameworks" -bind_at_load
 
 
... by the maintainer of Toolchain 2.0.
 
 
However, this does not appear to be necessary; nor does alteration of the code of main().
 
</pre>
 
 
* From the main directory, specific source edits are required for localization in this environment:
 
** <tt>win/tty/termcap.c:839</tt>: Comment out: <tt>// extern char *tparm();</tt>
 
** <tt>sys/unix/Makefile.src:234</tt>: Make sure <tt>WINTTYLIB</tt> is defined as <tt>-lncurses</tt> instead of <tt>-ltermlib</tt> or similar.
 
** <tt>sys/unix/Makefile.utl:108</tt>: Set <tt>YACC</tt> and <tt>LEX</tt> to use bison and flex, respectively. Example lines for these changes are given within this file.
 
** <tt>sys/unix/Makefile.doc:23</tt>: Set <tt>GUIDECMD = cat Guidebook.txt</tt> instead of the default.
 
 
<pre>Note: flex and bison may not work in the current build, stating "Bus error".
 
 
If this happens to you, it is advised to make the .c and .h files for this step
 
on a different system and copy them into /util and /include manually.</pre>
 
 
* (2.x firmware) At several steps in the build process, you will need to run <tt>ldid</tt> to ''sign'' each binary file, as unsigned files will be automatically "Killed" by the system with no further information.
 
 
To do this, either alter the make files or perform the following incremental steps manually:
 
 
<pre>
 
ldid -S makedefs  (in util)
 
ldid -S recover  (in util)
 
ldid -S dgn_comp  (in util)
 
ldid -S lev_comp  (in util)
 
ldid -S nethack  (in src or the final install location (we use /bin))
 
 
(ldid should be located in /usr/bin)
 
</pre>
 
 
* (Optional) Create your own <tt>.nethackrc</tt> in your system's user directory (usually <tt>/private/var/mobile</tt>) and enjoy! :)
 
 
Other steps may be required, but these are the major steps that result in a functional, working build of <tt>nethack</tt>. Of special note, <tt>color</tt> and <tt>lit_corridor</tt> does not work in certain local terminals, but otherwise, all Unix options appear to be supported.
 
 
Additionally, <tt>mobile</tt> and <tt>root</tt> seem to conflict if both users are used interchangeably, causing deaths by trickery. Only one user is advised (preferably the unprivileged <tt>mobile</tt>) with this current setup.
 
 
This test was performed with vanilla NetHack, though there should be no reason for this procedure to fail with many mods, patches, or [[slashem]].
 
 
=== Getting color to work on iPhone/iPod Touch ===
 
Of special note is the fact that this build supports color, but this does not appear to enable color within MobileTerminal.app (vt100).
 
 
However, color support can be verified by ssh in xterm, or in other terminal software for the iPhone and iPod Touch.
 
 
==Using Autotools==
 
There's a [http://bilious.alt.org/?330 patch for Vanilla NetHack] that uses the [http://en.wikipedia.org/wiki/GNU_build_system GNU build system] (aka Autotools).
 
 
It is known to have worked on Linux and Mac OSX for building a TTY version and on Windows XP for building a TTY and Windows GUI version.
 
 
==Notes==
 
 
*If using flex and 'make' fails with 'Broken Pipe', you'll need to downgrade your version of flex. Version [http://prdownloads.sourceforge.net/flex/flex-2.5.4a.tar.bz2?download 2.5.4a] works with NetHack.
 
 
*By default, the Guidebook isn't copied to the game's directory. You'll need to copy it manually.
 
 
 
==Cross building reference notes==
 
May be this can be interesting for developers who want to cross compile this great game:
 
 
http://crux-arm.nu/gitweb/?p=ports/games-cross.git;a=blob;f=nethack/Pkgfile;h=4403c9d78cdc20bbc975584e5dd40b52d91f7c4d;hb=c38c624a549d690faef9de02d1a01de4f1267253
 
 
[[Category:Development]]
 
[[Category:Development]]
 +
{{nethack-360}}

Latest revision as of 02:37, 19 December 2023

This page contains help on compiling NetHack 3.6, either the officially released version (3.6.0) or the version from the development sources (as of 2016 October). Compiling is necessary if you want to try out the dev team's latest changes when they haven't released a compiled version yet, or if you want to apply a patch, e.g. from the bilious database or make changes of your own.

Information about compiling NetHack 3.4.3 can still be found in an old version of this article, q.v..

Obtaining the officially released sources

The official released NetHack source can be found here: http://nethack.org/v360/download-src.html

Once you have obtained the tarball, you will need to extract it, either using standard tools (tar -xvf nethack-360-src.tgz) or some other archive extractor that can handle the tar and gzip formats.

Note that if you use a graphical archive extractor like 7zip, you may have to extract twice: once to decompress the gzip and obtain the plain tar file, and a second time to extract the actual contents.

Obtaining the development sources

The development source repository is available at http://git.code.sf.net/p/nethack/NetHack

If you have git installed, you can retrieve a copy of the repository using either of the following commands:

git clone https://github.com/NetHack/NetHack

alternately:

git clone http://git.code.sf.net/p/nethack/NetHack

(It doesn't matter which repository you use; they're both mirrors of the public portion of the dev team's repository.)

This will create a subdirectory (a.k.a., a "folder") called 'NetHack', in the current directory, and the new subdirectory, 'NetHack', will be a git repository, containing a working copy of the latest public development source for what will eventually become NetHack 3.6.1.

Preparing to compile

Gathering build tools

In order to build NetHack, you will neet at minimum the following tools:

  • a C compiler (typically 'gcc' or 'clang')
  • 'make' (typically Gnu 'make', and note in particular that Microsoft's 'nmake' won't work)
  • 'flex' and 'bison', or some other suitable lex/yacc implementation
  • common libraries (including development headers); these should be includeded if you have a complete working build environment (e.g., if you have installed build-essential on Debian-derived systems, you should have them). I don't know a complete list of all the libraries that are required, but I believe the list includes standard POSIX regular expression libraries, termcap/terminfo, and of course the standard C library. Beyond that, I'm not sure.
  • any libraries (including development headers) needed for the specific ports you wish to build; if this is the first time you are building NetHack, it is recommended that you start by building the Unix TTY port first, as it is the easiest and has no additional requirements of its own beyond the ones that all the ports require.

Note that you will need copies of all these on the platform for which you are building NetHack. Cross-compiling NetHack, if even possible, is much more complicated and far beyond the scope of this article.

Selecting a hints file

The new build system introduced in NetHack 3.6.0 is based on "hints files", which inform the rest of the build system about various things which might differ from one system to another, and which you might wish to customize when building, even if you are making no changes to the actual game. Things like which directory to install the game's binaries into, which directory to use for storing the game data, which flags to pass to the compiler, and even basic facts like what the compiler on your system is called, are all specified in the hints file. A hints file is simply a plain text file, formatted like a shell script, that contains hints for the NetHack build system, in the form of setting variables.

You can find a collection of existing hints files in the sys/unix/hints/ directory. You may find that one of them is closer to what you want than others. For example, the 'linux' hints file is designed for a single-user install in ~/nh/install; whereas, the 'unix' hints file is designed for a traditional system-wide install in /usr. Look through the existing hints files and see if you can find one that is a close fit for your needs.

Modifying a hints file

If you need to combine or modify a hints file, here are some useful things to know:

  • Lines starting with # are comments. You can thus prepend # to an existing line to comment it out, rendering it ineffective but leaving it there for reference.
  • There are four main directories that need to be specified: 'HACKDIR', 'SHELLDIR', 'INSTDIR', and 'VARDIR'. These are often based on a 'PREFIX' directory, which is not itself used directly for anything, but the others are typically located inside of it.
  • The CHOWN and CHGRP variables should contain the commands used to change ownership and group-ownership of files, respectively. Setting it to a command that doesn't do anything (such as 'yes' or 'true', as the stock 'linux' hints file does) will result in permissions not being changed, which is typically not what you want except possibly for a single-user install. If you are modifying the 'linux' hints file to build on a Linux system, you will probably want to change these lines.
  • The WINSRC, WINOBJ, and WINLIB lines control which window port you will build. If this is your first time compiling NetHack, it is recommended to start with the TTY port, which is easiest to build successfully.
  • In 3.7.0, hints/linux tends not to compile, but hints/linux-debug works fine. VARDIR seems to need '/var' to be at the end of the path you choose, or else the game won't start because it can't find the scoreboard. You may also want to remove 'CFLAGS+=-DPANICLOG_FMT2' because it generates tons of warnings.

Running the build setup script

Once you have selected and possibly modified a hints file, you are almost ready to run the setup script. Run "make fetch-lua".

Then on most platforms, setup is done with the following command: sys/unix/setup.sh sys/unix/hints/whichever_hints_file_you_are_using

On non-Unix platforms, the setup process may be slightly different. See the documentation for your platform. The setup script should copy the needed build-system files (e.g., makefiles) into place and modify them as needed.

Windows

For Nethack 3.6 on Windows, it's a good idea to follow the instructions inside sys\winnt\Install.nt. The following assumes that you have your compiler and coreutils from projects such as MinGW and GnuWin32.

  • Navigate to sys\winnt and run nhsetup.bat.
  • Navigate to src and edit Makefile.gcc.
  • Inside the Makefile, you're going to want to edit or check the following things:
    • Do you want the outputted binary to belong in ..\binary, or somewhere else?
    • Are you compiling for a specific architecture (x86 or x64)? If so, edit TARGET_CPU.
    • Do you want Curses window port support? If so, uncomment ADD_CURSES=Y and make sure that you've cloned the PDCurses repository from https://github.com/wmcbrine/PDCurses.
    • TODO: (Test this, I only wanted the terminal interface) Do you want Qt support? You're going to want to change this option and specify the location of the Qt directory.
    • On Line 484, are the compiler macros set up correctly? Mine were not, as Chocolatey links the compilers to standard gcc filenames instead of mingw32-gcc.exe. Comment out the #ifndef directive and specify the compilers.
  • Make with your make program. For me, the invocation was mingw32-make -f Makefile.gcc install. Note that if you don't specify the -f Makefile.gcc option, you'll get an error, as the standard Makefile is for Visual Studio's nmake program.

The following information pertains to an upcoming version (NetHack 3.7.0). If this version is now released, please verify that it is still accurate, then update the page to incorporate this information.

Nethack 3.7 has good support for building, running, and debugging in Visual Studio 2017, 2019, and 2022, including a working solution file. Tools such as MinGW and GnuWin32 are not required. See sys\windows\Install.windows for instructions.

Compiling

At this point, you may be able to just run the 'make' command and get a working build.

Installing

If the build goes well, you can (as root/superuser if necessary, or with sudo) use the 'make install' command to install your newly compiled NetHack.

Creating the system config file

When you try to run your new build, it will complain that there is no syscf file and refuse to do anything until you create that file. The syscf file is allowed to be empty; it just has to exist. Simply create an empty file named sysconf in your HACKDIR and nethack will run normally.

Depending on which hints file you used, some permissions may need to be changed before the game can be run as a regular user.

This page may need to be updated for the current version of NetHack.

It may contain text specific to NetHack 3.6.0. Information on this page may be out of date.

Editors: After reviewing this page and making necessary edits, please change the {{nethack-360}} tag to the current version's tag or {{noversion}} as appropriate.