Ricard Molins
Ricard Molins

Reputation: 450

Best practice to include C++ libraries on my project

I am facing some problems related to how to handle C++ projects. I have a set of repositories all of the tracked with git (hosted on gitlab).

Which is the best practice to include repo_B in repo_A?.

  1. Installing repo_B at system level? And then use repo_B from repo_A by using the CMakelists
  2. Adding repo_B as submodule on repo_A
  3. Adding it in the CMakeLists by using something similar to what is explained in the googletest repository

    cmake_minimum_required(VERSION 2.8.2)
    
    project(googletest-download NONE)
    
    include(ExternalProject)
    ExternalProject_Add(googletest
    GIT_REPOSITORY    https://github.com/google/googletest.git
    GIT_TAG           master
    SOURCE_DIR        "${CMAKE_BINARY_DIR}/googletest-src"
    BINARY_DIR        "${CMAKE_BINARY_DIR}/googletest-build"
    CONFIGURE_COMMAND ""
    BUILD_COMMAND     ""
    INSTALL_COMMAND   ""
    TEST_COMMAND      ""
    )
    

We are were originillay intalling libraries at user level. However, after starting to use Jenkins we are facing some problems.

Upvotes: 1

Views: 1715

Answers (1)

Gergely Nyiri
Gergely Nyiri

Reputation: 345

The best option would be a kind of mixed version. I usually track the external libraries as git submodules (in PROJECT_ROOT/submodules) and create a generic CMake file for all of them like this:

find_package(GTest)

if(GTEST_FOUND)
  message(STATUS "googletest was found!")
else()
  message(STATUS "googletest was not found, local build is needed!")

  include(ExternalProject)

  ExternalProject_Add(googletest-project
    URL ${CMAKE_SOURCE_DIR}/submodules/googletest/
    PREFIX "${CMAKE_CURRENT_BINARY_DIR}/ext/googletest"
    CMAKE_ARGS -DCMAKE_BUILD_TYPE:STRING=Release -DBUILD_GTEST=ON 
      DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
    LOG_DOWNLOAD ON
    LOG_CONFIGURE ON
    LOG_BUILD ON
    LOG_INSTALL ON
  )

  ExternalProject_Get_Property(googletest-project install_dir)
  ExternalProject_Get_Property(googletest-project source_dir)

  file(MAKE_DIRECTORY ${install_dir}/include)

  add_library(gtest STATIC IMPORTED)
  add_library(gtest_main STATIC IMPORTED)

  set_property(TARGET gtest PROPERTY IMPORTED_LOCATION 
    ${install_dir}/lib/libgtest.a)
  set_property(TARGET gtest_main PROPERTY IMPORTED_LOCATION 
    ${install_dir}/lib/libgtest_main.a)

  set_property(TARGET gtest APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES 
    ${install_dir}/include)
  set_property(TARGET gtest_main APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES 
    ${install_dir}/include)

  add_dependencies(gtest googletest-project)
  add_dependencies(gtest_main googletest-project)

  set(GTEST_LIBRARIES gtest)
  set(GTEST_MAIN_LIBRARIES gtest_main)

  set(GTEST_INCLUDE_DIRS ${install_dir}/include)
endif()

This way googletest will not be built when it is already installed on your system (e.g. in a Docker image with all the external dependencies pre-installed). As you can see, I build googletest from the submodule directory and will not clone from github.

Upvotes: 1

Related Questions