Compiling under Linux: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Setting bug notice hidetested=1 param on page where the bug might not need tested in param specified)
 
(19 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{LanguageBar}}
{{Outdated sdk|{{src07|4.1}}|newer-sdk={{src13|4.1}}|newer-sdk-equivalent=[[Source_SDK_Base_2013#Setting_up_&_compiling_code|Setting up Source 2013 (Platform Specific)]]}}
{{cleanup}}
{{toc-right}}
{{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.
'''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").}}
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 ==
== 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.
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).
 
=== 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 ==
== Requirements ==


* [http://gcc.gnu.org GCC] including G++ (4.2.2 recommended)
* [http://gcc.gnu.org GCC and G++ 4.2.x] or lower
* [http://xml.apache.org/xerces-c/ Xerces XML parser] 2.8.x
* [[Vprojtomake 2010]] '''or''' [http://xml.apache.org/xerces-c/ Xerces XML parser] 2.8.x
* GLIBC 2.3.2 or above
* <code>libstdc++</code> 6
* <code>libc</code> 2.4 or higher


64-bit:
If you are running 64-bit Linux:


* <code>ia32-libs</code> (or you will be told that 32-bit binaries don't exist)
* <code>ia32-libs</code> (or you will be told that 32-bit binaries don't exist)
* Make sure you have the 32-bit build of Xerces.
* GCC multilib for your GCC release
 
* Make sure you have the 32-bit build of Xerces, if you haven't upgraded to Vprojtomake 2010.
== Fixing the makefile maker ==
 
{{bug|Don't use <code>~</code> for your home directory since parts of the make process will not understand it.}}
 
; 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 thinks, change them to just gcc or g++, without a path. If that doesn't work either they probably aren't installed!
:: 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>.
 
{{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:
 
<source lang=bash>
export LD_LIBRARY_PATH=.
make
</source>
 
=== Bugs ===
 
{{confirm|These details are very old and may no longer be relevant.}}
 
<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:
 
<pre>
$ 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.)
== Setting up ==


The newer version of the SDK also makes symlinks to prevent trouble with tier0_i486.so.
{{bug|hidetested=1|Don't use <code>~</code> for your home directory. Parts of the make process do not understand it.}}


<pre>
Open <code>sdk_root/linux_sdk/Makefile</code>. Most of the config options here are straightforward, except for:
--- 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
; <code>MOD_CONFIG</code>
-CFLAGS= -w -fpermissive -D_LINUX -DNDEBUG -D_alloca=alloca -D_snprintf=snprintf -D_vsnprintf=vsnprintf $(ARCH_CFLAGS)
: 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>.
+CFLAGS= -w -fpermissive -D_LINUX -DNDEBUG $(ARCH_CFLAGS)
; <code>GAME_DIR</code>
#DEBUG = -g -ggdb
: To get this, you need to download a [[dedicated server]] from Valve. You need the <code>orangebox</code> game.
#CFLAGS+= $(DEBUG)
; <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.


INCLUDEDIRS=-I$(PUBLIC_SRC_DIR) -I$(XERCES_INC_DIR) -I$(UTIL_COMMON_SRC_DIR) -I$(TIER1_PUBLIC_SRC_DIR)
* [[Server plugin]]s need extra configuration. See [[Server plugins#Compiling]].
-LDFLAGS_VC=-lm -ldl -L$(XERCES_LIB_DIR) -lxerces-c $(GAME_DIR)/bin/tier0_i486.so $(GAME_DIR)/bin/vstdlib_i486.so
* 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]].
+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 $<
== Making ==


@@ -51,7 +51,7 @@
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>.
        $(DO_CC)


$(TIER1_OBJ_DIR)/%.o: $(TIER1_SRC_DIR)/%.cpp
{{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:
-      $(DO_CC) -Dstricmp=strcasecmp -Dstrcmpi=strcasecmp
+      $(DO_CC)


clean:
* Paths are case-sensitive in Linux. <code>/Multiplayer</code> is not the same as <code>/multiplayer</code>
        -rm -rf $(VCPM_OBJ_DIR)
* Paths must delimited with <code>/</code> characters in Linux, not Windows' <code>\</code>.
</pre>


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.
Making these changes to your source files will not affect compiling on Windows.}}
 
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 ==
== 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.


==Miscellaneous information==
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.
===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 [http://gcc.gnu.org/onlinedocs/libstdc++/manual/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'''.
<!-- 2.4 was released in 2006, and can be assumed present on modern-day Linux systems
== libc ==


===GLIBC version caveats===
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:
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.
<source lang=bash>
readelf -V your_server.so
</source>


To look at the requirements of your current file, look at the last section of the output of this command:
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.


readelf -V ''library''.so
If you want to see precisely which functions require a given version of libc, run the following:


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.)
<source lang=bash>
 
nm your_server.so | grep GLIBC_<VERSION>
If, therefore, the <code>readelf</code> 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 <code>-fno-stack-protector</code> 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:
</source>
 
-->
nm ''library''.so | grep GLIBC_2.4
 
(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]]
[[Category:Programming]]
[[Category:Linux]]

Latest revision as of 07:07, 20 May 2025

English (en)Русский (ru)中文 (zh)Translate (Translate)
Ambox warning yellow.png
This page was written for Source 2007 Source 2007 or earlier. It may not be applicable to newer versions of the Source SDK.
If you are using Source 2013 Source 2013, you may be looking for Setting up Source 2013 (Platform Specific).
Broom icon.png
This article or section needs to be cleaned up to conform to a higher standard of quality.
For help, see the VDC Editing Help and Wikipedia cleanup process. Also, remember to check for any notes left by the tagger at this article's talk page.

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.

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

Icon-Bug.pngBug: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.pngNote: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.