Search code examples
macosopencvcmakeopenninite

How to use CMake to include a library, then include its headers using angled brackets in source file?


I used homebrew to install opencv and openni and nite.

and I've managed to make a cmake file for opencv, and somehow it already had a defined path for its directory OpenCV_LIBS, not sure how or where I find out about that.

cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )

target_link_libraries( main ${OpenCV_LIBS} )

After installing toktakke's build openni and nite. I gave this a shot, and extended to the following, and again I'm not sure where I'd find these aliases to their directories.

cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
include_directories(${OPENNI_INCLUDE})
Include_directories(${NITE_INCLUDE})
add_executable( main main.cpp)
target_link_libraries( main ${OpenCV_LIBS} ${OPENNI_LIB} ${NITE_LIB} )

but it wasn't integrated well

in .bash_profile I have the following:

export OPENNI_INCLUDE="/usr/local/Cellar/openni/1.5.7.10/include/ni"
export OPENNI_LIB="/usr/local/Cellar/openni/1.5.7.10/lib"
export OPENNI_DIR="/usr/local/Cellar/openni/1.5.7.10"
export NITE_INCLUDE="/usr/local/Cellar/nite/1.5.2.21/include/nite"
export NITE_LIB="/usr/local/Cellar/nite/1.5.2.21/lib"
export NITE_DIR="/usr/local/Cellar/nite/1.5.2.21"

now I get the following error:

fatal error: 'XnOpenNI.h' file not found
#include <XnOpenNI.h>

Solution

  • You should add the

    include_directories($ENV{OPENNI_INCLUDE})
    

    after your project definition. This will add the path to the OPENNI include files as an -I option to the compiler. More about the 'include_directories' command is here.

    Regarding the origin of OpenCV_LIBS:

    When the 'find_package( OpenCV REQUIRED )' is used, cmake will automatically call the FindOpenCV.cmake (found in the path specified by CMAKE_MODULE_PATH variable). The FindOpenCV.cmake is responsible for actually locate the OpenCV libraries and include files and define both the OpenCV_LIBS and bunch of other variables.

    This makes it "magically" available for you. More info about find_package magic could be found in the CMake docs