Saturday, September 19, 2009

Building OpenFrameworks with CMake

Why ?

Openframework programs can be hard to build particulary when you are not using XCode or Code::Blocks.

I personally use linux (ubuntu) and prefer to use my favorite editor (vim) to program.


I decided a long time ago to create a CMake skeleton to be able to build openframeworks easily. I choosed to use CMake because i thought it could be useful - one day - for the ofx community where most of people are using XCode or Code::blocks. CMake can generate C::B and Xcode project files.

Prerequist

You can download OpenFrameworks CMake skelton on my github page.


git clone git://github.com/dopuskh3/ofx-cmake-build.git  
  

Edit CMakeBase.txt

This file provides all necessary checks to build openframeworks source code:

  • finds necessary include files
  • finds libraries (fmodex, poco, unicap...)
Edit this file to reflect your system configuration an openframworks sourcecode. Here is my configuration:
#######################################################################################################
########### Configuration Vars #########################################################################
########################################################################################################

# The path where stants openFrameworks's sources
set ( ofx_sources_directory "/home/fv/Dev/openFrameworks/ofx-dev/libs/openFrameworks" )

# Additional include directories 
set ( custom_include_dirs "/usr/include/libavformat;/usr/include/libavcodec;/usr/include/libswscale")

# Poco include path where to find Poco/Poco.h and libPocoFoundation                                                            
set (poco_includes "/usr/include" )
set (poco_libdir " ")

# GLee include path 
set (glee_includes "/home/fv/Dev/openFrameworks/ofx-dev/libs/GLee/include" )
set (glee_libdir   "/home/fv/Dev/openFrameworks/ofx-dev/libs/GLee/lib" )

# FModex 
set (fmodex_includes "/home/fv/Dev/openFrameworks/ofx-dev/libs/fmodex/inc" )
set (fmodex_libdir   "/home/fv/Dev/openFrameworks/ofx-dev/libs/fmodex/lib/linux/" )

# RtAudio
set (rtaudio_includes "/home/fv/Dev/openFrameworks/ofx-dev/libs/rtAudio/include")
set (rtaudio_libdir   "/home/fv/Dev/openFrameworks/ofx-dev/libs/rtAudio/lib")

# FreeImage 
set ( freeimage_includes "/home/fv/Dev/openFrameworks/ofx-dev/libs/freeimage/include" )
set ( freeimage_libdir   "/home/fv/Dev/openFrameworks/ofx-dev/libs/freeimage/lib" )

# For linux only 
set (unicap_includes "/home/fv/Dev/openFrameworks/ofx-dev/libs/unicap/include")
set (unicap_libdir   "/home/fv/Dev/openFrameworks/ofx-dev/libs/unicap/lib")

set (asound_includes "")
set (asound_libdir "")

set (raw1394_includes "")
set (raw1394_libdir "")

....
  

Creating a project from scratch

Create a subdirectory to store your project:
cd ofx-cmake-build/
mkdir myOfxProject/
mkdir myOfxProject/src
  
Copy the provided CMakeFiles.txt into your project root directory. Edit this file to change the project name and add some dependencies:
cd myOfxProject/
cp /path/to/ofx-cmake-build/sampleProgram/CMakeFiles.txt . 
 
Edit your CMakeFiles.txt to suit your freshly created project.

CMakeFiles.txt:
cmake_minimum_required(VERSION 2.6)                                                                                            

# project name
project(myOfxProject)

# path to CMakeBase.txt file
include ( ../CMakeBase.txt ) 

# add ofx includes directories for dependencies defined in CMakeBase.txt
include_directories ( ${ofx_includes} ) 

file ( GLOB_RECURSE app_sources_files src/*)

add_executable( myOfxProject
    ${app_sources_files}
    ${OFX_SOURCE_FILES} ) # Defined in CMakeBase.txt

set ( libs ${ofx_libs}) # Defined in CMakeBase.txt

target_link_libraries(manoProut ${libs} )
  
You're now ready to build your project.
mkdir build
cd build
cmake ../
make 
  
By default cmake generate unix makefiles. You can use the -G switch to use another generator (Xcode, code::blocks...)

A more complicated example: Using addons

You may want to use available addons to build more complicated projects. I personally use ofxOpenCv for one of my project. ofxOpenCv addon depends on ofxVectorMath addon. You can copy both ofxOpenCv and ofxVectorMath addons sourcecodes in your project root. Your project layout should be like this:
ofxSampleProjectWithAddons
|-- ofxOpenCv
|   `-- src
|       |-- ofxCvBlob.h
|       |-- ofxCvColorImage.cpp
|       |-- ofxCvColorImage.h
|       |-- ofxCvConstants.h
|       |-- ofxCvContourFinder.cpp
|       |-- ofxCvContourFinder.h
|       |-- ofxCvFloatImage.cpp
|       |-- ofxCvFloatImage.h
|       |-- ofxCvGrayscaleImage.cpp
|       |-- ofxCvGrayscaleImage.h
|       |-- ofxCvImage.cpp
|       |-- ofxCvImage.h
|       |-- ofxCvMain.h
|       |-- ofxCvShortImage.cpp
|       |-- ofxCvShortImage.h
|       `-- ofxOpenCv.h
|-- ofxVectorMath
|   `-- src
|       |-- ofxMatrix3x3.cpp
|       |-- ofxMatrix3x3.h
|       |-- ofxPoint2f.cpp
|       |-- ofxPoint2f.h
|       |-- ofxPoint3f.cpp
|       |-- ofxPoint3f.h
|       |-- ofxPoint4f.cpp
|       |-- ofxPoint4f.h
|       |-- ofxVec2f.cpp
|       |-- ofxVec2f.h
|       |-- ofxVec3f.cpp
|       |-- ofxVec3f.h
|       |-- ofxVec4f.cpp
|       |-- ofxVec4f.h
|       `-- ofxVectorMath.h
`-- src
    |-- main.cpp
    |-- testApp.cpp
    `-- testApp.h

  


You can had your includes and library checks into the project.

CMakeFiles.txt:
cmake_minimum_required(VERSION 2.6)                                                                                            

project(ofxSampleProjectWithAddons)

include ( ../CMakeBase.txt ) 

# add ofx includes directories for dependencies 
include_directories ( ${ofx_includes} ) 

file ( GLOB_RECURSE app_sources_files src/*)

# VectorMath addon ##############################
# add VectorMath addon sourcecode
file ( GLOB_RECURSE ofxVectorMath ofxVectorMath/src/* )
# add includes path for this addon
include_directories ( ofxVectorMath/src/ )

# OpenCv addon ##################################
# add ofxOpenCv source code
file ( GLOB_RECURSE ofxOpenCv ofxOpenCv/src/* )
# add include path for this addon
include_directories ( ofxOpenCv/src/ )

# search for opencv library and includes using pkg-config 
include(FindPkgConfig)
pkg_search_module(cv opencv)
include_directories ( ${cv_INCLUDE_DIRS} )
#################################################

# create an executable with all source files
add_executable( ofxSampleProjectWithAddons
    ${app_sources_files}
    ${ofxOpenCv}
    ${ofxVectorMath}
    ${OFX_SOURCE_FILES} )

# link with cv libraries and openFrameworks dependencies
set ( libs ${ofx_libs} ${cv_LIBRARIES})
target_link_libraries(manoProut ${libs} )
  

3 comments:

  1. https://github.com/dopuskh3/ofx-cmake-build/ gives me a 404 currently, has it moved?

    ReplyDelete
    Replies
    1. I think that it was moved to https://github.com/dopuskh3/openFrameworks-dependencies. I am not sure ...

      Delete