Cmake step by step explanation

In order to build the sample application, we need to set up the cmake building environment. This requires adding the following code to the CMakeLists.txt, that was created earlier.

CMakeLists.txt

Add this to the CMakeLists.txt in the project directory.

set (CMAKE_CXX_STANDARD 14)

# This allows you to use CLAID_Include,
# that can be used to include CLAID packages
find_package(CLAID REQUIRED)
CLAID_Include(ModuleAPI)

# Scanning for all .cpp files in the src directory
file(GLOB_RECURSE PROJECT_SOURCES
    "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
    )

# Add "include" as include directory
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)

## The executable will be compiled from the project source files and
## the source files from the included CLAID packages.
add_executable(MyApplication
    ${PROJECT_SOURCES}
    ${CLAID_SOURCES})

target_link_libraries(MyApplication ${CLAID_LINK_LIBRARIES})

Step-by-step explanation of code

The following provides a line by line explanation of the content of the CMakeLists.txt

    set (CMAKE_CXX_STANDARD 14)
This sets the C++ compiler standard to use C++14 for compilation. I.e., g++ will be invoked with "g++ -std=c++14" (why is this required?)

    find_package(CLAID REQUIRED)
This will include the CLAID cmake utilities. They can be found in the CMakeUtils directory of the CLAID installation directory. The utilities provide easy functions include CLAID packages, such as the ModuleAPI or MLAlgorithms.

    CLAID_Include(ModuleAPI)
CLAID_Include is a function provided by the CLAID CMakeUtils. It allows to easily include other packages. By using this function, the include directory (containing header files) of the target will be included and thus be made available for the current CMake project. Furthermore, the files contained in the src directory (cpp files) will be added to a separate CMake variable (CLAID_SOURCES), that can be used to integrate them into an application (see add_executable instruction below).

    file(GLOB_RECURSE PROJECT_SOURCES
    "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
    )
This will recursively scan the directory "src" of the project directory for files with suffix "*.cpp". All files that were found are added to the variable ${PROJECT_SOURCES}.

    include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
This adds the directory "include" which was created earlier as include directory. This allows source files (.cpp files) to easily include header files placed in that directory.

    add_executable(MyApplication
        ${PROJECT_SOURCES}
        ${CLAID_SOURCES})
This will add a new compilation target called "MyApplication". It will be compiled from all .cpp files found in the src directory (stored in variable ${PROJECT_SOURCES}) as well as all source files from every included CLAID package (stored in variable ${CLAID_SOURCES}).

    target_link_libraries(MyApplication ${CLAID_LINK_LIBRARIES})
This will link platform-specific libraries into the created application (e.g., pthread on Linux and Mac, wsock32 and ws2_32 on Windows). You can expect linker errors, if you remove this line. Note, that CLAID itself is not compiled into a library by default, as all CLAID sources will be directly compiled into the project to improve compatibility.