Boost Python and Cmake with Ubuntu and Python3.5

我是研究僧i 提交于 2019-12-22 09:17:16

问题


I'm having issues getting Boost Python to compile with Cmake. Everything works fine when compiled manually. Here is how I set everything up and ran it :

  • Install python 3.5 :

    sudo apt-get install python3-dev
    
  • Download the lastest version of Boost from http://www.boost.org/

  • Run bootstrap with correct flags :

    ./bootstrap.sh --with-python=python3.5
    
  • Compile Boost in directory :

    ./b2
    
  • Install Boost headers to /usr/local/include and Boost libs to /usr/lib/x86_64-linux-gnu

    sudo ./b2 install
    
  • Compile example with proper flags :

    g++ greet_binding.cpp -I/usr/include/python3.5m -I/usr/local/include -lboost_python-py35 -lpython3.5m -o greet.so -shared -fPIC
    
  • Start Python interpreter :

    $ python3
    
    import greet
    
    greet.say_greeting("Foo")
    
    Hello, Foo!
    

greet_binding.cpp :

#include <iostream>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>

using namespace std;
namespace bp = boost::python;

void say_greeting(const char* name)
{
        cout << "Hello, " << name << "!\n";
}

BOOST_PYTHON_MODULE(greet)
{
        bp::def("say_greeting", say_greeting);
}

However, when I try to set up a simple CmakeLists.txt file to compile it, I get an error when importing the compiled .so from Python.

CMakeLists.txt :

cmake_minimum_required( VERSION 2.8.3 )
project ( boost-python-3.5-examples )

set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-g -std=c++14 \
-I/usr/include/python3.5m \
-I/usr/local/include \
-lboost_python-py35 \
-lpython3.5m \
-fPIC" )

add_library( greet SHARED greet_binding.cpp )

This compiles fine, then I start the python interpreter in the build directory :

$ python3

import libgreet

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /home/james/Programming/boost-python-3.5-examples/build/libgreet.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

What am I doing wrong?

来源:https://stackoverflow.com/questions/40491536/boost-python-and-cmake-with-ubuntu-and-python3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!