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

syntax error near unexpected token `(' `int main(int argc, char **argv)'

asked 2016-09-27 06:16:19 -0500

moustafaessar gravatar image

updated 2016-09-27 07:33:09 -0500

when i build the entire workspace it run well as i think but there are some warnings appears

also gives me this two lines when i run the node

 /home/moustafa/catkin_ws/src/hamo/src/demo_topic_publisher.cpp: line 5: syntax error near unexpected token `('
 /home/moustafa/catkin_ws/src/hamo/src/demo_topic_publisher.cpp: line 5: `int main(int argc, char **argv)'

build output:

Base path: /home/moustafa/catkin_ws
Source space: /home/moustafa/catkin_ws/src
Build space: /home/moustafa/catkin_ws/build
Devel space: /home/moustafa/catkin_ws/devel
Install space: /home/moustafa/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/moustafa/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/moustafa/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/moustafa/rover_ws/devel;/home/moustafa/catkin_ws/devel;/opt/ros/kinetic
-- This workspace overlays: /home/moustafa/rover_ws/devel;/home/moustafa/catkin_ws/devel;/opt/ros/kinetic
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/moustafa/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.1
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - hamo
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'hamo'
-- ==> add_subdirectory(hamo)
-- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy
-- Configuring done
CMake Warning (dev) at hamo/CMakeLists.txt:200 (add_dependencies):
  Policy CMP0046 is not set: Error on non-existent dependency in
  add_dependencies.  Run "cmake --help-policy CMP0046" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
  The dependency target "mastering_ros_demo_pkg_generate_messeges_cpp" of
  target "demo_topic_subscriber" does not exist.
This warning is for project developers.  Use -Wno-dev to suppress it.
CMake Warning (dev) at hamo/CMakeLists.txt:199 (add_dependencies):
  Policy CMP0046 is not set: Error on non-existent dependency in
  add_dependencies.  Run "cmake --help-policy CMP0046" for policy details.
  Use the cmake_policy command to set the policy and suppress this warning.
  The dependency target "mastering_ros_demo_pkg_generate_messeges_cpp" of
  target "demo_topic_publisher" does not exist.
This warning is for project developers.  Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /home/moustafa/catkin_ws/build
####
#### Running command: "make -j4 -l4" in "/home/moustafa/catkin_ws/build"
  ####
  [ 50%] Built target demo_topic_subscriber
  [100%] Built target demo_topic_publisher

Thanks

edit retag flag offensive close merge delete

Comments

What command are you using to "run the node"? It looks to me like maybe your file demo_topic_publisher.cpp is executable, and you are trying to execute the source code file instead of the executable built by catkin. What are the permissions of the cpp file?

jarvisschultz gravatar image jarvisschultz  ( 2016-09-27 07:52:21 -0500 )edit

The permissions: i checked allow executing file as program, I'm using rosrun hamo demo_topic_publisher.cpp to run executable file after i modified package.xml and CMakeLists.txt NOTE: i'm following Mastering_ROS_for_Robotics_Programming book commands

moustafaessar gravatar image moustafaessar  ( 2016-09-27 08:12:30 -0500 )edit

That is exactly as I suspected. I'm going to write an answer explaining in more detail.

jarvisschultz gravatar image jarvisschultz  ( 2016-09-27 08:45:11 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-09-27 09:03:37 -0500

updated 2016-09-28 08:33:02 -0500

This error is almost certainly caused by trying to "execute" a CPP file instead of executing the binary that is produced by catkin when you build your workspace. Remove the ".cpp" extension from your rosrun command, and be sure that the last argument to rosrun is the name of the executable that was specified in your CMakeLists.txt. For example, let's say your CMakeLists.txt has the following line:

add_executable(demo_topic_publisher src/demo_topic_publisher.cpp)

Then when you build the workspace, you produce an executable binary that is called demo_topic_publisher and to run that binary, the command would be

rosrun hamo demo_topic_publisher

(without the .cpp extension). If your CMakeLists.txt had a different name for the executable (the first argument), then you'd need a different command.

More details:

In Linux if the file permissions of a particular file are set such that you have execute permissions, then this script can be "executed". When you try and execute it a number of different things can happen depending on what type of file you are trying to execute. In your case, you are trying to execute a .cpp file that really shouldn't have executable permissions, nor is it intended to be executed. If you had a hashbang on the first line, the CPP file would be passed to an interpreter specified on the first line. If you don't have a hashbang, then the, likely, your system is falling back to using sh as the interpreter. Since your CPP code is not valid sh code you get errors.

If you correctly execute the binary that is compiled by catkin (as described above), then there are magic numbers at the top of the file that tells the system how it should handle the running of the program. This post on Unix&Linux Overflow has a nice detailed description of how programs get run on Linux.

edit flag offensive delete link more

Comments

It works! Thank you

moustafaessar gravatar image moustafaessar  ( 2016-09-27 09:25:01 -0500 )edit

it doesnt work for me

nullspace gravatar image nullspace  ( 2020-10-08 12:19:54 -0500 )edit

Question Tools

Stats

Asked: 2016-09-27 06:16:19 -0500

Seen: 5,866 times

Last updated: Sep 28 '16