Ask Your Question

Embedding python in C++ [closed]

asked 2015-06-01 06:27:47 -0500

bulgrozer gravatar image

updated 2015-06-01 06:36:08 -0500


I'm trying to embedded a python function in a c++ node. I've read the documentation on docpython for Python 3.4 and I've tried a little example from the doc :

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <python3.4/Python.h>

int main(int argc, char *argv[])
   ros::init(argc, argv, "fault_detector");
   PyRun_SimpleString("from time import time,ctime\n"
                 "print('Today is', ctime(time()))\n");
return 0;

and i get this error :

CMakeFiles/ft_node.dir/src/fault_detector.cpp.o: In function `main': 
fault_detector.cpp:(.text+0x63): undefined reference to `Py_Initialize'
fault_detector.cpp:(.text+0x72): undefined reference to `PyRun_SimpleStringFlags'
fault_detector.cpp:(.text+0x77): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status 
make[2]: *** [/home/mamy/exo_ws/devel/lib/test/ft_node] Error 1
make[1]: *** [test/CMakeFiles/ft_node.dir/all] Error 2

I've done some research to find my problems but I can't find anything pertinent. It could be a problem of version as I read and i don't know how to change that. If anyone has an answer.

Thank you in advance for your answer

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by bulgrozer
close date 2015-08-25 03:54:53.680480

2 Answers

Sort by ยป oldest newest most voted

answered 2015-06-01 07:12:19 -0500

timster gravatar image

Looks like a linker error... Have you added the somehing like the following to your CMakeLists.txt?

find_package (PythonLibs 3.4 EXACT)

target_link_libraries(fault_detector ${catkin_LIBRARIES} ${PYTHON_LIBRARIES})
edit flag offensive delete link more

answered 2015-06-01 07:40:15 -0500

bulgrozer gravatar image

I didn't put the "find_package (PythonLibs 3.4 EXACT)" but when it's in the CMake, it return me this error :

CMake Error at /opt/ros/indigo/share/catkin/cmake/catkinConfig.cmake:75 (find_package): Could not find a package configuration file provided by "PythonLibs" with any of the following names:


Add the installation prefix of "PythonLibs" to CMAKE_PREFIX_PATH or set "PythonLibs_DIR" to a directory containing one of the above files. If "PythonLibs" provides a separate development package or SDK, be sure it has been installed.

i begin in development of complex systems and I must admit that I've a lack of knowledge in target link etc...

edit flag offensive delete link more


You should not add an answer when you are really providing an update. Instead, you should edit your original question. Have you installed the necessary Python 3.4 development packages? Installing python3-devshould be enough

jarvisschultz gravatar image jarvisschultz  ( 2015-06-01 15:05:56 -0500 )edit

Ok sorry, I wanted to answer in a comment but my answer was too long so I did not think to edit my original post. Yes I've install python3.4-dev package and python3-dev but not a change

bulgrozer gravatar image bulgrozer  ( 2015-06-02 02:40:39 -0500 )edit

In a simple test package , the answer from @timster works perfectly for me on 14.04. Could you be running into some CMake issues as described here:

jarvisschultz gravatar image jarvisschultz  ( 2015-06-02 08:22:41 -0500 )edit

Question Tools



Asked: 2015-06-01 06:27:47 -0500

Seen: 2,165 times

Last updated: Jun 01 '15