IAM

ARTICLE

Running OpenCV and Google GLog with CMake on Travis CI

Running C++ projects on Travis CI may be challenging depending on the libraries used. Here I briefly describe how to run OpenCV and Google GLog on Travis CI.

After successfully running some of my JavaScript projects on Travis CI, my C++ projects are next. While some libraries like OpenCV can be easily installed through apt-get, others like Google GLog need to be built manually. This article gives a brief example of how to link against OpenCV and Google GLog on Travis CI.

The project can be found on GitHub - in particular, here is the corresponding .travis.yml.

Running OpenCV on Travis

Using OpenCV on Travis, similar to many other libraries, can be accomplished through:

sudo apt-get install libopencv-dev

Other useful libraries:

sudo apt-get install libboost-all-dev
sudo apt-get install libeigen3-dev

Running Google GLog on Travis

Although Google GLog can be installed through sudo apt-get install libgoogle-glog-dev on recent versions of Ubuntu, this is not necessarily possible on Travis. However, Google GLog can be built on-the-fly using:

git clone https://github.com/davidstutz/glog
cd glog
git reset --hard 0b0b022
./configure
make
cd ..

This will install glog into the directory glog within the project's root directory. The hard reset is responsible for checking out version 0.3.3 of Google GLog. This is motivated by some issues with the latest version (see here).

When using CMake, the local Google GLog installation can easily be found using the following CMake script:

# - Try to find Glog
#
# The following variables are optionally searched for defaults
#  GLOG_ROOT_DIR:            Base directory where all GLOG components are found
#
# The following are set after configuration is done: 
#  GLOG_FOUND
#  GLOG_INCLUDE_DIRS
#  GLOG_LIBRARIES

include(FindPackageHandleStandardArgs)

set(GLOG_ROOT_DIR "" CACHE PATH "Folder contains Google glog")

find_path(GLOG_INCLUDE_DIR glog/logging.h
    PATHS
        ${GLOG_ROOT_DIR}
    PATH_SUFFIXES
        src)

find_library(GLOG_LIBRARY glog libglog
    PATHS
        ${GLOG_ROOT_DIR}
    PATH_SUFFIXES
        .libs
        lib
        lib64)

find_package_handle_standard_args(GLOG DEFAULT_MSG
    GLOG_INCLUDE_DIR GLOG_LIBRARY)

if(GLOG_FOUND)
    set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
    set(GLOG_LIBRARIES ${GLOG_LIBRARY})
endif()

When the script is saved to cmake/FindGLog.cmake, the CMakeLists.txt in the project's root needs to contain:

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

Google GLog can then be used using the variables GLOG_INCLUDE_DIRS and GLOG_LIBRARIES.

C++11 and C++14 on Travis

For using C++11 or C++14 features on Travis, for example using -std=c++11 for gcc, the Ubuntu 14.04 (Trusty) beta can be used:

sudo: required
dist: trusty

See here for details.

Putting it All Together

The full .travis.yml:

language: cpp

# For Ubuntu 14.04 C++11/15 support ...
sudo: required
dist: trusty

compiler:  
    - gcc

before_script:
    - sudo apt-get install build-essential
    - sudo apt-get install cmake
    - sudo apt-get install libboost-all-dev
    - sudo apt-get install libopencv-dev
    - sudo apt-get install libeigen3-dev
    # Glog through apt-get does not work ...
    - git clone https://github.com/davidstutz/glog
    - cd glog
    - git reset --hard 0b0b022
    - ./configure
    - make
    - cd ..
    - mkdir build
    - cd build
    - cmake .. -DGLOG_ROOT_DIR=../glog

script: make 
What is your opinion on this article? Let me know your thoughts on Twitter @davidstutz92 or LinkedIn in/davidstutz92.