65.9K
CodeProject 正在变化。 阅读更多。
Home

C++ 持续集成设置

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.77/5 (4投票s)

2016年3月14日

CPOL

1分钟阅读

viewsIcon

21704

使用 Travis CI、AppVeyor、CMake 和 Boost 测试库进行 C++ 持续集成设置

引言

根据 ThoughtWorks 的定义,持续集成 (CI) 是一种开发实践,要求开发者每天将代码集成到共享仓库多次。每次检入都会通过自动化构建进行验证,从而使团队能够尽早发现问题。通过定期集成,您可以预先检测到错误,并更轻松地定位它们。

开源社区中常用的 CI 服务有 Travis CIAppVeyor。Travis CI 支持多种语言,并在 Linux 和 OS X 上进行构建。AppVeyor 另一方面,是一个 Windows 构建系统。许多人同时使用它们来支持跨平台开发。 在本文中,除了 CI 服务之外,还使用 CMake 来管理构建过程,并通过 Boost 测试库 进行单元测试。

示例

示例代码安排在一个常见的文件夹结构中。前三个代码块包含 C++11 源代码、头文件和 boost 单元测试。后三个代码块是 CMake、Travis CI 和 AppVeyor 构建脚本。 如果您将代码放在 GitHub 上,并将项目添加到 Travis CI 和 AppVeyor,那么应该会发生成功的构建。 此外,Travis CI 将运行两个构建(gcc 和 clang),AppVeyor 将运行一个(VS2015)。

src/foo.cpp

int foo()
{
  auto x = 1; // C++11 feature
  return x;
}

include/foo.hpp

#ifndef FOO_HPP
#define FOO_HPP

int foo();

#endif

test/test.cpp

#define BOOST_TEST_MODULE test
#include <boost/test/unit_test.hpp>
#include "foo.hpp"

BOOST_AUTO_TEST_CASE(testFoo)
{
    BOOST_CHECK(foo() == 1);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(myProject)

# Add source code and setup folder structure.
set(src src/foo.cpp)
set(test test/test.cpp)
set(include include/foo.hpp)
include_directories(include)

# Add Boost unit test library.
set(Boost_USE_STATIC_LIBS on)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

# Add C++11 compiler flags.
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    add_definitions(-std=c++11 -stdlib=libc++ -O3 -Wall)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    add_definitions(-std=c++11 -O3 -Wall)
endif()

# Create executable that links the source code, unit test, header file, and Boost.
add_executable(exe ${src} ${test} ${include})
target_link_libraries(exe ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})

# Add support for unit tests e.g. ctest.
enable_testing()
add_test(myTest exe)

.travis.yml

matrix:
  include:

    # Build with Linux and gcc.
    # The default gcc compiler does not support C++11 so install a higher version.
    - os: linux
      env: CC=gcc-5 CXX=g++-5
      addons: &gcc5
        apt:
          packages:
            - libstdc++-5-dev
          sources:
            - ubuntu-toolchain-r-test
      install:
        - sudo apt-get update -qq
        - sudo apt-get install -qq g++-5
        - sudo apt-get install -y libboost-test-dev

    # Build with OS X and clang.
    # The default clang compiler supports C++11.
    - os: osx
      env: COMPILER=clang++

script:
  - mkdir build
  - cd build
  - cmake ..
  - make
  - ctest

appveyor.yml

os: Visual Studio 2015

# Boost is already installed on AppVeyor.
environment:
  BOOST_ROOT: C:\Libraries\boost_1_59_0
  BOOST_LIBRARYDIR: C:\Libraries\boost_1_59_0\lib64-msvc-14.0

build_script:
  - md build
  - cd build
  - cmake -G "Visual Studio 14 2015 Win64" ..
  - cmake --build . --config Release
  - ctest

讨论

为了加快构建过程,仅使用 boost 库的一个子集,即 unit_test_framework。 但是,这段代码可以修改以支持 boost 库的其他部分。

有时,您会看到对 boost 库的动态链接。 但是,在 AppVeyor 上有必要静态链接到库。 这是通过不在 test/test.cpp 中包含来实现的。

#define BOOST_TEST_DYN_LINK

并在 CMakeLists.txt 中包含

set(Boost_USE_STATIC_LIBS on)
© . All rights reserved.