CopperSpice Overview  1.5.1
CopperSpice Project Using CMake

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 CMakeLists.txt file in the subdirectory to list your source files. 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 and the extension is .cmake. 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 to include
myApp/src CMakeLists.txt Modified per project, contains a list of source files and libraries to include
myApp/src/subfolder subfolder.cmake Modified per project, contains a list of source files and libraries to include


Using our sample HelloLunch project the contents of the root level CMakeLists.txt is shown below.

1 cmake_minimum_required(VERSION 3.4.3 FATAL_ERROR)
2 project(HelloLunch)
3 
4 include(CheckCXXCompilerFlag)
5 include(CheckCXXSourceCompiles)
6 include(CheckIncludeFile)
7 include(CheckIncludeFiles)
8 include(CheckTypeSize)
9 
10 find_package(CopperSpice REQUIRED)
11 
12 set(PACKAGE "HelloLunch")
13 set(PACKAGE_NAME "HelloLunch")
14 
15 set(CMAKE_INCLUDE_CURRENT_DIR ON)
16 set(CMAKE_INCLUDE_DIRECTORIES_BEFORE ON)
17 set(CMAKE_CXX_STANDARD_REQUIRED ON)
18 set(CMAKE_CXX_STANDARD 14)
19 
20 # location for building binary files
21 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
22 
23 add_subdirectory(src)


The following CMakeLists.txt is located in the src subdirectory along with the source project files.

1 set(CS_VERSION 1.4)
2 
3 set(HelloLunch_INCLUDES
4  ${CMAKE_CURRENT_SOURCE_DIR}/lunch.h
5 )
6 
7 set(HelloLunch_SOURCES
8  ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
9  ${CMAKE_CURRENT_SOURCE_DIR}/lunch.cpp
10  ${CMAKE_CURRENT_SOURCE_DIR}/lunch.ui
11  qrc_hellolunch.cpp
12 )
13 
14 # run rcc to generate qrc output
15 COPPERSPICE_RESOURCES(
16  ${CMAKE_CURRENT_SOURCE_DIR}/../hellolunch.qrc
17 )
18 
19 # run uic to generate source
20 COPPERSPICE_RESOURCES(
21  ${CMAKE_CURRENT_SOURCE_DIR}/lunch.ui
22 )
23 
24 add_executable(HelloLunch
25  ${HelloLunch_SOURCES}
26 )
27 
28 target_include_directories(HelloLunch
29  PRIVATE ${CMAKE_SOURCE_DIR}/src
30 )
31 
32 target_link_libraries(HelloLunch
33  CopperSpice::CsCore${CS_VERSION}
34  CopperSpice::CsGui${CS_VERSION}
35 )
36 
37 if(WIN32)
38  # disable console on windows
39  target_link_libraries(HelloLunch -lnetapi32 -lmpr -mwindows)
40 endif()
41 
42 install(TARGETS HelloLunch DESTINATION .)

Using CMake to Build

To configure and build HelloLunch use the following two commands.

cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=[path_to_copperspice] .
ninja