-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: added more info to the buildcmake page. Merged cmake.md with #820…
…. (#872) Co-authored-by: Jakub 'Eremiell' Marek <[email protected]>
- Loading branch information
1 parent
8ffbe74
commit 206dd77
Showing
1 changed file
with
94 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,129 @@ | ||
\page buildcmake Building a Discord Bot using CMake/UNIX | ||
\page buildcmake Building a Discord Bot Using CMake (UNIX) | ||
|
||
\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. | ||
|
||
## 1. Toolchain | ||
Before compiling, you will need to install `cmake` on your system. | ||
To be sure that `cmake` is installed, you can type the following command: | ||
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 | ||
cmake version 3.20.4 | ||
```bash | ||
$ cmake --version | ||
cmake version 3.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: | ||
|
||
- your_project/ | ||
|-- libs/ | ||
|-- src/ | ||
|-- main.cpp | ||
|-- CMakeLists.txt | ||
|
||
|
||
In the `libs/` directory, clone D++ with: `git clone https://github.com/brainboxdotcc/DPP.git` | ||
\dot | ||
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..."; | ||
} | ||
\enddot | ||
|
||
## 3. Configure CMake | ||
|
||
Here is an example CMake configuration, adapt it according to your needs: | ||
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: | ||
|
||
~~~~~~~~~~~~~~{.cmake} | ||
# minimum CMake version required | ||
cmake_minimum_required(VERSION 3.15) | ||
~~~~~~~~~~~~~~cmake | ||
# Minimum CMake version required, we'll just use the latest version. | ||
cmake_minimum_required(VERSION 3.22) | ||
# Project name, version and description | ||
project(discord-bot VERSION 1.0 DESCRIPTION "A discord bot") | ||
# Add DPP as dependency | ||
add_subdirectory(libs/DPP) | ||
# You can also add any other libs you want to use | ||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) | ||
# Create an executable | ||
add_executable(${PROJECT_NAME} | ||
src/main.cpp | ||
# your other files... | ||
src/main.cpp | ||
) | ||
# Linking libraries | ||
target_link_libraries(${PROJECT_NAME} | ||
dpp | ||
# Add any other libs you want to use here | ||
# Find our pre-installed DPP package (using FindDPP.cmake). | ||
find_package(DPP REQUIRED) | ||
# Link the pre-installed DPP package. | ||
target_link_libraries(${PROJECT_NAME} | ||
${DPP_LIBRARIES} | ||
) | ||
# Specify includes | ||
# Include the DPP directories. | ||
target_include_directories(${PROJECT_NAME} PRIVATE | ||
libs/DPP/include | ||
# Remember to add the include directories of any other libraries too | ||
${DPP_INCLUDE_DIR} | ||
) | ||
# Set C++ version | ||
set_target_properties(${PROJECT_NAME} PROPERTIES | ||
CXX_STANDARD 17 | ||
CXX_STANDARD_REQUIRED ON | ||
CXX_STANDARD 17 | ||
CXX_STANDARD_REQUIRED ON | ||
) | ||
~~~~~~~~~~~~~~ | ||
|
||
Your project directory should look like this: | ||
We'll also need to populate our `FindDPP.cmake` file, inside the `cmake` directory! | ||
|
||
- your_project/ | ||
|-- libs/ | ||
|-- DPP | ||
|-- src/ | ||
|-- main.cpp | ||
|-- CMakeLists.txt | ||
Here's what you should use: | ||
|
||
~~~~~~~~~~~~~~cmake | ||
find_path(DPP_INCLUDE_DIR NAMES dpp/dpp.h HINTS ${DPP_ROOT_DIR}) | ||
**Have fun!** | ||
find_library(DPP_LIBRARIES NAMES dpp "libdpp.a" HINTS ${DPP_ROOT_DIR}) | ||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(DPP DEFAULT_MSG DPP_LIBRARIES DPP_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!** |