41. Programming part 2: External libraries

Various functions are needed to create a game server. You may also need to add functions to follow publisher or platform rules. You sometimes need to connect to external libraries to do this. In such cases, you can use external libraries as follows:

Here, we link to a crypto library called sodium as an example.

41.1. Installing external libraries

Install the library you want to use. Most libraries are in the form of Ubuntu or Centos packages. If a package exists for the relevant library, it is convenient to use the OS package manager (apt for Ubuntu, yum for Centos).

Note

Packages for Ubuntu or Centos are often divided into packages containing the library file and packages including the header file. You only need the library package after the game server is first built, but you need to install the header file package to build the game server (that is, to compile). Header file packages in Ubuntu usually end in -dev, while packages in Centos usually end in -devel.

The following is an example using the sodium library:

Ubuntu

$ sudo apt-get install libsodium-dev

CentOS

$ sudo yum install libsodium-devel

41.2. Add to CMakeLists.txt

In the source directory generated with funapi_initiator,

  • CMakeLists.txt
  • src/CMakeLists.txt

As such, there are two build configuration files. Modify the second file, src/CMakeLists.txt, to add the following. More details follow the code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...

###############################################################################
# Inclusion after setting variables.
###############################################################################

set(CMAKE_MODULE_PATH "/usr/share/funapi/cmake")

include(Funapi-src)

###############################################################################
# 추가 라이브러리 링크하기 위한 섹션
###############################################################################
# libsodium 라이브러리를 찾아서 LIBSODIUM 에 저장한다.
find_library(
  LIBSODIUM
  NAMES libsodium.so
  HINTS /usr/lib /usr/lib64
  REQUIRED)

# 위의 LIBSODIUM 에 저장되어있는 경로를 링크 대상으로 추가한다.
target_link_libraries(${PROJECT_NAME} ${LIBSODIUM})

...

41.2.1. Finding libraries

Use CMake’s find_library to find libraries. An explanation of the find_library parameters used above is as follows.

  • LIBSODIUM: Variable name to store the library path. Choose an appropriate name.
  • NAMES {library file names}: This is the actual name of the library file. If it is a static library, the file ends in .a rather than .so.
  • HINTS {library directory}: If the library is not in the default path, the relevant path must be specified. This can be omitted if the library is in the default path.
  • REQUIRED: Sets the build to fail if the relevant library isn’t there.

41.2.2. Linking libraries

Use CMake’s target_link_libraries to link to the relevant libraries.

If the name specified previously (LIBSODIUM in the example above) is sent as a parameter, this library is added when the linker links.

41.3. Distributing to service environments

If you use target_link_libraries and package the game server as explained in Server management part 3: Server packaging, dependencies for the library used are added automatically.

When a game server generated as .deb or .rpm is installed, it checks whether the library we added in the OS is installed. If it is not, an error message pops up and the game server package installation is declined. This immediately lets us know the necessary library is missing.

If this error message appears, you can install the necessary library as explained in Installing external libraries. For our libsodium example above, you can install the library as follows:

Ubuntu

$ sudo apt-get install libsodium18

Note

It may be a number other than 18 depending on your Ubuntu version.

CentOS

$ sudo yum install libsodium

Tip

Since the game server build is finished and distributed to the live server, development packages like -dev or -devel are not necessary.