Skip to content

CMake Args Ignored #140

Closed
Closed

Description

@eeyang92

Hi, hopefully this is the right place to ask this - I've been trying to create a statically linked package via rclcpp, but whenever I run the command colcon build --cmake-args -DBUILD_SHARED_LIBS=OFF on the examples project (https://github.com/ros2/examples), I get:

...

Finished <<< examples_rclcpp_minimal_subscriber [12.3s]
--- stderr: examples_rclcpp_minimal_timer
CMake Warning:
  Manually-specified variables were not used by the project:

    BUILD_SHARED_LIBS

...

Summary: 13 packages finished [25.9s]
  7 packages had stderr output: examples_rclcpp_minimal_action_client examples_rclcpp_minimal_action_server examples_rclcpp_minimal_client examples_rclcpp_minimal_publisher examples_rclcpp_minimal_service examples_rclcpp_minimal_subscriber examples_rclcpp_minimal_timer

If I attempt to run the executable in the /install/examples_rclcpp_minimal_publisher/lib/examples_rclcpp_minimal_publisher, in a fresh terminal (without sourcing the setup.bash file) I'll get an error stating:

./publisher_member_function: error while loading shared libraries: librclcpp.so: cannot open shared object file: No such file or directory

Which (I think) confirms that the arg was ignored.

Am I missing something?


Ubuntu 18.04 LTS
cmake version 3.10.2
colcon version-check:

colcon-argcomplete 0.2.2: newer version available (0.2.3)
colcon-bash 0.3.1: up-to-date
colcon-cmake 0.2.4: up-to-date
colcon-core 0.3.14: up-to-date
colcon-defaults 0.2.1: up-to-date
colcon-devtools 0.2.1: up-to-date
colcon-library-path 0.2.0: up-to-date
colcon-metadata 0.2.1: up-to-date
colcon-notification 0.2.5: up-to-date
colcon-output 0.2.3: up-to-date
colcon-package-information 0.2.1: up-to-date
colcon-package-selection 0.2.1: up-to-date
colcon-parallel-executor 0.2.2: up-to-date
colcon-pkg-config 0.1.0: up-to-date
colcon-powershell 0.3.3: up-to-date
colcon-python-setup-py 0.2.1: up-to-date
colcon-recursive-crawl 0.2.0: up-to-date
colcon-ros 0.3.5: up-to-date
colcon-test-result 0.3.0: up-to-date
colcon-zsh 0.3.1: up-to-date

Activity

  1. added
    question
    on Dec 26, 2018
  2. dirk-thomas commented on Dec 26, 2018

    @dirk-thomas
    Member

    The warning in examples_rclcpp_minimal_subscriber comes from the fact that the package doesn't declare that CMake option. The option is being defined in the ament_cmake_ros package which the example package doesn't use (since it doesn't build a library it doesn't have to). You can suppress the warning by passing the CMake argument --no-warn-unused-cli.

    From your description it isn't clear what packages you built in your workspace - only that contains 13 packages. Also you mention that you ran the example "without sourcing the setup.bash file". Shared libraries can't be found when the environment variable LD_LIBRARY_PATH isn't defined so that would explain why the example fails to load the shared library.

    In order to help you please provide complete steps to reproduce your issues.

  3. eeyang92 commented on Dec 27, 2018

    @eeyang92
    Author

    I want to be able to compile the example repository, and be able to run the output binaries anywhere (even on a different system - with the same architecture). This would require me to statically link the libraries, so --no-warn-unused-cli wouldn't work for me.

    Or is this not possible and I should just include the shared libraries in my packages, even on a different system?

  4. dirk-thomas commented on Dec 27, 2018

    @dirk-thomas
    Member

    This would require me to statically link the libraries, so --no-warn-unused-cli wouldn't work for me.

    The package examples_rclcpp_minimal_subscriber doesn't build any libraries therefore the BUILD_SHARED_LIBS options doesn't do anything for it. That is the reason for the warning and you can suppress it with the mentioned CMake argument (or you can just ignore the stderr output).

    In order to build the example without depending on any shared library you have to build the packages which provide these libraries with the BUILD_SHARED_LIBS option, e.g. rcutils, rcl, rclcpp, etc. That is why I mentioned the number of packages in your workspace. I suspect that you are not trying to build all the dependency packages yourself.

    Or is this not possible and I should just include the shared libraries in my packages, even on a different system?

    It is possible to build the packages with static libraries - you just have to actually build all packages which do contain libraries. Or you make sure that the shared libraries are available on the target system and findable via LD_LIBRARY_PATH ( I assume you are not aiming for a different architecture here, so no cross compiling is necessary).

    As mentioned before:

    In order to help you please provide complete steps to reproduce your issues.

  5. eeyang92 commented on Dec 27, 2018

    @eeyang92
    Author

    Original replication steps:

    1. Build from source, following the instructions here: https://index.ros.org/doc/ros2/Linux-Development-Setup/
      • Built via colcon build --symlink-install
    2. Clone the example here: https://github.com/ros2/examples
    3. Source: . ./ros2_dir/install/setup.bash
    4. Build via colcon build, also colcon build --cmake-args -DBUILD_SHARED_LIBS=OFF
    5. Attempt to run one of the example binaries in a new terminal without sourcing, resulting in the error in the OP

    After reading your message, it seems like I also need to build the entirety of ros2 statically? At least the ones you mentioned (i.e. rcutils).

    I then tried:

    1. Building ros2 from source, with colcon build --cmake-args -DBUILD_SHARED_LIBS=OFF
      • Warning that BUILD_SHARED_LIBS is ignored
      • Build failed for a majority of the modules due to some error about symlinked libraries
    2. Source: . ./ros2_dir/install/setup.bash
    3. Clone the example, and build with colcon build --cmake-args -DBUILD_SHARED_LIBS=OFF
      • Warning that BUILD_SHARED_LIBS is ignored
      • Failed due to missing packages in step 1

    Second attempt:

    1. Building ros2 from source, with colcon build --symlink-install
      • Manually build some of the libraries that seemed relevant (including the ones you listed previously): colcon build --packages-select rcutils rclcpp rcl_interfaces rcl --cmake-args -DBUILD_SHARED_LIBS=OFF
      • No errors reported
    2. Source: . ./ros2_dir/install/setup.bash
    3. Clone the example, and build with colcon build
    4. Attempt to run one of the example binaries in a new terminal without sourcing, same error as in OP
      • ./install/examples_rclcpp_minimal_publisher/lib/examples_rclcpp_minimal_publisher/publisher_member_function
        ./publisher_member_function: error while loading shared libraries: librclcpp.so: cannot open shared . 
        object file: No such file or directory
        

    I could be totally misunderstanding the issue and I apologize - it's been a long time since I've worked with CMake files and adding another layer of abstraction above that (colcon and ROS2) has made it so I really can't figure this out myself.

    Do you have any documentation on how to setup ROS2 (and colcon) to build statically from a fresh source code download of ros2? I've seen some of your previous comments (ros2/ros2#468) and that it does look possible for objective-C (https://github.com/esteve/ros2_objc), but I can't find exactly what needs to be done to make sure everything is working as expected.

    EDIT:

    I should also add that I took a look into the install folder of the static build of rclcpp, and I found the static library does exist:

    ericy@ros2-2:~/ros2_ws_static/install/rclcpp/lib$ ls
    librclcpp.a  librclcpp.so
    

    However, the binary built from the example still wants to look for the .so file? (Even when built without the symlink option)

  6. added
    invalid
    and removed
    question
    on Dec 27, 2018
  7. dirk-thomas commented on Dec 27, 2018

    @dirk-thomas
    Member

    After reading your message, it seems like I also need to build the entirety of ros2 statically?

    Yes. The rational is simple: a package like rclcpp provides a library rclcpp - by default a shared library but using the CMake option it can also be built into a static library. An example package builds an executable which is linked to other libraries like rclcpp. If you want the example to not depend on any shared libraries you need to build all packages providing libraries used by that executable using that CMake option.

    Build failed for a majority of the modules due to some error about symlinked libraries

    Without sharing the actual error message I can't provide any help.

    Manually build some of the libraries that seemed relevant (including the ones you listed previously)

    There will be more recursive dependencies than the ones I explicitly mentioned. Rather than manually enumerating those that information is available from the package manifest files and can be extracted automatically.

    Not every package will support to be built into static libraries. E.g. you can only have one typesupport package (otherwise it needs to build shared libraries to choose one of them at runtime). So you might want to limit the build to packages you actually want to use and only have a single RMW implementation installed (e.g. only FastRTPS but not Debian packages for Connext and OpenSplice). Then you should be able to run this build:

    colcon build --packages-up-to examples_rclcpp_minimal_timer --packages-ignore rosidl_typesupport_introspection_c rosidl_typesupport_introspection_cpp rosidl_generator_py rmw_fastrtps_dynamic_cpp --cmake-args -DBUILD_SHARED_LIBS=OFF --no-warn-unused-cli

    You can list multiple package names behind --packages-up-to.

    In principle this approach should work (and it has in the past) but a quick test shows that it currently fails for several packages. I have created ros2/ros2#635 to track the problem with some ROS packages.

    Since this is not a problem in colcon itself I will go ahead an close this issue in favor of the newly created one.

  8. eeyang92 commented on Dec 27, 2018

    @eeyang92
    Author

    Ah, --packages-up-to is really handy!

    Sorry, I should have included the error message I received before:

    /usr/bin/ld: /home/ericy/ros2_ws_static/install/rcutils/lib/librcutils.a(allocator.c.o): relocation R_X86_64_PC32 against symbol `rcutils_allocator_is_valid' can not be used when making a shared object; recompile with -fPIC
    

    Which ties into what you said about not all modules being able to be built statically.

    Running with colcon build --packages-up-to examples_rclcpp_minimal_publisher --packages-ignore rosidl_typesupport_introspection_c rosidl_typesupport_introspection_cpp rosidl_generator_py rmw_fastrtps_dynamic_cpp --cmake-args -DBUILD_SHARED_LIBS=OFF --no-warn-unused-cli, I get:

    ...
    /usr/bin/ld: /home/ericy/ros2_ws_static/install/fastcdr/lib/libfastcdr.a(Cdr.cpp.o): relocation R_X86_64_PC32 against symbol `_ZN8eprosima7fastcdr9exception24NotEnoughMemoryException33NOT_ENOUGH_MEMORY_MESSAGE_DEFAULTE' can not be used when making a shared object; recompile with -fPIC
    ...
    

    -fPIC causes it to not build at all:

    CMake Error: The source directory "/home/ericy/ros2_ws_static/build/fastcdr/-fPIC" does not exist.
    

    But it seems like you've also confirmed it isn't working, so for now, I'll leave it as is, since I'm not at a point where I absolutely need a standalone executable (although it is planned down the road).

    Thanks for your help! I will keep an eye out on ros2/ros2#635

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions