Compiling under Linux: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(partial rewrite)
Line 1: Line 1:
[[Category:Programming]]
{{toc-right}}
Adding a Linux version of your multiplayer mod server allows more sites to be able to host servers. More hosting increases the chance of your mod being a success. Much of the grunt work of getting a Linux server binary created has been automated. This document describes the environment and configuration needed to get you going with a Linux port.
 
'''Creating a Linux build''' of your multiplayer [[dedicated server]] or [[server plugin]] is not required, but does make it much more likely to be accepted by commercial server operators.
 
{{note|Building on Linux requires an existing Visual Studio project, which is converted to a makefile by Valve's <code>vcpm</code> tool ("Visual C++ Project to Make").}}
 
== Getting Linux ==
 
There is a bewildering array of Linux distributions. If you are unsure which to use go for [http://www.ubuntu.com/ Ubuntu], which tries to be user-friendly. It has a "software centre" that makes installing packages simple.
 
=== 64-bit ===
 
64-bit builds of Linux compile in 64 bits by default. While it is possible to compile in 32-bit in such an environment, and this page will detail how, your life will be easier if you use a 32-bit distribution.
 
== Requirements ==
 
* [http://gcc.gnu.org GCC] including G++ (4.2.2 recommended)
* [http://xml.apache.org/xerces-c/ Xerces XML parser] 2.8.x
* GLIBC 2.3.2 or above
 
64-bit:


==How to obtain==
* <code>ia32-libs</code> (or you will be told that 32-bit binaries don't exist)
The SDK is distributed via the Windows version of Steam. Get the SDK by looking under the ''View'' menu, choosing ''Tools'', and double-clicking ''Source SDK''. Run it when the installation is done, choose ''Create a mod'' and ''Source code only'', create a new directory for it (e.g. <tt>hl2sdk</tt>) and let the SDK program do the rest.
* Make sure you have the 32-bit build of Xerces.
* There is a repository which supposedly contains GCC 4.* capable source code, you can find it here:
http://hg.alliedmods.net/hl2sdks


==Distribution==
== Fixing the makefile maker ==
In general, any up-to-date 32-bit Linux distribution should do.


* Slackware 13.0 ( http://www.slackware.com/ ) is a good starting base to create your compiling environment.
{{bug|Don't use <code>~</code> for your home directory since parts of the make process will not understand it.}}
* Fedora Core 6 [http://mirrors.fedoraproject.org/publiclist/Fedora/6/] is an easy way to get the compiling requirements.


{{note|Even though it is recommended you use up-to-date software, many servers use distributions such as Debian Linux Stable, which use older software. Please check your software runs using a system such as this. Remember, software is usually more backwards compatible than forwards!}}
; Open <code>sdk_root/linux_sdk/Makefile</code>
:Most of the config options here are straightforward, except for:
:; <code>GAME_DIR</code>
:: To get this, you need to download a [[dedicated server]] from Valve. You'll also have to find/replace the names of the libraries the makefile searches for:
::* tier0_i486 > libtier0
::* vstdlib_i486 > libvstdlib
::* steam_api_i486 > libsteam_api
:; <code>CC</code>, <code>CPLUS</code>, <code>CLINK</code>
:: If the compilers are not where the makefile things, it may work to change them to just gcc or g++, without a path. Otherwise correct the path.
:: If you are compiling under 64-bit, add -m32 to the end and wrap in quotes, e.g. "g++ -m32".
:; <code>CPP_LIB</code>
:: Again, these files may not be where Valve think they are. To find them, browse to <code>/usr/</code> and search.
:: 64-bit users will probably encounter two files; choose whichever has '32' somewhere in its path.
; Open <code>sdk_root/public/tier0/platform.h</code>
: On line 46 replace <code><new.h></code> with <code><new></code>.


==Requirements==
{{todo|Currently getting "shadows template parm" errors when building server...plugins work though.}}
The following tools are required to compile the SDK under Linux. Any other version will probably cause complete failure at compile or run time. (Nothing prevents you from experimenting with the latest and greatest versions while your mod is in development, but server admins won't like you if your server library causes crashes!)


* [http://gcc.gnu.org GCC] 3.4.1 or above on the 3.x branch. (3.4.4 recommended to fix an [[SDK_Known_Issues_List#offsetof_errors|offsetof]] bug.) GCC 4.0 and 4.1 compile and link too, but don't count on your mod running perfectly; there aren't any detailed success/failure reports from such configurations.
== Building ==
* [http://xml.apache.org/xerces-c/ Xerces XML parser] 2.6.0 or above.
* GLIBC 2.3.2 or above.


==Setting up the Makefile==
Once configured correctly, making your mod should be a simple as:
As part of the compile process, the Microsoft Visual C++ Project file used to compile your mod under Windows is converted into a snippet of a Makefile. This conversion process needs to be seeded with a few configuration parameters, all of which are contained in linux_sdk/Makefile. At a minimum you should configure these parameters:


* '''MOD_PROJ''', the filename of the Windows project file used to compile your mod.
<source lang=bash>
* '''MOD_CONFIG''', the configuration set to use when compiling the Linux version of your mod. Typically this is the ''Release Win32'' option. The easiest method to determine this parameter is to leave it at default, run the make process once and then look at the Makefile snippets that are produced, choosing the appropriate one.
export LD_LIBRARY_PATH=.
* '''GAME_DIR''', the path to an installation of the game. You can get the correct files by installing the Source Dedicated Server and updating it. (This is described in a [http://forums.steampowered.com/forums/showthread.php?t=292495 thread] on the Steampowered boards). The Makefile says ''"the directory the base binaries (tier0_i486.so, etc) are located"'' but actually it wants one level up from the directory with tier0_i486.so.
make
* '''CC, CPLUS, CLINK, CPP_LIB''', these parameters should be pointed to your particular install of GCC that you wish to use for compiling your mod/plugin. In most linux distros, this is ''/usr/bin/gcc'' or ''/usr/local/bin/gcc''.
</source>
* '''XERCES_INC_DIR, XERCES_LIB_DIR''', the installation directory of the Xerces library.
{{note|Don't use "~" for your home directory since parts of the make process will not understand it.}}


Once these configuration parameters are set compiling your mod should be a simple as running:
=== Bugs ===


export LD_LIBRARY_PATH=.
{{confirm|These details are very old and may no longer be relevant.}}
make


<code>CreateInterface</code> is the only dynamic symbol you need to export. This looks like an oversight on Valve's part - removing the rest will save around 5 megs of pointless and possibly dangerous exports. Here's a version_script to fix it:
<code>CreateInterface</code> is the only dynamic symbol you need to export. This looks like an oversight on Valve's part - removing the rest will save around 5 megs of pointless and possibly dangerous exports. Here's a version_script to fix it:
Line 86: Line 108:
Since you're using GCC, you may also want to look into [[SDK Known Issues List#Getting the SDK to work under -Wall -Werror]].
Since you're using GCC, you may also want to look into [[SDK Known Issues List#Getting the SDK to work under -Wall -Werror]].


==Running your Mod==
== Running ==
To run the mod copy the file produced by the make process to <code>''mod dir''/bin/server_i486.so</code> and then run <code>srcds_run</code> with the appropriate <code>-game</code> parameter.
To run the mod copy the file produced by the make process to <code>''mod dir''/bin/server_i486.so</code> and then run <code>srcds_run</code> with the appropriate <code>-game</code> parameter.


Line 111: Line 133:


(Replace ''2.4'' if it's a different version causing complications.)
(Replace ''2.4'' if it's a different version causing complications.)
== See also ==
* [http://hg.alliedmods.net/hl2sdks A repository which supposedly contains GCC 4.* capable source code]
[[Category:Programming]]

Revision as of 07:13, 15 September 2010

Creating a Linux build of your multiplayer dedicated server or server plugin is not required, but does make it much more likely to be accepted by commercial server operators.

Note.pngNote:Building on Linux requires an existing Visual Studio project, which is converted to a makefile by Valve's vcpm tool ("Visual C++ Project to Make").

Getting Linux

There is a bewildering array of Linux distributions. If you are unsure which to use go for Ubuntu, which tries to be user-friendly. It has a "software centre" that makes installing packages simple.

64-bit

64-bit builds of Linux compile in 64 bits by default. While it is possible to compile in 32-bit in such an environment, and this page will detail how, your life will be easier if you use a 32-bit distribution.

Requirements

64-bit:

  • ia32-libs (or you will be told that 32-bit binaries don't exist)
  • Make sure you have the 32-bit build of Xerces.

Fixing the makefile maker

Icon-Bug.pngBug:Don't use ~ for your home directory since parts of the make process will not understand it.  [todo tested in ?]
Open sdk_root/linux_sdk/Makefile
Most of the config options here are straightforward, except for:
GAME_DIR
To get this, you need to download a dedicated server from Valve. You'll also have to find/replace the names of the libraries the makefile searches for:
  • tier0_i486 > libtier0
  • vstdlib_i486 > libvstdlib
  • steam_api_i486 > libsteam_api
CC, CPLUS, CLINK
If the compilers are not where the makefile things, it may work to change them to just gcc or g++, without a path. Otherwise correct the path.
If you are compiling under 64-bit, add -m32 to the end and wrap in quotes, e.g. "g++ -m32".
CPP_LIB
Again, these files may not be where Valve think they are. To find them, browse to /usr/ and search.
64-bit users will probably encounter two files; choose whichever has '32' somewhere in its path.
Open sdk_root/public/tier0/platform.h
On line 46 replace <new.h> with <new>.
Todo: Currently getting "shadows template parm" errors when building server...plugins work though.

Building

Once configured correctly, making your mod should be a simple as:

export LD_LIBRARY_PATH=.
make

Bugs

Confirm:These details are very old and may no longer be relevant.

CreateInterface is the only dynamic symbol you need to export. This looks like an oversight on Valve's part - removing the rest will save around 5 megs of pointless and possibly dangerous exports. Here's a version_script to fix it:

$ cat mod/src/linux_sdk/version_script
VERS_1.1 {
global:
  CreateInterface;
local:
  *;
};

Several people have reported a bug that "export LD_LIBRARY_PATH=~/source/bin" was required before vcpm would run. Here's a patch to fix that bug. (If you haven't applied the SDK Known Issues List#First_big_pass_at_-Wall_and_consistent_code fixes, you will have to take out the -D changes below.)

The newer version of the SDK also makes symlinks to prevent trouble with tier0_i486.so.

--- Makefile.vcpm       Sat Oct  1 22:26:26 2005
+++ Makefile.vcpm       Sat Oct  1 22:25:43 2005
@@ -14,12 +14,12 @@
 TIER1_OBJ_DIR=$(BUILD_OBJ_DIR)/vcpm/public

 #we use custome CFLAGS because the base ones interfere with XERCES
-CFLAGS= -w -fpermissive -D_LINUX -DNDEBUG -D_alloca=alloca -D_snprintf=snprintf -D_vsnprintf=vsnprintf $(ARCH_CFLAGS)
+CFLAGS= -w -fpermissive -D_LINUX -DNDEBUG $(ARCH_CFLAGS)
 #DEBUG = -g -ggdb
 #CFLAGS+= $(DEBUG)

 INCLUDEDIRS=-I$(PUBLIC_SRC_DIR) -I$(XERCES_INC_DIR) -I$(UTIL_COMMON_SRC_DIR) -I$(TIER1_PUBLIC_SRC_DIR)
-LDFLAGS_VC=-lm -ldl -L$(XERCES_LIB_DIR) -lxerces-c $(GAME_DIR)/bin/tier0_i486.so $(GAME_DIR)/bin/vstdlib_i486.so
+LDFLAGS_VC=-lm -ldl -Wl,-rpath -Wl,$(GAME_DIR)/bin -L$(XERCES_LIB_DIR) -lxerces-c $(GAME_DIR)/bin/tier0_i486.so $(GAME_DIR)/bin/vstdlib_i486.so

 DO_CC=$(CPLUS) $(INCLUDEDIRS) -w $(CFLAGS) -DARCH=$(ARCH) -o $@ -c $<

@@ -51,7 +51,7 @@
        $(DO_CC)

 $(TIER1_OBJ_DIR)/%.o: $(TIER1_SRC_DIR)/%.cpp
-       $(DO_CC) -Dstricmp=strcasecmp -Dstrcmpi=strcasecmp
+       $(DO_CC)

 clean:
        -rm -rf $(VCPM_OBJ_DIR)

This will compile the vcpm program, run this against your VCProject file and then use the resulting Makefile snippet and the configuration parameters you set above to compile your mod.

Since you're using GCC, you may also want to look into SDK Known Issues List#Getting the SDK to work under -Wall -Werror.

Running

To run the mod copy the file produced by the make process to mod dir/bin/server_i486.so and then run srcds_run with the appropriate -game parameter.

Miscellaneous information

How can I know if my GCC version will work?

First, try compiling the SDK whole. If it doesn't work, then you'll have to choose a different version.

If it worked, a factor might cause silent failures later on: the version of the Standard C++ Library. You can consult the list of GCC and libstdc++ versions in the libstdc++ documentation. The major version number (first of the three) of the library is increased each time a change that breaks backward compatibility is performed. The current builds of the Source Dedicated Server require version 6; any other version, older or newer, will not work.

GLIBC version caveats

The C library, just like the C++ library, is a bit demanding. You can't expect server administrators to upgrade straight away to bleeding-edge (on the contrary, they take their time, mainly with such important components such as libc), so it's your job to make the library work on as many systems as possible.

Each new GCC version tends to require newer GLIBC versions. GLIBC is designed in a way that if you don't use any backwards-incompatible functions, your library will function on systems with older GLIBC versions than the one you've used while compiling.

To look at the requirements of your current file, look at the last section of the output of this command:

readelf -V library.so

Currently, the best compatibility/functionality tradeoff would be achieved with version 2.3, which is available on Ubuntu since "warty", on Debian since at least "sarge", on Fedora Core since Core 1 and OpenSUSE since 10.0. (Newer distribution releases will contain newer GLIBC versions, but, fortunately, there is forward compatibility.)

If, therefore, the readelf output contains a higher number than GLIBC_2.3, you might be using some features maybe too advanced for some systems. (With GCC 4.1, you'll have to add -fno-stack-protector to your USER_CFLAGS to turn off such a feature (the stack-thrash protector). If you want to know which potentially bad functions are being referenced, run the following:

nm library.so | grep GLIBC_2.4

(Replace 2.4 if it's a different version causing complications.)

See also