Building a project using CMake requires at least one CMakeLists.txt file in the root of your project. If your source files are located in a subdirectory it is common to use a separate CMake build file in the src subdirectory. This is the second file shown in the table below.
If there are additional subdirectories below the main source directory the CMake file is usually named after the folder. This is the third file shown in the table below.
| Directory | File | Purpose |
| myApp | CMakeLists.txt | Modified per project, contains a list of source files and libraries |
| myApp/src | CMakeLists.txt | Modified per project, contains a list of source files and libraries |
| myApp/src/subfolder | subfolder.txt | Modified per project, contains a list of source files and libraries |
This is a fictitious project intended to show the syntax for building a CopperSpice project. For a full working program review the KitchenSink demo build files which is available on github and from our CopperSpice download page.
CMake Build Files
Using our sample HelloLunch project the contents of the root level CMakeLists.txt is shown below.
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(HelloLunch)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckIncludeFile)
include(CheckIncludeFiles)
include(CheckTypeSize)
find_package(CopperSpice REQUIRED)
set(PACKAGE "HelloLunch")
set(PACKAGE_NAME "HelloLunch")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INCLUDE_DIRECTORIES_BEFORE ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_subdirectory(src)
The following CMakeLists.txt is located in the src subdirectory along with your source files.
list(APPEND HelloLunch_INCLUDES
${CMAKE_CURRENT_SOURCE_DIR}/lunch.h
)
list(APPEND HelloLunch_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/lunch.cpp
${CMAKE_CURRENT_SOURCE_DIR}/lunch.ui
${CMAKE_CURRENT_SOURCE_DIR}/lunch_de.ts
${CMAKE_CURRENT_SOURCE_DIR}/lunch_fr.ts
qrc_hellolunch.cpp
)
COPPERSPICE_RESOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/../hellolunch.qrc
)
COPPERSPICE_RESOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/lunch.ui
)
set(TS_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../resources)
COPPERSPICE_RESOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/../resources/lunch_de.ts
${CMAKE_CURRENT_SOURCE_DIR}/../resources/lunch_fr.ts
)
add_executable(HelloLunch
${HelloLunch_SOURCES}
)
target_include_directories(HelloLunch
PRIVATE
${CMAKE_SOURCE_DIR}/src
)
target_link_libraries(HelloLunch
CopperSpice::CsCore
CopperSpice::CsGui
)
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
target_link_libraries(HelloLunch
netapi32
mpr
)
if (MSVC)
target_link_options(HelloLunch
PRIVATE
/subsystem:windows
/entry:mainCRTStartup
)
else()
target_link_libraries(HelloLunch
-mwindows
)
endif()
endif()
install(TARGETS HelloLunch DESTINATION .)
CMake Build Process
To configure, comile, link, and install HelloLunch use the following commands. We suggest saving these to a bash script.
mkdir build
cd build
cmake -G "Ninja" -DCMAKE_PREFIX_PATH=[path_to_copperspice] \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=[deploy_path] \
..
ninja install
| Parameter | Example | Information |
| -DCMAKE_PREFIX_PATH | /c/cs_lib/cmake/copperspice | typically a path to the CopperSpice CMake export files |
| -DCMAKE_BUILD_TYPE | | optional parameter, default is Release |
| -DCMAKE_INSTALL_PREFIX | /c/projects/HelloLunch/deploy | path where HelloLunch will be installed |
| Last Parameter | [dot][dot] | path to HelloLunch project root directory |
Other Build Examples
Refer to the Building KitchenSink documentation which contains the suggested script to build and install the KitchenSink project.