Ask Your Question
0

I am not able to run a node. [closed]

asked 2019-12-01 11:04:44 -0600

ammaralam221 gravatar image

I am running ros kinetic on ubuntu 16.

While running a file hello_world.cpp, I get the following error

/media/ammar/New Volume/December/ros_directory/src/agitr_book/src/hello_world.cpp: line 2: syntax error near unexpected token `('
/media/ammar/New Volume/December/ros_directory/src/agitr_book/src/hello_world.cpp: line 2: `int main (int argc, char **argv)'

This is the code in hello_world.cpp

#include "ros/ros.h"
int main (int argc, char **argv)
{
    //intializes ros client library
    //last parameter is a string containing
    //deafult name of the node
    //can be overwridden by a launch file
    //call this once at the beginning of the program
    ros::init(argc, argv, "hello_ros");


    //establishes this program as a ros node
    //nh is the main mechanism that the program
    //will use to interact with the ROS system
    //creating this object registers the program
    //as a node at ros master
    ros::NodeHandle nh;


    //generates an informational message
    //send to console screen
    ROS_INFO_STREAM("HELLO_ROS");
}

I don't think, there is anything wrong the code as I am following it along with a book. I believe the problem is with the #include line but i don't know what?? If anyone could help me, it will be appreciated.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by ammaralam221
close date 2019-12-02 05:36:11.056769

Comments

Since your node is C++, you need to compile it before you run it.

ahendrix gravatar imageahendrix ( 2019-12-01 12:32:39 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-12-01 12:24:47 -0600

I ran your code and found no issues... (ubuntu 16.04 with ROS kinetic), however you are not publishing your CMakeLists.txt neither your package.xml files. Here are mine for reference:

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(hello_world)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

find_package(catkin REQUIRED
COMPONENTS
    roscpp
)

catkin_package(
CATKIN_DEPENDS
)

add_executable(hello_world ros/src/hello_world.cpp)
target_link_libraries(hello_world ${catkin_LIBRARIES})

package.xml

<?xml version="1.0"?>
<package>
<name>hello_world</name>
<version>1.0.0</version>
<description>
    hello world tutorial
</description>

<license>GPLv3</license>

<author email="olima@isr.tecnico.ulisboa.pt">Oscar Lima</author>
<maintainer email="olima@isr.tecnico.ulisboa.pt">Oscar Lima</maintainer>

<buildtool_depend>catkin</buildtool_depend>

<build_depend>roscpp</build_depend>

</package>

Compile your code

catkin build --this

Very important: source your workspace again after having compiled

source ~/my_catkin_ws/devel/setup.bash

Run the code

roscore
rosrun hello_world hello_world

You should now be able to see the desired output

[ INFO] [/hello_ros]: HELLO_ROS
edit flag offensive delete link more

Comments

Thanks for your help. I found what I was doing wrong, there was a problem with the rosrun command I was using

rosrun agitr_book hello_world.cpp

there shouldn't be a .cpp in this, the correct command is

rosrun agitr_book hello_world
ammaralam221 gravatar imageammaralam221 ( 2019-12-02 05:35:40 -0600 )edit

Question Tools

Stats

Asked: 2019-12-01 11:04:44 -0600

Seen: 22 times

Last updated: Dec 01