0

I write a C++ program to invoke a function coded with python and everything goes well, the program has been successfully compiled. However, when I use this program, it posts the error like this:

gzserver: symbol lookup error: /home/shin/gazebo_plugin_tutorial/build/libhello_world.so: undefined symbol: Py_Initialize

I used to guess it is because I do not link related library and I try to modify the CMake file, but there is no effect. I have searched for a long time, but similar cases always happen in the stage of compiling, those solution seems do not work in my case, and I really do not know how to deal with this case.

Here is my simple C++ code:

public: void OnNewFrame(const unsigned char *_image,
        unsigned int _width, unsigned int _height, unsigned int _depth,
        const std::string &_format)
    {
        const unsigned char * str = this->parentSensor->GetCamera()->ImageData();
        const char* str1 = static_cast<const char*>( static_cast<void*>( const_cast<unsigned char*>(str)));
        Py_Initialize();
        PyObject *pyMod = PyImport_ImportModule("/home/shin/gazebo_plugin_tutorial/int");
        PyObject *pyFunc = PyObject_GetAttrString(pyMod,"reimg");
        PyObject *pyParams = PyTuple_New(1);
        PyTuple_SetItem(pyParams,0,PyString_FromString(str1));
        PyObject_CallObject(pyFunc,pyParams);
    }

Could someone give me a hand?

shinshiner
  • 41
  • 6
  • Are you linking in libpython? You mention cmake... have a look at the docs for [FindPythonLibs](https://cmake.org/cmake/help/v3.0/module/FindPythonLibs.html), and then make sure `PYTHONLIBS_FOUND` is true, and use `target_link_libraries` to link against `PYTHON_LIBRARIES` – Steve Lorimer May 25 '17 at 15:33
  • I have tried similar way, and my CMake file is like this, I think everything should goes well, but this error still exists:`cmake_minimum_required(VERSION 2.8 FATAL_ERROR) find_package(gazebo REQUIRED) include_directories(${GAZEBO_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS}) link_directories(${GAZEBO_LIBRARY_DIRS} ${PYTHON_LIBRARIES_DIRS} ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GAZEBO_CXX_FLAGS}") add_library(hello_world SHARED hello_world.cc) target_link_libraries(hello_world ${GAZEBO_LIBRARIES} CameraPlugin) target_link_libraries(hello_world ${PYTHON_LIBRARIES} CameraPlugin)` – shinshiner May 25 '17 at 15:49
  • You called `find_package` for `gazebo`, but not for `python` – Steve Lorimer May 25 '17 at 15:59
  • After I add related code, the CMake file seems to have linked this lib, but the error still exists, this is part of the CMake messages, is there anything wrong ? :`-- Boost version: 1.54.0 -- Found the following Boost libraries: -- thread -- signals -- system -- filesystem -- program_options -- regex -- iostreams -- date_time -- Boost version: 1.54.0 ...... -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found version "2.7.6") -- Configuring done -- Generating done -- Build files have been written to: /home/shin/gazebo_plugin_tutorial/build` – shinshiner May 26 '17 at 01:30

0 Answers0