Difference between revisions of "CMake compilation"

From Bio++ Wiki
Jump to: navigation, search
Line 11: Line 11:
|-- build/
|-- build/
|-- include/
|-- include/
|  `-- objects.h
|  `-- classes.h
`-- src/
`-- src/
     |-- dark_star.cpp
     |-- dark_star.cpp
     `-- objects.cpp
     `-- classes.cpp

Revision as of 09:59, 2 July 2012

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 :

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

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

cmake_minimum_required (VERSION 2.6)


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

#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a ${CMAKE_FIND_LIBRARY_SUFFIXES}") # uncomment for static compilation

# --- 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})

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

#--- Libraries & includes ---

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})

Remark: For static compilation, uncomment line 7.


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>

Remark: It is also possible to set the CMAKE_LIBRARY_PATH and CMAKE_INCLUDE_PATH environment variables to avoid repeating the library paths. (In turn, those variables can be superseded by values passed through the -D option.)