Working With External Libraries

You may need to rely on external libraries files to implement your game contents or to integrate with the authentication/billing systems of your publisher. In such cases, you need to tell the build chain which external libraries it should include and where to find them. This example illustrates how to include a crypto library named sodium.

Installing external libraries

In general, you may be able to find Ubuntu/Centos packages corresponding to the external libraries. In this case, package managers (apt on Ubuntu or yum on Centos) are the best way to install the packages.

Note

Both Ubuntu and Centos typically pack header files into a package separated from the library package. Running a built game server does not require header files, but it’s required to have header files to build a game server out of your sources files using the library. Package for header files is suffixed with -dev on Ubuntu and -devel on Centos.

For the example of the sodium library:

Ubuntu

$ sudo apt-get install libsodium-dev

CentOS

$ sudo yum install libsodium-devel

Including the library in CMakeLists.txt

The source directory generated by funapi_initiator has two CMakeLists.txt files.

  • CMakeLists.txt

  • src/CMakeLists.txt

And we need to edit the second file to tell the build chain that we need the library. Details are given after the code snippet.

# ... (omitted)

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

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

include(Funapi-src)

###############################################################################
# Section to include external libraries.
###############################################################################
# Finds the library location using ``find_library`` and store the path to LIBSODIUM.
find_library(
  LIBSODIUM
  NAMES libsodium.so
  HINTS /usr/lib /usr/lib64
  REQUIRED)

# Appends the path string stored in LIBSODIUM to the linkage list.
target_link_libraries(${PROJECT_NAME} ${LIBSODIUM})

# ... (omitted)

Locating the library

We can figure out the path of the target library using the Cmake’s find_library function. Please refer to this for details.

  • LIBSODIUM: Variable to store the library path. Pick a name according to your taste.

  • NAMES {library file name}: Library file name. For static library, the extension should be .a instead of .so.

  • HINTS {라이브러리 디렉터리}: If the library is not in the default linux library path, we need to give the find_library a hint about the location.

  • REQUIRED: Tells the find_library to fail if the library file is missing.

Appending to the linkage list

After getting the path information about the library, we need to append the path to the Cmake’s linkage list via target_link_libraries. Please see here for details.

We simply need to pass the output variable (LIBSODIUM in this example).

Pack the external library to your package

As long as we properly link using the target_link_libraries function, the package result mentioned in Server management part 3: Server packaging, a resulting package in .deb or .rpm should correctly include the dependency information. So, there is nothing more you should do for the additional dependency.

Both apt and yum refuse to install a package if the package’s dependency is not satisfied. This is useful as this guarantees that your game server package is installed only after all the dependency has been satisfied.

For the sodium library example, we will see an error message installing the game server package if sodium library has not been installed, yet. In this case, we can install the library like this:

Ubuntu

$ sudo apt-get install libsodium18

Note

Different Ubuntu versions may need to install different library versions (other than 18)

CentOS

$ sudo yum install libsodium

Tip

Since we are about to install the game server package after building the game server, we do not need development package suffixed by either -dev or -devel.