Cpp executeables only work with roslaunch, not rosrun, how can I fix this

asked 2019-09-06 11:12:01 -0600

user_handle_9918 gravatar image

updated 2019-09-06 11:57:20 -0600

I have had multiple workspaces/packages that I have been working with, and for a reason I can't explain, as of this morning, executeables built from cpp files only work with roslaunch and not rosrun. Specifically, if called with roslaunch, they can publish/subscribe/everything in realtime as intended, but when I call them with rosrun, even ones that worked before, they do not execute until I hit ctrl-C, where they run once and close. This problem is common across all workspaces and packages therein.

For an example:

grey_pub.cpp: (shortened to include the relevant parts only)

#include <ros/ros.h>
#include <cstdio>

int main(int argc, char** argv){
    printf("Script called");
    ros::init(argc,argv, "grey_pub");
    printf("Node initialized");
    //pub mainobject;
    //printf("Object initialized");

catkin_make for the workspace:

cmake_minimum_required(VERSION 2.8.3)


find_package(catkin REQUIRED COMPONENTS

find_package(OpenCV REQUIRED)


## Generate added messages and services with any dependencies listed here
   opencv_apps  # Or other packages containing msgs

CATKIN_DEPENDS message_runtime


add_executable (grey_pub scripts/grey_pub.cpp)

target_link_libraries (grey_pub ${OpenCV_LIBS} ${catkin_LIBRARIES})

When run, it just hangs endlessly, doing nothing. This was a new script, so although it may not work perfectly the first time, the first line in main is a printf command, and yet nothing happens until I ctrl-C

command line output:

rosrun odom_reporter grey_pub 

^CScript calledNode initializeddechering@dechering-OptiPlex-7440-AIO:~$

As you can see, the print statements only appear AFTER I have called ctrl-C. In addition, in omitted code there are publisher setups, yet when running rqt there is no topic, just rosout.

When running launch files on very similarly structured nodes, the topics set up properly, functions are performed, etc, but when I use rosrun to individually run those same nodes, rqt graph shows no subscriptions, publishers, etc. This seems like a very odd error, and I am at a loss as to why roslaunch/rosrun would handle these differently.

I have already relaunched the roscore/terminal window, re-sourced my devel/setup file, checked my main .bashrc file, restarted the computer even, and the problem persists. Any help anyone can give is much appreciated. I run kinetic on a linux 16.04 system. This is my first time posting on answers.ros.org, so any input on formatting this question is also appreciated.

edit retag flag offensive close merge delete



I edited your question to put the code snippets in preformatted text tags. This can be done with the 101010 button, or by pressing Ctrl-k.

jarvisschultz gravatar image jarvisschultz  ( 2019-09-06 11:58:19 -0600 )edit

I have had multiple workspaces/packages that I have been working with, and for a reason I can't explain, as of this morning, executeables built from cpp files only work with roslaunch and not rosrun.

I'd check whether you've installed any apt updates in the meantime (note: this can happen automatically, if you have that configured).

Check that you have no outstanding updates to any ROS packages (partial upgrades are not supported), then remove the build and devel folders from your Catkin workspaces (and the install one, if you have that). source the appropriate setup.bash (either the main /opt/ros/kinetic/setup.bash or the one from an underlay if you're overlaying) and rebuild each workspace.

Then source the workspace and try again.

gvdhoorn gravatar image gvdhoorn  ( 2019-09-07 04:30:41 -0600 )edit

Thanks @jarvisschultz, I will keep this in mind for future posts! @gvdhoorn, I don't see any recently installed packages or instances of any apt commands (used HISTTIMEFORMAT="%d/%m/%y %T " history | grep '[a]pt'), and I removed the build, devel, and install folders, resourced the /opt/... setup.bash file, and rebuilt, but nothing changed, sorry. I checked c++ compiling on my computer just in case and it works just fine. I appreciate your suggestion, though.

user_handle_9918 gravatar image user_handle_9918  ( 2019-09-07 12:07:44 -0600 )edit

I built a helloworld program and added features one by one as a test - adding \n to my printf commands seems to have fixed it. I don't understand why not having it would delay execution, but problem solved. Thanks to both of you for your time!

user_handle_9918 gravatar image user_handle_9918  ( 2019-09-07 12:47:54 -0600 )edit

adding \n to my printf commands seems to have fixed it

printf() doesn't add end-of-line characters for you, so you'd have to add those yourself always in any case.

What was probably happening was the output streams were not flushed to your terminal, causing nothing to appear. As soon as you terminate the process, all streams are flushed, causing whatever was buffered there to be written to the screen.

gvdhoorn gravatar image gvdhoorn  ( 2019-09-08 10:37:00 -0600 )edit