python - Boost.Python hello world教程: ImportError: ./hello.so: undefined symbol :_ZN3Num3setEf

标签 python boost boost-python

我被困在this point of the hello world tutorial of boost.Python ,我添加了教程要求的内容,编译共享库后我得到了一个臭名昭著的 ImportError:

1 >>> import hello
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-f2ff2800b3ef> in <module>()
----> 1 import hello

ImportError: ./hello.so: undefined symbol: _ZN3Num3setEf

2 >>> 

我编译了这段代码:

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

using namespace boost::python;

struct World
{
    World(std::string msg): msg(msg)
    {

    } // constructor anadido...
    void set(std::string msg)
    {
        this->msg = msg;
    }

    std::string greet()
    {
        return msg;
    }
    std::string msg;

};

struct Var
{
    Var(std::string name): name(name), value() {}
    std::string const name;
    float value;
};


struct Num
{
    Num();
    float get() const;
    void set(float value);
};


BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World", init<std::string>())
        .def("greet", &World::greet)
        .def("set", &World::set)
        ;

    class_<Var>("Var", init<std::string>())
        .def_readonly("name", &Var::name)
        .def_readwrite("value", &Var::value)
        ;

    class_<Num>("Num")
        .add_property("rovalue", &Num::get)
        .add_property("value", &Num::get, &Num::set)
        ;
}

这是我的 Jamroot 文件:

# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

import python ;

if ! [ python.configured ]
{
    ECHO "notice: no Python configured in user-config.jam" ;
    ECHO "notice: will use default configuration" ;
    using python ;
}

# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.
use-project boost
  : . ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.
project
  : requirements <library>/usr/lib/libboost_python.so ;

# Declare the three extension modules.  You can specify multiple
# source files after the colon separated by spaces.
python-extension hello : hello.cpp ;

# Put the extension and Boost.Python DLL in the current directory, so
# that running script by hand works.
install convenient_copy
  : hello
  : <install-dependencies>on <install-type>SHARED_LIB <install-type>PYTHON_EXTENSION
    <location>.
  ;

如果我注释 struct Num 及其对 Boost.Python 的定义,一切都会按预期工作。

1 >>> import hello

2 >>> hello.
hello.Var    hello.World  hello.cpp    hello.so     

2 >>> x = hello.Var("prueba")

3 >>> x.name
3 <<< 'prueba'

4 >>> x.value = 900

5 >>> x
5 <<< <hello.Var at 0x13245d0>

6 >>> x.value
6 <<< 900.0

7 >>> type(x.value)
7 <<< float

8 >>> exit
jorge [~/coders/desarrollo/practicas/boost] ~>

有什么帮助吗? :)

P.D:我不是一个有 C++ 经验的程序员! :(

最佳答案

该错误表明您的 Num::set 成员函数未定义。如果您将 Num 结构更改为:

struct Num
{
    Num():internal_value(){}
    float get() const
    {
        return internal_value;
    }
    void set(float value)
    {
        internal_value=value;
    }
private:
    float internal_value;
};

它应该可以工作。

关于python - Boost.Python hello world教程: ImportError: ./hello.so: undefined symbol :_ZN3Num3setEf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908864/

相关文章:

python - Urllib2 & BeautifulSoup : Nice couple but too slow - urllib3 & threads?

python - 通过列表理解展平列表列表

c++ - 具有在编译时定义的不同类型的 vector

c++ - 使用 boost::thread::interrupt() 时,您*需要*捕获 thread_interrupted 异常吗?

c++ - boost Python 可移植性问题

python - 拆分后为什么要在这里加上\t和\x08?如何避免这种转换?

python - 使用 html5lib 验证 HTML 片段

c++ - 如何将一个项目添加到两个队列并保证它存在于两个队列中或不存在(多线程)

python - 通过嵌入式 Python 调用 C++ 代码

python - boost python raw_function 方法