Skip to content

Add -Wl,--no-undefined to make compilation of components fail if a symbol is not defined? #10

@VRichardJP

Description

@VRichardJP

Checklist

  • I've read the contribution guidelines.
  • I've searched other issues and no duplicate issues were found.
  • I've agreed with the maintainers that I can plan this task.

Description

Autoware relies a lot on ROS2 components, which are nothing but shared library.
Because it is totally fine for a shared library to contain undefined symbols, the linker won't complain if it is the case at compilation time.
However, if you try to load into a ROS2 container a component with undefined symbols at runtime, then your application will crash.

Not catching undefined symbols when compiling components is thus quite annoying.
One solution is to add tests or to link it to a binary, but this means extra compilation time.
Another simple solution is to pass -Wl,--no-undefined to the target libraries.

For example, I have commented a function definition in gnss_poser_node.cpp, then I have added the following line to autoware_cmake:

# DIRTY? add -Wl,--no-undefined to *ALL* shared libraries
add_link_options(
      "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:LINKER:--no-undefined>")

With this extra flag, I get the following compilation error:

/home/sig/autoware/src/universe/autoware.universe/sensing/gnss_poser/src/gnss_poser_core.cpp:46: undefined reference to `gnss_poser::GNSSPoser::callbackMapProjectorInfo(std::shared_ptr<tier4_map_msgs::msg::MapProjectorInfo_<std::allocator<void> > const>)'
collect2: error: ld returned 1 exit status

Whereas without the flag no error is reported (but the application would crash at runtime).

Purpose

Detect broken components at compilation time

Possible approaches

Add the flag to all shared libraries:
add_link_options("$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:LINKER:--no-undefined>")

Or add target_link_options(tgt PRIVATE LINKER:--no-undefined) to individual components.

Definition of done

Depend on chosen solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions