I am not able to run a node.
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.
Asked by ammaralam221 on 2019-12-01 12:04:44 UTC
Answers
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
Asked by Oscar Lima on 2019-12-01 13:24:47 UTC
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
Asked by ammaralam221 on 2019-12-02 06:35:40 UTC
Comments
Since your node is C++, you need to compile it before you run it.
Asked by ahendrix on 2019-12-01 13:32:39 UTC