0
$\begingroup$

I am trying to understand custom memory allocators for ROS2. To understand the functioning of tlsf I am using tlsf_example wrapper project present in src/ros2/realtime_support. I have added dummy print statements to src/ros2/tlsf/tlsf/src/tlsf.c, to just verify if my changes are being applied. The colcon command I ran to build is colcon build --cmake-args -DCMAKE_BUILD_TYPE=Debug --packages-select tlsf tlsf_cpp. I am running the binary present in src/install/tlsf_cpp/lib/tlsf_cpp. But I don't see any of my changes being outputed to terminal. I also tried using gdb but I am not getting any source line info for tlsf_malloc function defined in tlsf.c file.

CMakeLists.txt file for tlsf.c

cmake_minimum_required(VERSION 3.5)

project(tlsf)

if(WIN32 OR APPLE OR ANDROID)
  message(STATUS "tlsf allocator does not target Windows, OSX or Android, skipping...")
  return()
endif()

# Default to C11
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 11)
endif()
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic -g -O0)
endif()

find_package(ament_cmake REQUIRED)

add_library(${PROJECT_NAME} STATIC src/tlsf.c src/target.h include/tlsf/tlsf.h)
target_include_directories(${PROJECT_NAME} PUBLIC
  "$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/tlsf>"
  "$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
set_target_properties(${PROJECT_NAME} PROPERTIES C_VISIBILITY_PRESET hidden)

ament_export_targets(export_${PROJECT_NAME})

# Disable linting for now.
#if(BUILD_TESTING)
#  find_package(ament_lint_auto REQUIRED)
#  ament_lint_auto_find_test_dependencies()
#endif()

ament_package()

install(DIRECTORY include/
  DESTINATION include/${PROJECT_NAME}
)

install(
  TARGETS ${PROJECT_NAME}
  EXPORT export_${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

I am sure that the build command touches tlsf.c file after changes but I am unable to observe any changes in the output. I want to know whether the application I am running actually links to the changed file, is there any way to achieve this? What changes should I do to the CMakeLists.txt file for tlsf? Is the binary I ran the correct binary to expect changes? Also how can I debug tlsf_malloc function in ROS2? I have very little idea about ROS build system, any resources to learn more about it are welcome.

$\endgroup$

1 Answer 1

0
$\begingroup$

In cmake, add_library() is used to build a library. It does not link it into the project executable. target_link_libraries() does that.

https://cmake.org/cmake/help/latest/command/add_library.html

If the compiler can see both a shared library and a static library with the same library name, I don't recall what the compiler does. You may need to explicitly provide the .a suffix to tell the compiler to use the static version.

$\endgroup$
2
  • $\begingroup$ Thanks for the answer. I used absolute paths in target_link_libraries to link modified tlsf library with tlsf_cpp project. That solved my problem. Previously I was just passing reelative paths but I guess they weren't getting resolved as I had expeected. I was just wondering how can I use relative path instead of absolute ones in the CMakeLists.txt files? $\endgroup$
    – red_demise
    Commented Nov 23, 2023 at 2:13
  • $\begingroup$ If I'm understanding you correctly, the simplest approach is to give your custom library a unique name. Then use that library name in the target_link_libraries() command. You should not need to specify any paths. $\endgroup$
    – Mike973
    Commented Nov 23, 2023 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.