Skip to content

Setting up GDK with CMake

Zeex edited this page Jul 2, 2015 · 31 revisions

Update: Please use the dynamic version of the GDK (that is, libsampgdk.so.* or sampgdk4.dll) if there's a chance that your plugin will be running alongside with other GDK-based plugin(s) or you're planning to load such plugins together with your plugin (for example, streamer).

This tutorial will show you how to set up a new GDK project using CMake, a popular cross-platform build system, step by step.

  1. Create a new directory for your project. I'll refer to this directory as PROJECT_SOURCE_DIR throughout this tutorial.

  2. Download the SA-MP plugin SDK and extract the contents to PROJECT_SOURCE_DIR. You can download it from my samp-plugin-sdk repo or elsewhere.

  3. Download sampgdk-x.y.z-amalgamation.zip from this page and extract sampgdk.c and sampgdk.h to PROJECT_SOURCE_DIR.

  4. Download AMXConfig.cmake and AddSAMPPlugin.cmake here and save them to PROJECT_SOURCE_DIR/cmake/.

  5. Download the source code of helloworld: helloworld.cpp and helloworld.def - and save the two files to PROJECT_SOURCE_DIR.

  6. Replace all #include <sampgdk/...> lines in helloworld.cpp with the single line #include "sampgdk.h".

  7. At this point you should have a directory structure as follows:

    -- PROJECT_SOURCE_DIR
       |
       |-- amx
       |   |-- amx.h
       |   |-- getch.h
       |   |-- sclinx.h
       |
       |-- cmake
       |   |-- AMXConfig.cmake
       |   |-- AddSAMPPlugin.cmake
       |
       |-- amxplugin.cpp
       |-- plugin.h
       |-- plugincommon.h
       |-- helloworld.cpp
       |-- helloworld.def
       |-- sampgdk.c
       |-- sampgdk.h
    
  8. Next, create a file called CMakeLists.txt in PROJECT_SOURCE_DIR with the following contents:

    project(helloworld)
    
    cmake_minimum_required(VERSION 2.8)
    list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
    
    include(AMXConfig)
    include(AddSAMPPlugin)
    
    include_directories(
      ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/amx
    )
    
    add_definitions(-DSAMPGDK_AMALGAMATION)
    
    add_samp_plugin(helloworld
      amxplugin.cpp
      helloworld.cpp
      helloworld.def
      sampgdk.c
      sampgdk.h
    )
  9. Download and install CMake.

    CMake is a cross-platform "meta" build system that generates projects for various IDEs and build systems, such as Visual Studio solutions or Makefiles.

    Most Linux distributions provide packages for CMake. For example, on Ubuntu it can be installed via apt-get:

    sudo apt-get install cmake

    Obviously you'll need a C++ compiler as well, so you might want to install something like GCC or Visual Studio 2013 Express.

    In the rest of this tutorial I'll use cmake-gui, the GUI frontend for CMake that ships with the main CMake installation. On Linux it most likely comes in a separate package that is called something like cmake-qt-gui or similar.

    If you're comfortable with the command line, you can stop right here and run the following command instead:

    cmake PROJECT_SOURCE_DIR -G "Your Generator"

    where Your Generator is one of the generators supported by CMake (run cmake --help for the list of all generators). This will output a project file or a makefile, all left to do is build the project.

  10. Open up CMake GUI (Start Menu -> Programs -> CMake or cmake-gui command) and fill in the following fields:

    Where is the source code

    This is the full path to your PROJECT_SOURCE_DIR.

    Where to build the binaries

    This is the directory where output files will be created. You can choose any directory you wish. Usually it's something like PROJECT_ROOT/build.

    Let's call it PROJECT_BINARY_DIR.

  11. Press Configure.

  12. Select a generator of your choice.

    If you're using Visual Studio 2013 choose "Visual Studio 12 2013". If you're building on Linux it probably should be "Unix Makefiles".

  13. Press Generate.

  14. CMake has generated your Visual Studio solution or Makefile and saved it to PROJECT_BINARY_DIR.

  15. Now you can build the project.

Useful links

Clone this wiki locally