ROSDS - colcon build fails - environment dependency requirement

Hi The Construct Team,

I am currently working on the Course Project [rosject] for ROS2 Basics in 5 Days - Python.
I created my custom interfaces package but colcon build ... fails.

I tried creating the exact same package in course environment and it works fine. There seems to be some issue with ROSDS environment, I believe, seems more like dependency requirement.

TL;DR / Summary: It seems the ROSDS environment is missing pthreads in path /usr/bin/ld as stated in last part of CMakeError.log file

Output of colcon build --packages-select my_interfaces --event-handlers console_cohesion+ :

user:~/ros2_ws$ colcon build --packages-select my_interfaces --event-handlers console_cohesion+
Starting >>> my_interfaces
--- output: my_interfaces
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ament_cmake: 0.9.9 (/opt/ros/foxy/share/ament_cmake/cmake)
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3")
-- Using PYTHON_EXECUTABLE: /usr/bin/python3
-- Found rclcpp: 2.4.0 (/opt/ros/foxy/share/rclcpp/cmake)
-- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c
-- Found rosidl_adapter: 1.2.1 (/opt/ros/foxy/share/rosidl_adapter/cmake)
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1f")
-- Found FastRTPS: /opt/ros/foxy/include
-- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp
-- Found rmw_implementation_cmake: 1.0.3 (/opt/ros/foxy/share/rmw_implementation_cmake/cmake)
-- Using RMW implementation 'rmw_fastrtps_cpp' as default
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found geometry_msgs: 2.0.4 (/opt/ros/foxy/share/geometry_msgs/cmake)
-- Found action_msgs: 1.0.0 (/opt/ros/foxy/share/action_msgs/cmake)
-- Found rosidl_default_generators: 1.0.1 (/opt/ros/foxy/share/rosidl_default_generators/cmake)
-- Found PythonInterp: /usr/bin/python3 (found version "3.8.10")
-- Configuring incomplete, errors occurred!
See also "/home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeOutput.log".
See also "/home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeError.log".
---
Failed   <<< my_interfaces [2.60s, exited with code 1]

Summary: 0 packages finished [2.75s]
  1 package failed: my_interfaces

Contents of CMakeError.log file:

Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make cmTC_d6686/fast && /usr/bin/make -f CMakeFiles/cmTC_d6686.dir/build.make CMakeFiles/cmTC_d6686.dir/build
make[1]: Entering directory '/home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_d6686.dir/src.c.o
/usr/bin/cc   -DCMAKE_HAVE_LIBC_PTHREAD   -o CMakeFiles/cmTC_d6686.dir/src.c.o   -c /home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_d6686
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d6686.dir/link.txt --verbose=1
/usr/bin/cc  -DCMAKE_HAVE_LIBC_PTHREAD    CMakeFiles/cmTC_d6686.dir/src.c.o  -o cmTC_d6686 
/usr/bin/ld: CMakeFiles/cmTC_d6686.dir/src.c.o: in function `main':
src.c:(.text+0x46): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x52): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x63): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_d6686.dir/build.make:87: cmTC_d6686] Error 1
make[1]: Leaving directory '/home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_d6686/fast] Error 2


Source file was:
#include <pthread.h>

void* test_func(void* data)
{
  return data;
}

int main(void)
{
  pthread_t thread;
  pthread_create(&thread, NULL, test_func, NULL);
  pthread_detach(thread);
  pthread_join(thread, NULL);
  pthread_atfork(NULL, NULL, NULL);
  pthread_exit(NULL);

  return 0;
}

Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/bin/make cmTC_d2b17/fast && /usr/bin/make -f CMakeFiles/cmTC_d2b17.dir/build.make CMakeFiles/cmTC_d2b17.dir/build
make[1]: Entering directory '/home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_d2b17.dir/CheckFunctionExists.c.o
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_d2b17.dir/CheckFunctionExists.c.o   -c /usr/share/cmake-3.16/Modules/CheckFunctionExists.c
Linking C executable cmTC_d2b17
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d2b17.dir/link.txt --verbose=1
/usr/bin/cc  -DCHECK_FUNCTION_EXISTS=pthread_create    CMakeFiles/cmTC_d2b17.dir/CheckFunctionExists.c.o  -o cmTC_d2b17  -lpthreads 
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_d2b17.dir/build.make:87: cmTC_d2b17] Error 1
make[1]: Leaving directory '/home/user/ros2_ws/build/my_interfaces/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_d2b17/fast] Error 2

CMakeLists.txt file contents:

cmake_minimum_required(VERSION 3.5)
project(my_interfaces)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(action_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/FindWall.srv"
  "action/OdomRecord.action"
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

package.xml file contents:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>my_interfaces</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="user@todo.todo">user</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>std_msgs</depend>
  <depend>geometry_msgs</depend>
  <depend>action_msgs</depend>
  <depend>rosidl_default_generators</depend>

  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

Please let me know if you would also require CMakeOutput.log file contents. The contents are large, so I removed it from this post.

I tried to Google this issue, but this seems to be more of a fix required on your side, I believe.

Please fix this issue and let me know!

Thanks
Girish

After Googling for a long time regarding this issue, this seems to be having a dead-end.
My observations from may answers on stack exchange and reddit posts were that:

  1. Not to include find_package(Threads) - seems to be already present in the colcon build routine.
  2. -lpthreads is a typo and must be corrected to -lpthread.
  3. Upgrade / re-install g++ or libboost-dev.

I am not sure what file actually does colcon building. Maybe changing -lpthreads to -lpthread might fix this issue.

I am not sure how to proceed!

Hi The Construct Team,

New update: Found a problem almost similar to my problem on ROS Discourse

https://answers.ros.org/question/359578/colcon-build-symlink-install-cant-find-__main__-module-ld-cannot-find-lpthreads/

(I have put the link in code block because hyperlink did not work correctly!)

I will try to relate the solution to my problem and attempt to fix this.

Please do keep me informed if any of you could solve this issue.

Thanks,
Girish

New Update:

Commenting out these lines on the CMakeLists.txt file, the colcon build succeeds. However, with these lines uncommented, the colcon build fails with the same error: cannot find -lpthreads (which basically defeats the purpose of having custom interfaces).

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/FindWall.srv"
  "action/OdomRecord.action"
)

I did try playing around with moving the package outside of ros2_ws and creating a new workspace (named colcon_ws), but the error still kept happening.

I referred to some problems stated on ROS Answers website, but they were either not applicable or not related to Ubuntu or not having accepted answers.
To save some googling time, here are the links:

  1. Cannot install ros-melodic-rostime on Archlinux - ROS Answers: Open Source Q&A Forum
  2. rosidl_generate_interfaces fails (with no error message) with baroque path names - ROS Answers: Open Source Q&A Forum

The good news here is: The custom interface package creation works well in both Python and C++ course environments of ROS2 Basics in 5 Days. It is just not working in the rosject / rosds environment.

I think replicating the same environment as course in rosds will fix the issue.

Also checked the CMake versions in the course and the rosds environment - they are same 3.16.3.

For the time being, I cannot proceed with the ROS2 course project. [Sorry for the long post.]

Thanks.
Girish

Hi The Construct Team,

Were you able to work on this problem or happen to know any fixes? Please update me if possible.
[Problem still persists as of Monday, October 24, 2022]

I am planning to post this issue in ROS Answers too. This error seems to be a colcon specific build error. I believe someone could help with this problem there.

Thanks,
Girish

UPDATE:

Posted the same problem on ROS Answers.
Link here: https://answers.ros.org/question/408434/ros2-colcon-build-fails-with-custom-interface-cannot-find-lpthreads/

— Girish

Update: I now have a Good and a Bad news.

Good news: At the moment, colcon build is successful with action message removed from the CMakeLists.txt file, like shown below:

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/FindWall.srv"
)
# "action/OdomRecord.action"

Bad News: If I uncomment action message and place the line back inside rosidl_generate_interfaces(...), the colcon build fails with these errors:

Error processing 'geometry_msgs/msg/Point32[] list_of_odoms' of
'custom_interfaces/OdomRecord_Result': 'geometry_msgs/msg/Point32'

(and)

rosidl_adapter.parser.InvalidResourceName: geometry_msgs/msg/Point32

That’s all for this update.

Thanks,
Girish

Final Update [ also the Solution ]: The problem is finally Solved !

The above mentioned action message error can be fixed by changing geometry_msgs/msg/Point32[] list_of_odoms —> to —> geometry_msgs/Point32[] list_of_odoms. The additional /msg is not required in the interface definition. That is what causes the Error processing … message along with the rosidl_adapter.parser error message.

Regarding the service message: There was not any real fixes. Everything was done right. I just waited for a few days and it got fixed by itself. I did nothing to solve that issue. I also do not know what happened.
[As a side note, I did notice a non-ASCII character in my service interface definition file. I removed that character too, to keep the contents 100% ASCII characters.]

Also adding DEPENDENCIES as shown below, does not have any effect. It is better without it!

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/FindWall.srv"
  "action/OdomRecord.action"
  # DEPENDENCIES std_msgs geometry_msgs  <--- this line has no effect !
)

colcon build ... command now succeeds and the package is built to 100% successfully !

Thanks and Regards,
Girish

1 Like

Hello @girishkumar.kannan ,

Just saw this post. Glad that you were able to fix it. I’ll add a note in the course to make this point very clear and avoid further confusion.

2 Likes

I experience exactly same issue. I tried to recreate VM, same problem.

Concon build fails without error message.
user:~/ros2_ws$ colcon build
Starting >>> wf_interfaces
Failed <<< wf_interfaces [1.19s, exited with code 2]

Summary: 0 packages finished [1.33s]
1 package failed: wf_interfaces
1 package not processed

In logs:
– Found ament_cmake: 0.9.9 (/opt/ros/foxy/share/ament_cmake/cmake)
– Found PythonInterp: /usr/bin/python3 (found suitable version “3.8.10”, minimum required is “3”)
– Using PYTHON_EXECUTABLE: /usr/bin/python3
– Found rclcpp: 2.4.0 (/opt/ros/foxy/share/rclcpp/cmake)
– Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c
– Found rosidl_adapter: 1.2.1 (/opt/ros/foxy/share/rosidl_adapter/cmake)
– Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp
– Found rmw_implementation_cmake: 1.0.3 (/opt/ros/foxy/share/rmw_implementation_cmake/cmake)
– Using RMW implementation ‘rmw_fastrtps_cpp’ as default
– Found geometry_msgs: 2.0.4 (/opt/ros/foxy/share/geometry_msgs/cmake)
– Found rosidl_default_generators: 1.0.1 (/opt/ros/foxy/share/rosidl_default_generators/cmake)
– Found PythonInterp: /usr/bin/python3 (found version “3.8.10”)
– Configuring incomplete, errors occurred!
See also “/home/user/ros2_ws/build/wf_interfaces/CMakeFiles/CMakeOutput.log”.
See also “/home/user/ros2_ws/build/wf_interfaces/CMakeFiles/CMakeError.log”.

Error log CMakeError.log:
Run Build Command(s):/usr/bin/make cmTC_f0824/fast && /usr/bin/make -f CMakeFiles/cmTC_f0824.dir/build.make CMakeFiles/cmTC_f0824.dir/build

make[1]: Entering directory ‘/home/user/ros2_ws/build/wf_interfaces/CMakeFiles/CMakeTmp’

Building C object CMakeFiles/cmTC_f0824.dir/src.c.o

/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_f0824.dir/src.c.o -c /home/user/ros2_ws/build/wf_interfaces/CMakeFiles/CMakeTmp/src.c

Linking C executable cmTC_f0824
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_f0824.dir/link.txt --verbose=1
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_f0824.dir/src.c.o -o cmTC_f0824
/usr/bin/ld: CMakeFiles/cmTC_f0824.dir/src.c.o: in function main': src.c:(.text+0x46): undefined reference to pthread_create’
/usr/bin/ld: src.c:(.text+0x52): undefined reference to pthread_detach' /usr/bin/ld: src.c:(.text+0x63): undefined reference to pthread_join’
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_f0824.dir/build.make:87: cmTC_f0824] Error 1
make[1]: Leaving directory ‘/home/user/ros2_ws/build/wf_interfaces/CMakeFiles/CMakeTmp’
make: *** [Makefile:121: cmTC_f0824/fast] Error 2

If i comment “action/OdomRecord.action”, build is successful.
rosidl_generate_interfaces(${PROJECT_NAME}
“srv/FindWall.srv”
“action/OdomRecord.action”
)

File action/OdomRecord.action:

geometry_msgs/msg/Point list_of_odoms

float32 current_total

Hi @grif ,

Here is the solution: RosJect did work but now shows error - #15 by girishkumar.kannan

Regards,
Girish

@bayodesegun Please close this issue when you get time. Thanks!

1 Like