ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Not recommended

To run any shell commands from a cpp program, you can use the std::system function. ie

#include <cstdlib>  // I think 'system' is in here?

int main(void) {
    std::system("rosrun [package] [node]");  // replace [package] and [node] with the relevant text

    return EXIT_SUCCESS;
}

This won't work unless the master node is running (i.e you have called roscore or roslaunch first)

Recommended

Normally when you want to start a node, you run all the nodes you'll need in the program using a launch file. Instead of generating new nodes during runtime, you could also create services. The Cpp API gives you basically all the interfaces you'll need.

I would highly recommend doing the tutorials. They take ages, but go a long way to understanding ROS.

Also, before posting a question next time, look for the answer first, and if these don't answer your question, follow the support guidelines. A more descriptive title might be "How to execute rosrun command from cpp", then the informal text can go in the text body.