Difference between revisions of "CMake compilation"

From Bio++ Wiki
Jump to: navigation, search
m (Formating)
Line 77: Line 77:
 
</source>
 
</source>
  
Remark: It is also possible to set the <tt>CMAKE_LIBRARY_PATH</tt> and <tt>CMAKE_INCLUDE_PATH</tt> environment variables to avoid repeating the library paths. (In turn, those variables can be superseded by values passed through the <tt>-D</tt> option.)
+
===== Notes =====
 +
* It is possible to set the <tt>CMAKE_LIBRARY_PATH</tt> and <tt>CMAKE_INCLUDE_PATH</tt> environment variables to avoid repeating the library paths at the command line. (Those variables, if they are set, are superseded by values passed at the command line.)

Revision as of 11:54, 4 August 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 :

dark_star/
|-- 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)

project("dark_star")

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

  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/")

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.

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, if they are set, are superseded by values passed at the command line.)