Difference between revisions of "Compiling under Linux"

From Valve Developer Community
Jump to: navigation, search
(GCC 4.1 should work. Some restructuring, some adding.)
 
(34 intermediate revisions by 17 users not shown)
Line 1: Line 1:
[[Category:Programming]]
+
{{otherlang2
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.
+
|title=Compiling under Linux
 +
|ru=Compiling_under_Linux:ru
 +
}}
 +
{{Cleanup}}
 +
{{toc-right}}
 +
 
 +
'''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|This page assumes you are building for [[Source 2007]] (Orange Box).}}
 +
 
 +
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 ==
 +
 
 +
If you are unsure which version of Linux 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, and can be run from a CD if you want to experiment (but you'll want to install it permanently before you start digging in).
 +
 
 +
== Requirements ==
  
==How to obtain==
+
* [http://gcc.gnu.org GCC and G++ 4.2.x] or lower
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.
+
* [[Vprojtomake 2010]] '''or''' [http://xml.apache.org/xerces-c/ Xerces XML parser] 2.8.x
 +
* <code>libstdc++</code> 6
 +
* <code>libc</code> 2.4 or higher
  
==Requirements==
+
If you are running 64-bit Linux:
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.) All versions until including 4.1.x should work too, since no changes were done to the ABI (Application Binary Interface) that would break compatibility.
+
* <code>ia32-libs</code> (or you will be told that 32-bit binaries don't exist)
* [http://xml.apache.org/xerces-c/ Xerces XML parser] 2.6.0 or above.
+
* GCC multilib for your GCC release
* GLIBC 2.3.2 or above.
+
* Make sure you have the 32-bit build of Xerces, if you haven't upgraded to Vprojtomake 2010.
  
Slackware 11.0 ( http://www.slackware.com/ ) is a good starting base to create your compiling environment.
+
== Setting up ==
  
==Setting up the Makefile==
+
{{bug|Don't use <code>~</code> for your home directory. Parts of the make process do not understand it.}}
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.
+
Open <code>sdk_root/linux_sdk/Makefile</code>. Most of the config options here are straightforward, except for:
* '''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.
 
* '''GAME_DIR''', the path to an installation of the game. 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.
 
* '''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.
 
* '''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:
+
; <code>MOD_CONFIG</code>
 +
: These values should come straight out of your VS project. Remove all whitespace. To build the 'My Server' project in release mode, this should read <code>MyServer_ReleaseWin32</code>.
 +
; <code>GAME_DIR</code>
 +
: To get this, you need to download a [[dedicated server]] from Valve. You need the <code>orangebox</code> game.
 +
; <code>CC</code>, <code>CPLUS</code>, <code>CLINK</code>
 +
: Change these to read "gcc -m32" or "g++ -m32", with quotes. If your system's default build of GCC is too recent, specify an older version with "gcc-4.2 -m32" or similar; check <code>/usr/bin</code> to see what you've got installed.
 +
; <code>CPP_LIB</code>
 +
: These files may not be where Valve think they are. To find them, browse to <code>/usr/lib</code> and search. 64-bit users will encounter two version of each file; choose the ones in the '32' folder.
  
make
+
* [[Server plugin]]s need extra configuration. See [[Server_plugins#Compiling]].
 +
* Experienced GCC programmers may be interested in [[SDK Known Issues List#Getting the SDK to work under -Wall -Werror|Getting the SDK to work under -Wall -Werror]].
  
<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. (It is much easier for other people to disassemble your code when you export all of your functions.) Here's a version_script to fix it:
+
== Making ==
  
<pre>
+
Once everything is configured correctly, you can build your mod by navigating to the <code>linux_sdk</code> folder and performing a <code>make</code>.
$ cat mod/src/linux_sdk/version_script
 
VERS_1.1 {
 
global:
 
  CreateInterface;
 
local:
 
  *;
 
};
 
</pre>
 
  
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.)
+
{{note|If you've done a lot of development on Windows you are likely to encounter "No such file or directory" or "no rule to make target" errors (if you get a mess of errors, scroll up to the very first one). These should all be down to two easily-rectified issues:
  
<pre>
+
* Paths are case-sensitive in Linux. <code>/Multiplayer</code> is not the same as <code>/multiplayer</code>
--- Makefile.vcpm      Sat Oct  1 22:26:26 2005
+
* Paths must delimited with <code>/</code> characters in Linux, not Windows' <code>\</code>.
+++ 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
+
Making these changes to your source files will not affect compiling on Windows.}}
-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)
+
== Running ==
-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 $<
+
To run the mod perform <code>make install</code> to copy the server binary to your mod's folder, then cd into your dedicated server folder and do <code>./srcds_run</code> with the appropriate <code>-game</code> parameter.
  
@@ -51,7 +51,7 @@
+
<!-- 2.4 was released in 2006, and can be assumed present on modern-day Linux systems
        $(DO_CC)
+
== libc ==
  
$(TIER1_OBJ_DIR)/%.o: $(TIER1_SRC_DIR)/%.cpp
+
The version of the C runtime library on a system running your server code can affect stability. To look at the requirements of your binary, examine the last section of the output of this command:
-      $(DO_CC) -Dstricmp=strcasecmp -Dstrcmpi=strcasecmp
 
+      $(DO_CC)
 
  
clean:
+
<source lang=bash>
        -rm -rf $(VCPM_OBJ_DIR)
+
readelf -V your_server.so
</pre>
+
</source>
  
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.
+
The highest version of GLIBC reported is the one that your binary requires. You will have to consider where to draw the line between backwards-compatibility and functionality; as the SDK stands the highest version you will see is probably 2.4, [http://ftp.gnu.org/gnu/glibc/?C=M;O=D which was released in 2006] and is highly likely to be present on target systems.
  
Since you're using GCC, you may also want to look into [[SDK Known Issues List#Getting the SDK to work under -Wall -Werror]].
+
If you want to see precisely which functions require a given version of libc, run the following:
  
==Running your Mod==
+
<source lang=bash>
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.
+
nm your_server.so | grep GLIBC_<VERSION>
 +
</source>
 +
-->
  
==Miscellaneous information==
+
[[Category:Programming]]
===How can I know if my GCC version will work?===
+
[[Category:Linux]]
You can consult the list of GCC and libstdc++ versions in the [http://gcc.gnu.org/onlinedocs/libstdc++/abi.html 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'''.
 

Latest revision as of 11:17, 27 August 2016

Русский

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:This page assumes you are building for Source 2007 (Orange Box).

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

If you are unsure which version of Linux to use, go for Ubuntu, which tries to be user-friendly. It has a "software centre" that makes installing packages simple, and can be run from a CD if you want to experiment (but you'll want to install it permanently before you start digging in).

Requirements

If you are running 64-bit Linux:

  • ia32-libs (or you will be told that 32-bit binaries don't exist)
  • GCC multilib for your GCC release
  • Make sure you have the 32-bit build of Xerces, if you haven't upgraded to Vprojtomake 2010.

Setting up

Bug: Don't use ~ for your home directory. Parts of the make process do not understand it.

Open sdk_root/linux_sdk/Makefile. Most of the config options here are straightforward, except for:

MOD_CONFIG
These values should come straight out of your VS project. Remove all whitespace. To build the 'My Server' project in release mode, this should read MyServer_ReleaseWin32.
GAME_DIR
To get this, you need to download a dedicated server from Valve. You need the orangebox game.
CC, CPLUS, CLINK
Change these to read "gcc -m32" or "g++ -m32", with quotes. If your system's default build of GCC is too recent, specify an older version with "gcc-4.2 -m32" or similar; check /usr/bin to see what you've got installed.
CPP_LIB
These files may not be where Valve think they are. To find them, browse to /usr/lib and search. 64-bit users will encounter two version of each file; choose the ones in the '32' folder.

Making

Once everything is configured correctly, you can build your mod by navigating to the linux_sdk folder and performing a make.

Note:If you've done a lot of development on Windows you are likely to encounter "No such file or directory" or "no rule to make target" errors (if you get a mess of errors, scroll up to the very first one). These should all be down to two easily-rectified issues:
  • Paths are case-sensitive in Linux. /Multiplayer is not the same as /multiplayer
  • Paths must delimited with / characters in Linux, not Windows' \.
Making these changes to your source files will not affect compiling on Windows.

Running

To run the mod perform make install to copy the server binary to your mod's folder, then cd into your dedicated server folder and do ./srcds_run with the appropriate -game parameter.