Skip to content

Instantly share code, notes, and snippets.

@cohnt
Last active June 11, 2018 19:23
Show Gist options
  • Save cohnt/632b1fef42fbc90b3e2bea1dbf31c7b4 to your computer and use it in GitHub Desktop.
Save cohnt/632b1fef42fbc90b3e2bea1dbf31c7b4 to your computer and use it in GitHub Desktop.

Correct include_dirs in setup.py to be whichever folder contains all of the ROS libraries. For example, [your_include_dir]/ros/ros.h should be the main ros library for C++.

Run python2 setup.py build_ext --inplace to build the program

Run python2 to enter a python environment, and type import node to start it off.

You will probably see an error message that looks something like ImportError: /file/path/to/node.so: undefined symbol: [some mangled symbol name].

You can use c++filt to try determine what it actually talking about by running echo "[mangled symbol name]" | c++filt.

For example, the problem I'm running into can be clarified with the command echo "_ZN3ros9PublisherD1Ev" | c++filt, which outputs ros::Publisher::~Publisher(), the destructor for a ros publisher. I've also had the missing symbol _Py_ZeroStruct, but c++filt can't parse it.

#include "/opt/ros/kinetic/include/ros/ros.h"
#include "/opt/ros/kinetic/include/ros/time.h"
#include "/opt/ros/kinetic/include/std_msgs/String.h"
#include <string>
ros::NodeHandle nh;
ros::Publisher pub;
int zero = 0;
void startROS() {
ros::init(zero, 0, "testNode");
pub = nh.advertise<std_msgs::String>("chatter", 1000);
}
bool rosOK() {
return ros::ok();
}
void rosSpin() {
ros::spinOnce();
}
void publish(std::string str) {
std_msgs::String msg;
msg.data = str;
pub.publish(msg);
}
# distutils: language=c++
from libcpp.string cimport string
from libcpp cimport bool
cdef extern from "file.h":
void startROS()
bool rosOK()
void rosSpin()
void publish(string)
startROS()
while rosOK():
publish("test")
rosSpin()
from distutils.core import setup
from distutils.core import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
ext = Extension(
"node",
["node.pyx"],
language="c++",
include_dirs=["/opt/ros/kinetic/include"]
# libraries=[],
# extra_link_args=[],
# cmdclass = {'build_ext': build_ext}
)
setup(
ext_modules = cythonize(
ext
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment