Note: This tutorial assumes that you have completed the previous tutorials: The Ecl CMake Library.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Link as Needed

Description: Remove unused library dependencies from being automatically linked by ros.

Keywords: ecl linking

Tutorial Level: INTERMEDIATE

This tutorial is no longer relevant for most platforms supporting ros hydro and beyond since gcc has switched its internal behaviour to link as needed.

Problem

The ros framework automatic dependency management will often bring in far more libraries as a dependency to your library/app than it actually uses. Actually, linux build frameworks used to have this issue as well. For us this often means we're pulling in ros dependencies when ecl is actually not depending on any ros code (just the build environment). It also means we string together too many dependencies within the ecl as well, which creates a size problem for embedded builds.

To get around this, most distros have moved to using the --link-as-needed flag with gcc to automatically prune these redundant dependencies. This flag is different on different platforms though (e.g. macosx use a different flag), so ecl provides a cmake macro which can be used to set this for your package. In your CMakeLists.txt:

Solution

  • Include ecl_build as a dependency in your package's manifest.xml (this allows you to rosbuild_include).
  • Set up your CMakeLists.txt to bring in ecl's cmake macro libraries as follows:

cmake_minimum_required(VERSION 2.4.6)

include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()
rosbuild_include(ecl_build ecl_platform_detection)

ecl_link_as_needed(ECL_LINK_AS_NEEDED_FLAG)
set(ROS_LINK_FLAGS "${ROS_LINK_FLAGS} ${ECL_LINK_AS_NEEDED_FLAG}")

# Actual build stuff here

Wiki: ecl_build/Tutorials/Link as Needed (last edited 2014-04-05 06:52:32 by DanielStonier)