-
Only tried this project out briefly but certainly hope to spend more time with it.. I know enough C++ to be pretty keen to try to write my own code.. but not very familiar with cmake and finding it pretty overwhelming .. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Yeah cmake is a very useful tool but it is a bit intimidating when you're not familiar with it. You'll find below a small project example to start your own project. It's not much, but it should get you started. I'm assuming you're working with the following structure:
cmake_minimum_required(VERSION 3.10)
project(My_Example)
# Here we can set some values for botcraft options
# They will still appear in cmake_gui in windows
set(BOTCRAFT_BUILD_EXAMPLES OFF CACHE BOOL "")
# This will enter the Botcraft folder and
# process Botcrat/CMakeLists.txt to add its
# targets to our current project
add_subdirectory(Botcraft)
# This will enter our My_Code folder and
# process My_Code/CMakeLists.txt
add_subdirectory(My_Code)
project(My_Code)
# Declaring my source files
set(SRC_FILES
${PROJECT_SOURCE_DIR}/src/main.cpp
)
# Declaring my headers (this is not strictly
# necessary, but this will add them to the file
# structure in visual on windows)
set(HDR_FILES
${PROJECT_SOURCE_DIR}/include/my_header.hpp
)
# Create my target
add_executable(${PROJECT_NAME} ${HDR_FILES} ${SRC_FILES})
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
# Add my include folder to the include path
target_include_directories(${PROJECT_NAME} PUBLIC include)
# Linking my code to botcraft, cmake will handle everything
target_link_libraries(${PROJECT_NAME} botcraft)
# All this stuff is not really necessary, we just set the output folder
# to be the same than for botcraft so we don't need to manually copy the shared
# library next to the executable
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "_d")
set_target_properties(${PROJECT_NAME} PROPERTIES RELWITHDEBINFO_POSTFIX "_rd")
if(MSVC)
# To avoid having one subfolder per configuration when building with Visual
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BOTCRAFT_OUTPUT_DIR}/bin")
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BOTCRAFT_OUTPUT_DIR}/bin")
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${BOTCRAFT_OUTPUT_DIR}/bin")
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${BOTCRAFT_OUTPUT_DIR}/bin")
set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin")
else()
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${BOTCRAFT_OUTPUT_DIR}/bin")
endif(MSVC)
#pragma once
int add(const int a, const int b)
{
return a + b;
}
#include <iostream>
#include <botcraft/Game/ConnectionClient.hpp>
#include "my_header.hpp"
int main(int argc, char* argv[])
{
// Use some of my code
std::cout << "1 + 2 = " << add(1, 2) << std::endl;
// Use some of botcraft code
Botcraft::ConnectionClient client;
// Don't connect or anything because this is a minimal example
return 0;
}
Feel free to ask more details if there is something unclear. |
Beta Was this translation helpful? Give feedback.
Yeah cmake is a very useful tool but it is a bit intimidating when you're not familiar with it.
You'll find below a small project example to start your own project. It's not much, but it should get you started. I'm assuming you're working with the following structure:
Botcraft
folder is the content of this repository whileMy_Code
contains your own code. The rootCMakeLists.txt
will just glue the two together../CMakeLists.txt
cmake_minim…