In this section we will cover the compilation part of openZia. We will see how to compile it independently, how to add it to your actual CMake project and how to automate module compilation.
You may first want to assert that openZia compiles well in your system. The first dependency is to check your C++ compiler, it must be able to support all C++17 features. You must also have CMake installed with a lower build system like Makefiles or Visual studio solutions. If you wish to start unit tests, you must install Criterion unit-testing library. For the coverage, you will need gcovr.
First, you need to clone the repository and create a build directory:
git clone git https://github.com/MatthieuMv/openZia.git
cd openZia && mkdir Build && cd BuildThen, for a simple library build use the following commands
cmake ..
cmake --build .If you want to run test or see coverage please watch below.
First you need add openZia's repository to your submodules.
git submodule add https://github.com/MatthieuMv/openZia.gitNext you need to automate compilation of openZia in your CMakeLists.txt, and link your program to it.
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(Server)
# Include directly the openZia.cmake to your CMake build
# This include gives 'openZiaLibs' and 'openZiaIncludes' variables.
include(openZia/openZia/openZia.cmake)
# Create the executable and link openZia library to it
add_executable(Server main.cpp)
target_link_libraries(Server ${openZiaLibs})
target_include_directories(Server PRIVATE ${openZiaIncludes}))A good practice is to create a directory for each module, and include their .cmake file individually. I assume you already included openZia.cmake in your CMakeLists.txt and that you have created a directory named Template which contains the following Template.cmake, Template.hpp and Template.cpp.
# Template.cmake
project(Template)
# Save this directory as TemplateSourcesDir
get_filename_component(TemplateSourcesDir ${CMAKE_CURRENT_LIST_FILE} PATH)
set(TemplateSources
${TemplateSourcesDir}/Template.hpp
${TemplateSourcesDir}/Template.cpp
)
# Compile the module, and link it to openZia
add_library(${PROJECT_NAME} SHARED ${TemplateSources})
target_link_libraries(${PROJECT_NAME} ${openZiaLibs})
target_include_directories(${PROJECT_NAME} PRIVATE ${openZiaIncludes})
# Copy the library into the binary modules directory
set_target_properties(${PROJECT_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Modules}
)Even if the API uses windows only for dynamic load, you may need the following configuration :
// Windows socket version
#include <winsock2.h> // or <winsock.h>
// Include windows header after selecting a winsock version !
#include <Windows.h>In that case, you can define a macro named OPENZIA_PRE_WINDOWS_INCLUDE to your build system to import a custom header.
For example you can add the following line into your cmake module.
# Please note that quotes matters /!\
target_compile_definitions(${PROJECT_NAME} PUBLIC OPENZIA_PRE_WINDOWS_INCLUDE="winsock2.h")If you want to start unit tests replace independent build instruction by:
cmake .. -DOPENZIA_TESTS=TRUE
cmake --build .
./openZiaTestsIf you want code coverage then:
cmake .. -DOPENZIA_TESTS=TRUE -DOPENZIA_COVERAGE=TRUE
cmake --build .
./openZiaTests
cd ..
gcovr --exclude Tests