In this article we will explain how to create the build file for Example 1.
The CMake program uses the following project build file and gathers information about the system you are building on to create Ninja files. These are the actual files which are used to compile and link your application. The Ninja program will read the files CMake generated and invoke your compiler and linker to produce a binary executable of your project.
This is the CMake file which is used to build Example 1 and must be named CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(example_1)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckIncludeFile)
include(CheckIncludeFiles)
find_package(CopperSpice REQUIRED)
# file locations for installing
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
include(GNUInstallDirs)
# where libraries are located relative to the executable
set(CMAKE_INSTALL_RPATH "@executable_path/../Resources")
elseif(CMAKE_SYSTEM_NAME MATCHES "(Linux|OpenBSD|FreeBSD)")
include(GNUInstallDirs)
set(CMAKE_INSTALL_RPATH "\$ORIGIN")
elseif(MSVC)
# use defaults
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION .)
include(${CopperSpice_DIR}/InstallMinGW.cmake)
endif()
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INCLUDE_DIRECTORIES_BEFORE ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# location for building binary files
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
list(APPEND PROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
target_link_libraries(${PROJECT_NAME}
PRIVATE
netapi32
mpr
-mwindows
)
endif()
target_link_libraries(${PROJECT_NAME}
PRIVATE
CopperSpice::CsCore
CopperSpice::CsGui
)
install(TARGETS ${PROJECT_NAME} DESTINATION .)
cs_copy_library(CsCore)
cs_copy_library(CsGui)
# installs the platform Gui plugin
cs_copy_plugins(CsGui)
Line 2 sets the name of the project which will also be used for the executable file name. So this line must be adjusted for every application.
On line 9 we are calling find_package(). This will try to locate the CMake files which were exported when CopperSpice was built.
Line 34 can be changed if you want to build your application with a newer version, like C++20. Please be aware that CopperSpice and all applications which use our libraries must be built with at least C++17.
The four highlighted lines towards the bottom of this CMake file will need to change if other CopperSpice libraries ( like CsNetwork ) are required to build your program.