Modern C + + CI

Source: Internet
Author: User
Tags glob

Original

https://juan-medina.com/2017/07/01/moderncppci/


C + + active than ever, with the C++17 standard ready, a widely support on c++14 from major compilers and c++20 Ning on the way there are a interesting future for the standard.

Modern C + + is great, some people are even calling it a new language, but isn't only the language what is evolving the too L-chain is getter better, so doing continuous integration for cross platform projects are simple and effective.

I decide to does a simple project using some of the c++14 features and following the C + + Core guidelines whenever its POSSIB Le. The result is available in this repository.

I set some goals to doing the Project:project is organized with a logical structure Need to be a small c++14 project, BU T nothing really complicate. Would have at least two modules, a library and a program that uses it. Modern unit Tests. It should use some third party software. CI would be triggered each commit and build using; GCC & clang on Linux XCode on OS X Visual Studio on Windows

The initial project structure

  /lib
    /src
    /include
    /test
  /app
  /src/test/third_party

Nothing complicated, and easy to manage, with a clear meaning of each folder.

The Simple program

#include "calc.h"
#include "logger.h"

int main (int argc, char *argv[]) {
  using namespace Moderncppci;
  Logger::level (loglevel::info);

  Logger log{__func__};

  Log.info ("Doing some calculation");
  Log.info (calc{} << 1 << "+" << 2 << "*" << 5 << "-" << 3 << "/" << 4 );

  return 0;
}

This program would output when run:

[2017-07-01 11:09:22.766] [Console] [INFO] [main] Doing some calculation
[2017-07-01 11:09:22.768] [console] [INFO] [main] 1 + 2 * 5-3/4 = 3

This example use a couple of the classes defined in the library, Calc and Logger to display a simple calculation.

Doing Unit Tests

I decide to use the "wonderful Catch for doing" unit test, as a example:

Test_case ("Chain operations'll Work", "[Calc]") {
  Auto calc = calc{} << 1 << "+" << 2 << "*" << 5 << "-" << 3 << "/" << 4;

  REQUIRE (Calc.result () = 3);
}

Test_case ("We could stream results", "[Calc]") {
  std::ostringstream string_stream{};

  String_stream << (calc{} << 1 << "+" << 2);

  REQUIRE (string_stream.str () = = "1 + 2 = 3");

We are not going to the detail in the implementation of the "classes" or "test," the Repositoryhas all the details about It, lest focus now on the CI.

Build and test in the all platform

We are going to use CMake and ctest for creating our builds so we start with the main CMakeLists.txt on the root of the pro Ject.

# CMake Build:global Project

cmake_minimum_required (VERSION 3.3)

project (MODERNCPPCI)

Set_property ( GLOBAL Property use_folders in) set (

Cmake_cxx_standard)
set (cmake_cxx_standard_required on) set

( Threads_prefer_pthread_flag on)
find_package (THREADS REQUIRED)

add_subdirectory (Third_party exclude_from_ All)
Add_subdirectory (Lib)
Add_subdirectory (APP)

enable_testing ()

We just set some settings, as we are going to we require c++14, we'll find the Thread library for any tool-ch Ain, we include our directories, but we exlude for the third party builds targets that our dependencies could, And finally we set this CMake project would have tests.

Preparing the third party software

The third part software so we are going to use Is:catch for unit Test. Spdlog for a very fast C + + logging library.

The third party software are cloned as submodules, so we use their original project structure.

Now we prepare a new CMakeLists.txt under The/third_party folder:

 # CMake Build:third Party #configure directories set (Third_party_module_path "${project_source_dir}/third_party" # Catch #configure directories set (Catch_module_path "${third_party_module_path}/catch") Set (Catch_include_path "${C") Atch_module_path}/include ") #include custom cmake function include (" ${catch_module_path}/contrib/ Parseandaddcatchtests.cmake ") # Spdlog #configure directories Set (Spdlog_module_path" ${third_party_module_path}/ Spdlog ") Set (Spdlog_include_path" ${spdlog_module_path}/include ") #set variables set (Third_party_include_path ${ Spdlog_include_path}) #set variables for tests set (Test_third_party_include_path ${catch_include_path}) #export VARs s ET (Third_party_include_path ${third_party_include_path} parent_scope) set (Test_third_party_include_path ${TEST_ Third_party_include_path} parent_scope) 

The most import part of this is so we export two variables that would have the corresponding directories to add to the INCLU De paths for our targets and we push them to the "parent scope so we could use them." Additionally for Catch we include a custom function that would allow to auto discover tests.

Bulding the Library

For creating our library we add a new CMakeLists.txt under The/lib folder:

The 
 # CMake build:library #configure variables Set (lib_name "${project_name}lib") #configure directories set (Librar Y_module_path "${project_source_dir}/lib") Set (Library_src_path "${LIBRARY_MODULE_PATH}/SRC") Set (LIBRARY_INCLUDE_ PATH "${library_module_path}/include") #set includes include_directories (${library_include_path} ${third_party_ Include_path}) #set sources file (GLOB lib_header_files "${library_include_path}/*.h") file (GLOB lib_source_files "${li") Brary_src_path}/*.cpp ") #set library Add_library (${lib_name} STATIC ${lib_source_files} ${lib_header_files}) #export V Ars Set (Library_include_path ${library_include_path} parent_scope) set (Lib_name ${lib_name} parent_scope) #test enable _testing () add_subdirectory (test) 

Here we set the desired include directories and we built a list of sources and headers files, them we create the library an D Export a couple of variable so we could use them in our application, finally we add the "Test directory so" we build the T EST for this library.

Testing the Library

No we create a new CMakeLists.txt under The/lib/test directory:

# CMake Build:library tests

#configure variables
set (test_app_name "${lib_name}test")

#configure Directories
Set (Test_module_path "${library_module_path}/test")

#configure TEST directories
set (test_ Src_path  "${test_module_path}/src")

#set includes
include_directories (${library_include_path} ${test _third_party_include_path})

#set test sources
file (GLOB test_source_files "${test_src_path}/*.cpp")

#set Target Executable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.