buildcmake

# Building a Discord Bot Using CMake (UNIX)

This tutorial will assume that you have already installed DPP. If you haven't, please head over to this page, or any install page that matches your OS. If you want to use source, then continue your journey over at this page for a full explanation into using CMake with source.

## 1. Toolchain

Before continuing, you will need to install `cmake` on your system. To be sure that `cmake` is installed, you can type the following command:

    $cmake--version
    cmakeversion3.22.1

If your CMake version is not as shown above then don't worry! You can still follow along, even if you're ahead or behind!

## 2. Create a CMake project

In an empty directory, create the following files and directories:

digraph "Example Directory" { graph [ranksep=1]; node [colorscheme="blues9", fontname="helvetica"]; "Your Directory" [style=filled, color=1, shape=rect] subgraph cluster_0 { style=filled; color=lightgrey; node [style=filled, color=2, shape=rect] build; cmake; src; "CMakeLists.txt"; label = "The main area for your bot's files."; } subgraph cluster_1 { style=filled; color=lightgrey; node [style=filled, color=3, shape=rect] "FindDPP.cmake"; label = "This file is to tell CMake where DPP is."; } subgraph cluster_2 { style=filled; color=lightgrey; node [style=filled, color=3, shape=rect] "main.cpp"; "main.h"; "more code..."; label = "This is where your bot's code will go."; } "Your Directory" -> build; "Your Directory" -> src; "Your Directory" -> cmake; "Your Directory" -> "CMakeLists.txt"; cmake -> "FindDPP.cmake"; src -> "main.cpp"; src -> "main.h"; src -> "more code..."; }

## 3. Configure CMake

You'll need to modify the `CMakeLists.txt` to tell CMake what it's looking for, and other information.

Here is an example CMake configuration, you can adapt it according to your needs:

    #MinimumCMakeversionrequired,we'lljustusethelatestversion.
    cmake_minimum_required(VERSION3.22)
    #Projectname,versionanddescription
    project(discord-botVERSION1.0DESCRIPTION"Adiscordbot")

    list(APPENDCMAKE_MODULE_PATH${CMAKE_CURRENT_SOURCE_DIR}/cmake)

    #Createanexecutable
    add_executable(${PROJECT_NAME}
    src/main.cpp
    )

    #Findourpre-installedDPPpackage(usingFindDPP.cmake).
    find_package(DPPREQUIRED)

    #Linkthepre-installedDPPpackage.
    target_link_libraries(${PROJECT_NAME}
    ${DPP_LIBRARIES}
    )

    #IncludetheDPPdirectories.
    target_include_directories(${PROJECT_NAME}PRIVATE
    ${DPP_INCLUDE_DIR}
    )

    #SetC++version
    set_target_properties(${PROJECT_NAME}PROPERTIES
    CXX_STANDARD20
    CXX_STANDARD_REQUIREDON
    )

We'll also need to populate our `FindDPP.cmake` file, inside the `cmake` directory!

Here's what you should use:

    find_path(DPP_INCLUDE_DIRNAMESdpp/dpp.hHINTS${DPP_ROOT_DIR})

    find_library(DPP_LIBRARIESNAMESdpp"libdpp.a"HINTS${DPP_ROOT_DIR})

    include(FindPackageHandleStandardArgs)

    find_package_handle_standard_args(DPPDEFAULT_MSGDPP_LIBRARIESDPP_INCLUDE_DIR)

## 4. Build the bot.

Now that we have our all our cmake stuff setup and we've got our code in place, we can initalise CMake. You'll want to go inside the `build/` directory and do `cmake ..`.

Once that's completed, you'll want to head back to your up-most folder (where all the folders are for your bot) and run `cmake --build build/ -j4` (replace -j4 with however many threads you want to use). This will start compiling your bot and creating the executable.

After that has finished, you can head into `build/` and run your bot by doing `./discord-bot`! If everything went well, you should see your bot come online!

Have fun!