简体   繁体   中英

Boost.Python hello world tutorial: ImportError: ./hello.so: undefined symbol: _ZN3Num3setEf

I'm stuck at this point of the hello world tutorial of boost.Python , I added what the tutorial asks and after compile the shared library I got an infamous 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 >>> 

I compiled this code:

#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)
        ;
}

and this is my Jamroot file:

# 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>.
  ;

If I comment the struct Num and its definition for Boost.Python, everything works as expected.

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] ~>

Any help? :)

PD: I'm NOT a C++ experienced programmer! :(

The error says that your Num::set member function is not defined. If you change your Num struct to something like:

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

it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM