Simple template for libraries in contrib

From Wiki for iCub and Friends
Revision as of 19:58, 19 November 2013 by (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

These instructions only apply to YARP > 2.3.23 and iCub > 1.1.13 software versions

See Simple template for libraries in contrib old for older instructions

CMake code for a library in contrib is quite similar to that required for modules.

cmake_minimum_required(VERSION 2.8.7)

#find packages, examples YARP and/or ICUB
find_package(YARP REQUIRED)
find_package(ICUB REQUIRED)
find_package(ICUBcontrib REQUIRED)
#optionally: use cmake find scripts provided by YARP and iCub
icubcontrib_set_default_prefix() #set CMAKE_INSTALL_PREFIX to the ICUBcontrib one; print warning if user modifies it
## useful options (i.e., rpath settings and name of debug libraries on msvc)
#find packages

set(folder_source src/file1.cpp src/file2.cpp)
set(folder_header include/iCub/file1.h include/iCub/file2.h)

# add local directory and other (optional) packages to to search path for 
# header files 

source_group("Source Files" FILES ${folder_source})
source_group("Header Files" FILES ${folder_header})

add_library(${PROJECT_NAME} ${folder_source} ${folder_header})
#link other packages, this is required to let cmake know dependencies
#your code will compile without this, but you can have problems linking agains
#your lib in modules using it

if you want to link all the libraries in iCub, you can rely on the ICUB_LIBRARIES variable:

#link against all libraries in the iCub build
target_link_libraries(${PROJECT_NAME} ${ICUB_LIBRARIES})

but you might prefer to just link the one you actually need:

 #link against single libraries in the iCub package
 #Important: notice that we use the name of the project
 target_link_libraries(${PROJECT_NAME} ctrlLib)

Now, if you want your library to be imported by 3rd-party projects through CMake, you can export it with the icubcontrib_export_library(...) macro. The paradigm is the same as for the icub_export_library(...) macro in the "main" iCub repository . Example:

 icubcontrib_export_library(${PROJECT_NAME} INTERNAL_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include 
                     EXTERNAL_INCLUDE_DIRS ${YARP_INCLUDE_DIRS}  # only list directory for headers that are included by the header files of the library you are exporting
                     DESTINATION include/iCub
                     FILES f${foler_header})

You can export multiple libraries within the same source tree, calling the icubcontrib_export_library(...) macro for each of them. In order to be able to all the libraries as a single package, you need to call then the icubcontrib_finalize_export(<packagename>) macro:


This allows 3rd-party projects to find and use your library like this:

 target_link_libraries(my3dpartyproj mylibrary)

Note that:

  • you need to call the icubcontrib_finalize_export macro even if you are only exporting one library (<packagename> can be the same as the library name)
  • for 3d-party projects to be able to find your exported library, its exported configuration files should be in the CMake search list; if users do not install icub-contrib libraries to default system directories, they are advised to set the CMAKE_PREFIX_PATH environment variable to point to the ICUBcontrib installation path (see the Setup ICUBcontrib sections in the installation instructions).