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_buildas a dependency in your package'smanifest.xml(this allows you to rosbuild_include). - Set up your
CMakeLists.txtto bring in ecl's cmake macro libraries, similar to the following example
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