Difference between revisions of "CMake and FIND PACKAGE"

From Wiki for iCub and Friends
Jump to: navigation, search
Line 14: Line 14:
 
<name>_LINK_DIRECTORIES
 
<name>_LINK_DIRECTORIES
  
For example, suppose you want to include the ACE package in your project:
+
For example, suppose you want to include the ACE package in a new project:
  
 
#find ACE
 
#find ACE
FIND_PACKAGE(ACE)
+
FIND_PACKAGE(OpenCV)
  
 
# searches for all .cpp and .h files in the current directory and add them  
 
# searches for all .cpp and .h files in the current directory and add them  
Line 26: Line 26:
 
SOURCE_GROUP("Header Files" FILES ${folder_header})
 
SOURCE_GROUP("Header Files" FILES ${folder_header})
  
 +
# create the project
 
ADD_EXECUTABLE(your_project ${folder_source} ${folder_header})
 
ADD_EXECUTABLE(your_project ${folder_source} ${folder_header})
  
INCLUDE_DIRECTORIES(${ACE_INCLUDE_DIR})
+
# add include directories
TARGET_LINK_LIBRARIES(your_project ${ACE_DEBUG_LIBRARY})
+
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})
 +
INCLUDE_DIRECTORIES(${OPENCV_INCLUDE_DIR})
 +
 
 +
# link opencv libraries
 +
TARGET_LINK_LIBRARIES(your_project ${OPENCV_LIBRARIES})

Revision as of 02:14, 5 February 2007

WARNING: this file is work in progress, by nat

One of the most important features of CMake is that it allows to configure your project to include/link additional packages (libraries). To do this you have to tell CMake you want to use an additional package, you do this with the FIND_PACKAGE macro. I found FIND_PACKAGE a bit confusing so I try to explain here how it works and how we have been using it on YARP.

Note: I here explain the functionalities of FIND_PACKAGE that are used in YARP, go to http://www.cmake.org/HTML/Documentation.html for a more complete explanation.

FIND_PACKAGE(<name>) ask CMake to find and load settings from the external projects/packages called <name>. This works in a few steps:

- CMake searches the directories listed in CMAKE_MODULE_PATH for a file called Find<name>.cmake. If found, this macro is executed and it is responsible for finding the package (examples of similar macro are: FindACE.cmake, FindBoost.cmake, FindOpenCV.cmake that are located in your $YARP_ROOT/conf directories). "Find" macros define variables which allow you to check if the package has been found and use it in your project. Unfortunately different macros behave differently so you have to check the code to find out the exact names of those variables. A good idea would be to define:

<name>_FOUND <name>_INCLUDE_DIR <name>_LIBRARIES <name>_LINK_DIRECTORIES

For example, suppose you want to include the ACE package in a new project:

  1. find ACE

FIND_PACKAGE(OpenCV)

  1. searches for all .cpp and .h files in the current directory and add them
  2. to the current project

FILE(GLOB folder_source *.cpp) FILE(GLOB folder_header *.h) SOURCE_GROUP("Source Files" FILES ${folder_source}) SOURCE_GROUP("Header Files" FILES ${folder_header})

  1. create the project

ADD_EXECUTABLE(your_project ${folder_source} ${folder_header})

  1. add include directories

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}) INCLUDE_DIRECTORIES(${OPENCV_INCLUDE_DIR})

  1. link opencv libraries

TARGET_LINK_LIBRARIES(your_project ${OPENCV_LIBRARIES})