CMake compilation

From Bio++ Wiki
Jump to: navigation, search


This page explains how to compile programs that depend on Bio++ using CMake.

Project organisation and CMakeLists.txt file

Here we assume that your project is organized as follows, with the main() function in dark_star.cpp :

dark_star/
|-- CMakeLists.txt
|-- build/
|-- include/
|   `-- my_class.h
`-- src/
    |-- dark_star.cpp
    `-- my_class.cpp

For a project that uses bpp-phyl, the CMakeLists.txt file should look like this :

cmake_minimum_required (VERSION 2.6)

project("dark_star")

set(CMAKE_CXX_FLAGS "-Wall -Wshadow -Weffc++")

# --- the IMPROVED_FIND_LIBRARY macro (taken from the Bio++ CMakeLists.txt) ---
macro(IMPROVED_FIND_LIBRARY OUTPUT_LIBS lib_name include_to_find)
  FIND_PATH(${lib_name}_INCLUDE_DIR ${include_to_find})
  SET(${lib_name}_NAMES ${lib_name} ${lib_name}lib ${lib_name}dll)
  FIND_LIBRARY(${lib_name}_LIBRARY NAMES ${${lib_name}_NAMES})

  IF(${lib_name}_LIBRARY)
    MESSAGE("-- Library ${lib_name} found here:")
    MESSAGE("    ${${lib_name}_INCLUDE_DIR}/")
    MESSAGE("    ${${lib_name}_LIBRARY}")
  ELSE(${lib_name}_LIBRARY)
    MESSAGE(FATAL_ERROR "${lib_name} required but not found.")
  ENDIF(${lib_name}_LIBRARY)
  
  #add the dependency:
  INCLUDE_DIRECTORIES(${${lib_name}_INCLUDE_DIR})
  SET(${OUTPUT_LIBS} ${${OUTPUT_LIBS}} ${${lib_name}_LIBRARY})
ENDMACRO(IMPROVED_FIND_LIBRARY)

#--- Libraries & includes ---
include_directories("include/")

#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a ${CMAKE_FIND_LIBRARY_SUFFIXES}") # uncomment for static compilation
IMPROVED_FIND_LIBRARY(LIBS_BPP bpp-phyl "Bpp/Phyl/Tree.h")
IMPROVED_FIND_LIBRARY(LIBS_BPP bpp-seq "Bpp/Seq/Sequence.h")
IMPROVED_FIND_LIBRARY(LIBS_BPP bpp-core "Bpp/Clonable.h")

#--- Targets ---

set(exe ${PROJECT_NAME})

file(GLOB sources src/*.cpp)
file(GLOB includes include/*.h)
add_executable(${exe} ${sources} ${includes})

target_link_libraries(${exe} ${LIBS_BPP})

Compilation

Assuming you want to link Bio++ libraries located in $HOME/local/bpp/dev, the compilation should be something like : <source lang="bash"> cd build bpp_dir=$HOME/local/bpp/dev cmake -D CMAKE_LIBRARY_PATH=$bpp_dir/lib -D CMAKE_INCLUDE_PATH=$bpp_dir/include ../ make </source>

Notes
  • It is possible to set the CMAKE_LIBRARY_PATH and CMAKE_INCLUDE_PATH environment variables to avoid repeating the library paths at the command line. (Those variables are superseded by values passed at the command line, if provided.)
  • For static compilation, uncomment line 27 in CMakeLists.txt or add -DBUILD_STATIC=true to the cmake command.