Robotics StackExchange | Archived questions

C++ ROS Tutorial Chatter

Hello everyone, I'm trying to do this simple tutorial using C++ but for some reason i'm having difficulty with it since no executable files are generated under devel/lib

files:

Publisher:

#include "ros/ros.h"
#include "std_msgs/Int32.h"
#include <iostream>

int main (int argc, char **argv)
{
    ros::init(argc, argv,"demo_topic_publisher"); 
    ros::NodeHandle node_obj;     
    ros::Publisher number_publisher = node_obj.advertise<std_msgs::Int32>("/numbers",10); 
    ros::Rate loop_rate(10);     
    int number_count = 0;     

    while (ros::ok()) 
    {
        std_msgs::Int32 msg;  
        msg.data = number_count;     
        ROS_INFO("%d",msg.data);    
        number_publisher.publish(msg);    
        ros::spinOnce();     
        loop_rate.sleep();     
        ++number_count;
    }

    return 0;    
}

Subscriber:

#include "ros/ros.h"
#include "std_msgs/Int32.h"
#include <iostream>

void number_callback(const std_msgs::Int32::ConstPtr& msg)
{    
    ROS_INFO("Received [%d]",msg->data);    
}

int main(int argc, char **argv)
{    
    ros::init(argc, argv, "demo_topic_subscriber");         
    ros::NodeHandle node_obj;       
    ros::Subscriber number_subscriber = node_obj.subscribe("/numbers", 10, number_callback);            
    ros::spin();            
    return 0;     
}

CMakeList.txt:

cmake_minimum_required(VERSION 2.8.3)
project(book_mr)

find_package(catkin REQUIRED COMPONENTS
  actionlib
  actionlib_msgs
  roscpp
  std_msgs
)

catkin_package()

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)  

add_executable(demo_topic_publisher src/demo_topic_publisher.cpp)
add_executable(demo_topic_subscriber src/demo_topic_subscriber.cpp)    

add_dependencies(demo_topic_publisher book_mr_generate_messages_cpp)
add_dependencies(demo_topic_subscriber book_mr_generate_messages_cpp)    

target_link_libraries(demo_topic_publisher ${catkin_LIBRARIES})
target_link_libraries(demo_topic_subscriber ${catkin_LIBRARIES})

update 1:

When I use this command to properly initialise the workspace:

    $ catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3

This error comes up:

(base) banana@Science:~/catkin_ws$ catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
Base path: /home/banana/catkin_ws
Source space: /home/banana/catkin_ws/src
Build space: /home/banana/catkin_ws/build
Devel space: /home/banana/catkin_ws/devel
Install space: /home/banana/catkin_ws/install
####
#### Running command: "cmake /home/banana/catkin_ws/src -DPYTHON_EXECUTABLE=/usr/bin/python3 -DCATKIN_DEVEL_PREFIX=/home/banana/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/banana/catkin_ws/install -G Unix Makefiles" in "/home/banana/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/banana/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/banana/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/banana/catkin_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.6.9", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python3
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/banana/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python3 (found version "3.6.9") 
-- Using Python nosetests: /usr/bin/nosetests-2.7
ImportError: "from catkin_pkg.package import parse_package" failed: No module named 'catkin_pkg'
Make sure that you have installed "catkin_pkg", it is up to date and on the PYTHONPATH.
CMake Error at /opt/ros/melodic/share/catkin/cmake/safe_execute_process.cmake:11 (message):
  execute_process(/usr/bin/python3
  "/opt/ros/melodic/share/catkin/cmake/parse_package_xml.py"
  "/opt/ros/melodic/share/catkin/cmake/../package.xml"
  "/home/banana/catkin_ws/build/catkin/catkin_generated/version/package.cmake")
  returned error code 1
Call Stack (most recent call first):
  /opt/ros/melodic/share/catkin/cmake/catkin_package_xml.cmake:74 (safe_execute_process)
  /opt/ros/melodic/share/catkin/cmake/all.cmake:168 (_catkin_package_xml)
  /opt/ros/melodic/share/catkin/cmake/catkinConfig.cmake:20 (include)
  CMakeLists.txt:56 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/banana/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/banana/catkin_ws/build/CMakeFiles/CMakeError.log".
Invoking "cmake" failed

CMakeOutput.log is kindof huge and i cant upload the file with less than 5 pts, i'll upload it if needed

CMakeError.log:

Determining if the pthread_create exist failed with the following output:
Change Dir: /home/banana/catkin_ws/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTC_a5239/fast"
/usr/bin/make -f CMakeFiles/cmTC_a5239.dir/build.make CMakeFiles/cmTC_a5239.dir/build
make[1]: Entering directory '/home/banana/catkin_ws/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_a5239.dir/CheckSymbolExists.c.o
/usr/bin/cc    -o CMakeFiles/cmTC_a5239.dir/CheckSymbolExists.c.o   -c /home/banana/catkin_ws/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_a5239
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_a5239.dir/link.txt --verbose=1
/usr/bin/cc      -rdynamic CMakeFiles/cmTC_a5239.dir/CheckSymbolExists.c.o  -o cmTC_a5239 
CMakeFiles/cmTC_a5239.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x1b): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_a5239.dir/build.make:97: recipe for target 'cmTC_a5239' failed
make[1]: *** [cmTC_a5239] Error 1
make[1]: Leaving directory '/home/banana/catkin_ws/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_a5239/fast' failed
make: *** [cmTC_a5239/fast] Error 2

File /home/banana/catkin_ws/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>

int main(int argc, char** argv)
{
  (void)argv;
#ifndef pthread_create
  return ((int*)(&pthread_create))[argc];
#else
  (void)argc;
  return 0;
#endif
}

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

Run Build Command:"/usr/bin/make" "cmTC_a8ad0/fast"
/usr/bin/make -f CMakeFiles/cmTC_a8ad0.dir/build.make CMakeFiles/cmTC_a8ad0.dir/build
make[1]: Entering directory '/home/banana/catkin_ws/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_a8ad0.dir/CheckFunctionExists.c.o
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_a8ad0.dir/CheckFunctionExists.c.o   -c /usr/share/cmake-3.10/Modules/CheckFunctionExists.c
Linking C executable cmTC_a8ad0
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_a8ad0.dir/link.txt --verbose=1
/usr/bin/cc  -DCHECK_FUNCTION_EXISTS=pthread_create    -rdynamic CMakeFiles/cmTC_a8ad0.dir/CheckFunctionExists.c.o  -o cmTC_a8ad0 -lpthreads 
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_a8ad0.dir/build.make:97: recipe for target 'cmTC_a8ad0' failed
make[1]: *** [cmTC_a8ad0] Error 1
make[1]: Leaving directory '/home/banana/catkin_ws/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_a8ad0/fast' failed
make: *** [cmTC_a8ad0/fast] Error 2

and Yes, I tried changing ROSPYTHONVERSION to 3 to solve this, didnt work, reinstalled the ros-melodic-catkin and python-catkin-pkg to no avail...

Asked by KforBanana on 2020-02-23 22:48:05 UTC

Comments

Could you please remove the boilerplate comments from the CMakeLists.txt?

And please do not link to external sites for these small snippets. Just copy-paste it verbatim into your question. I've done that for you this time, but please keep it in mind.

Asked by gvdhoorn on 2020-02-24 03:49:12 UTC

Thank you very much, and noted!

Asked by KforBanana on 2020-02-24 07:48:43 UTC

Hi @KforBanana

First of all why do you have as a required component actionlib? since you are using only subscriber pusblisher paradigm you will not need actionlib and actionlib_msgs. Same whith boost ${Boost_INCLUDE_DIRS}

Also you do not need to add dependencies to generate_messages_cpp since you are not using a custom message.

Tested and working:

cmake_minimum_required(VERSION 2.8.3)
project(book_mr)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
)

catkin_package()

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

include_directories(
${catkin_INCLUDE_DIRS}
)  

add_executable(demo_topic_publisher src/demo_topic_publisher.cpp)
add_executable(demo_topic_subscriber src/demo_topic_subscriber.cpp)   

target_link_libraries(demo_topic_publisher ${catkin_LIBRARIES})
target_link_libraries(demo_topic_subscriber ${catkin_LIBRARIES})

Asked by Weasfas on 2020-02-24 11:26:35 UTC

There is any eror during catkin_make?

I don't see anything wrong. The only thing i think is strange is that you are not using any headers, but i think it should work like that.

Did you done properly the workspace creation? (http://wiki.ros.org/catkin/Tutorials/create_a_workspace)

Asked by Solrac3589 on 2020-02-24 11:27:42 UTC

@weasfas well it should work either way, It's there because the tutorial uses it later on @Solrac3589

ok, so i checked this and when I run

$ catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3

The Python3 works great but the CMake craps out. I'll update the post with the errors and error logs. Thanks for everyone's help. Hope to figure this out since I don't want to just purge everything and re install every time something like this happens it's a learning experience.

Asked by KforBanana on 2020-02-25 08:50:19 UTC

The error message is pretty clear I would think:

ImportError: "from catkin_pkg.package import parse_package" failed: No module named 'catkin_pkg' Make sure that you have installed "catkin_pkg", it is up to date and on the PYTHONPATH.

catkin_pkg needs to be installed for the Python 3 version you are using (how to install it depends on the platform you are on which you didn't mention).

Asked by Dirk Thomas on 2020-02-25 09:37:46 UTC

On Ubuntu you will need to install python3-catkin-pkg-modules. Just keep in mind that Python 3 is only officially supported by the to-be-released ROS distro Noetic (May 2020). All older ROS distros use Python 2.

Asked by Dirk Thomas on 2020-02-25 09:41:53 UTC

ok that, fixed it and no more errors on catkin_make, but also the original problem wasnt solved, which is nothing is being compiled in devel/lib, so whenever i try to run the package, nothing works

(base) banana@Science:~/catkin_ws$ catkin_make
Base path: /home/banana/catkin_ws
Source space: /home/banana/catkin_ws/src
Build space: /home/banana/catkin_ws/build
Devel space: /home/banana/catkin_ws/devel
Install space: /home/banana/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/banana/catkin_ws/build"
####
####
#### Running command: "make -j4 -l4" in "/home/banana/catkin_ws/build"
####

Asked by KforBanana on 2020-02-25 09:59:04 UTC

If catkin_make succeeds without an error there has to be some files under devel. Maybe share the recursive directory listing? Also did you source the setup file before trying to run any of the executables?

Asked by Dirk Thomas on 2020-02-25 10:48:04 UTC

@Dirk Thomas

(base) banana@Science:~/catkin_ws$ ls -R devel/
devel/:
cmake.lock  lib               local_setup.sh   setup.bash  _setup_util.py
env.sh      local_setup.bash  local_setup.zsh  setup.sh    setup.zsh

devel/lib:
(base) banana@Science:~/catkin_ws$ 

and yes i sourced both setup.bash in devel/ and in install/ then ran catkin_make and tried rosrun but still there was nothing to run under devel/lib

Asked by KforBanana on 2020-02-25 11:06:14 UTC

Answers