| 1 | \page buildcmake Building a Discord Bot Using CMake (UNIX) |
| 2 | |
| 3 | \warning **This tutorial will assume that you have already installed DPP.** If you haven't, please head over to \ref install-linux-deb "this page", or any install page that matches your OS. If you want to use source, then continue your journey over at \ref buildlinux "this page" for a full explanation into using CMake with source. |
| 4 | |
| 5 | ## 1. Toolchain |
| 6 | Before continuing, you will need to install `cmake` on your system. To be sure that `cmake` is installed, you can type the following command: |
| 7 | |
| 8 | ```bash |
| 9 | $ cmake --version |
| 10 | cmake version 3.22.1 |
| 11 | ``` |
| 12 | |
| 13 | 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! |
| 14 | |
| 15 | ## 2. Create a CMake project |
| 16 | |
| 17 | In an empty directory, create the following files and directories: |
| 18 | |
| 19 | \dot |
| 20 | digraph "Example Directory" { |
| 21 | graph [ranksep=1]; |
| 22 | node [colorscheme="blues9", fontname="helvetica"]; |
| 23 | |
| 24 | "Your Directory" [style=filled, color=1, shape=rect] |
| 25 | |
| 26 | subgraph cluster_0 { |
| 27 | style=filled; |
| 28 | color=lightgrey; |
| 29 | node [style=filled, color=2, shape=rect] |
| 30 | build; |
| 31 | cmake; |
| 32 | src; |
| 33 | "CMakeLists.txt"; |
| 34 | label = "The main area for your bot's files."; |
| 35 | } |
| 36 | |
| 37 | subgraph cluster_1 { |
| 38 | style=filled; |
| 39 | color=lightgrey; |
| 40 | node [style=filled, color=3, shape=rect] |
| 41 | "FindDPP.cmake"; |
| 42 | label = "This file is to tell CMake where DPP is."; |
| 43 | } |
| 44 | |
| 45 | subgraph cluster_2 { |
| 46 | style=filled; |
| 47 | color=lightgrey; |
| 48 | node [style=filled, color=3, shape=rect] |
| 49 | "main.cpp"; |
| 50 | "main.h"; |
| 51 | "more code..."; |
| 52 | label = "This is where your bot's code will go."; |
| 53 | } |
| 54 | |
| 55 | "Your Directory" -> build; |
| 56 | "Your Directory" -> src; |
| 57 | "Your Directory" -> cmake; |
| 58 | "Your Directory" -> "CMakeLists.txt"; |
| 59 | |
| 60 | cmake -> "FindDPP.cmake"; |
| 61 | |
| 62 | src -> "main.cpp"; |
| 63 | src -> "main.h"; |
| 64 | src -> "more code..."; |
| 65 | } |
| 66 | \enddot |
| 67 | |
| 68 | ## 3. Configure CMake |
| 69 | |
| 70 | You'll need to modify the `CMakeLists.txt` to tell CMake what it's looking for, and other information. |
| 71 | |
| 72 | Here is an example CMake configuration, you can adapt it according to your needs: |
| 73 | |
| 74 | ~~~~~~~~~~~~~~cmake |
| 75 | # Minimum CMake version required, we'll just use the latest version. |
| 76 | cmake_minimum_required(VERSION 3.22) |
| 77 | # Project name, version and description |
| 78 | project(discord-bot VERSION 1.0 DESCRIPTION "A discord bot") |
| 79 | |
| 80 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) |
| 81 | |
| 82 | # Create an executable |
| 83 | add_executable(${PROJECT_NAME} |
| 84 | src/main.cpp |
| 85 | ) |
| 86 | |
| 87 | # Find our pre-installed DPP package (using FindDPP.cmake). |
| 88 | find_package(DPP REQUIRED) |
| 89 | |
| 90 | # Link the pre-installed DPP package. |
| 91 | target_link_libraries(${PROJECT_NAME} |
| 92 | ${DPP_LIBRARIES} |
| 93 | ) |
| 94 | |
| 95 | # Include the DPP directories. |
| 96 | target_include_directories(${PROJECT_NAME} PRIVATE |
| 97 | ${DPP_INCLUDE_DIR} |
| 98 | ) |
| 99 | |
| 100 | # Set C++ version |
| 101 | set_target_properties(${PROJECT_NAME} PROPERTIES |
| 102 | CXX_STANDARD 20 |
| 103 | CXX_STANDARD_REQUIRED ON |
| 104 | ) |
| 105 | ~~~~~~~~~~~~~~ |
| 106 | |
| 107 | We'll also need to populate our `FindDPP.cmake` file, inside the `cmake` directory! |
| 108 | |
| 109 | Here's what you should use: |
| 110 | |
| 111 | ~~~~~~~~~~~~~~cmake |
| 112 | find_path(DPP_INCLUDE_DIR NAMES dpp/dpp.h HINTS ${DPP_ROOT_DIR}) |
| 113 | |
| 114 | find_library(DPP_LIBRARIES NAMES dpp "libdpp.a" HINTS ${DPP_ROOT_DIR}) |
| 115 | |
| 116 | include(FindPackageHandleStandardArgs) |
| 117 | |
| 118 | find_package_handle_standard_args(DPP DEFAULT_MSG DPP_LIBRARIES DPP_INCLUDE_DIR) |
| 119 | ~~~~~~~~~~~~~~ |
| 120 | |
| 121 | ## 4. Build the bot. |
| 122 | |
| 123 | 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 ..`. |
| 124 | |
| 125 | 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. |
| 126 | |
| 127 | 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! |
| 128 | |
| 129 | **Have fun!** |