colcon/colcon-core

[Question] Correct way to build a ros driver which depends on a library without having to run 'sudo make install' on the library.

vik748 opened this issue · 6 comments

vik748 commented

My big picture goal is to be able to build and use a c++ ros driver which depends on a c++ library without having to run 'sudo'.

For my example, I am trying to build and use ydlidar_ros2_driver which depends on YDLidar-SDK.

So first I install the dependency locally with:

cd ~
git clone https://github.com/YDLIDAR/YDLidar-SDK ydlidar_sdk
mkdir ydlidar_sdk/build
cd ydlidar_sdk/build
cmake --install-prefix /home/<USER>/install ..
make install

So this builds and installs it in ~/install
Now to make the ROS driver I do:

mkdir -p ~/ros_ws/src
cd ~/ros_ws/src
git clone git clone https://github.com/YDLIDAR/ydlidar_ros2_driver.git ~/ros_ws/src/ydlidar_ros2_driver
cd ~/ros_ws/src/ydlidar_ros2_driver
git checkout humble
colcon build ydlidar_ros2_driver --symlink-install --cmake-args "-D CMAKE_PREFIX_PATH=/home/<USER>/install"

The build seems to work find, but I get a linking error:

/usr/bin/ld: cannot find -lydlidar_sdk: No such file or directory
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/ydlidar_ros2_driver_node.dir/build.make:206: ydlidar_ros2_driver_node] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:139: CMakeFiles/ydlidar_ros2_driver_node.dir/all] Error 2
gmake[1]: *** Waiting for unfinished jobs....
gmake: *** [Makefile:146: all] Error 2

How do I fix this issue and what is the best practice for dealing with non-system installed packages?
Thanks in advance for any advice. Cheers.

Change to `--cmake-args "-DCMAKE_PREFIX_PATH=/home//install".

Make sure you call find_package(ydlidar_ros2_driver REQUIRED). It seems you aren't doing this, and it's instead failing at link time rather than configure time.

vik748 commented

I am assuming you meant I should remove the space between -D and CMAKE_PREFIX_PATH, I did try it and it didn't make a difference.
As you can see here there is already a find_package(ydlidar_sdk REQUIRED) in the ros_driver's CMakeLists.txt file.

Use target_link_libraries instead of link_directories in CMake 3.

What does this build error have to do with colcon?

It doesn't. It's a user error, this is just standard CMake. I would recommend getting help on the CMake user forums.

vik748 commented

Thanks for your help folks, I asked here since this problem only showed up when building the ROS driver and I figured there might be something getting missed out when running colcon build. So looks like the SDK library ydlidar_sdk is exporting the YDLIDAR_SDK_LIBRARY_DIRS variable but leaving it empty. I need to figure out the right way to configure its CMakeLists or install_package.cmake to make this work correctly. Once again thanks for the help.