I want to create a ROS publisher node outside a catkin workspace. Can it be created?
2 Answers
Of course you can. Treat ROS like any other cpp library or python package.
In python you have to keep PYTHONPATH
environment variable pointing to ros packages in /opt/ros/kinetic/lib/python2.7/dist-packages
.
In cpp you have to tell compiler where to look for includes (/opt/ros/kinetic/include
), libraries (/opt/ros/kinetic/lib
) and which library to import. For the simplest application -lroscpp -lrostime -lrosconsole
should be sufficient. Ex:
g++ node.cpp -o node -I/opt/ros/kinetic/include -L/opt/ros/kinetic/lib -lroscpp -lrostime -lrosconsole
Remember that you still need ros environment variables like ROS_MASTER_URI
.
However, I don't know if there is an easy way to generate custom ros messages without using catkin_make
and cmake files.

- 716
- 6
- 4
Yes, in Python, you can write ROS nodes outside your catkin workspace.
Launch your roscore
and then run the Python script in a new terminal normally like python filename
and it runs as it would if you had placed in inside your catkin workspace and built and sourced it.
I have successfully created subscriber and publisher nodes and run them on an actual TurtleBot2 without the nodes being inside the catkin workspace.
I don't think the method I have described works for C++. It only works for Python. In C++ you will have to link the libraries while compiling. So, check that. For example, we do g++ filename.cpp -lm, where -lm links the math library to be used in the filename.cpp, so you might need to check how to do that. On the other hand, it is easier to just add the file in your catkin workspace or just shift to Python.
You have to include ros/ros.h and std_msgs/message_name.h where message_name is replaced by whatever message you're using in code. You might find these files somewhere or you can get the source code of the files online by searching.

- 2,092
- 1
- 20
- 32
-
Thank you @akshayk07 , for the response. I have created a publisher node in *cpp*. when I am compiling as like any other *cpp* file compilation in terminal window. I am experiencing errors like **"undefined reference to *ros::init()* , undefined reference to *ros::ok() *"**. while we are compiling in terminal, just adding the path for ros is okay or something else we have to do along with addition of */opt/ros/kinetic/include* path. Sorry for the format of my writing here in stack overflow – rama May 15 '18 at 03:48
-
I don't think the method I have described works for C++. It only works for Python. In C++ you will have to link the libraries while compiling. So, check that. For example, we do `g++ filename.cpp -lm`, where `-lm` links the math library to be used in the `filename.cpp`, so you might need to check how to do that. On the other hand, it is easier to just add the file in your catkin workspace or just shift to Python. – akshayk07 May 15 '18 at 13:12
-
Surly it won't work. I think we have to specify the dedicated library directories and link the library. But I'm not sure which libraries I have to specify. if I have to create a package in catkin for the node I am trying to create , in ***add_package*** section of **CMakeLists.txt**, I am adding **std_msgs** , **roscpp**. for this which libraries is being used , I could not figure out. . If I can get those than I would have tried, execution of .cpp file outside of catkin_ws. if you have some suggestion, would you like to share? Thank you – rama May 16 '18 at 05:43
-
You have to include `ros/ros.h` and `std_msgs/message_name.h` where message_name is replaced by whatever message you're using in code. You might find these files somewhere or you can get the source code of the files online by searching. – akshayk07 May 16 '18 at 15:05
-
I have added this information to the answer. If this answer is satisfactory, click the green tick mark to accept the answer. If you have other questions, post them as separate questions and not in the comments as that would help to keep this site organized. Thanks. – akshayk07 May 16 '18 at 15:07