samedi 2 février 2019

Errors in exposing C++ boost::variant to Python

I am writing a Python module in which I need to access C++ library. I am using Boost-Python to expose the functionality implemented in the C++ library to Python.

I have to expose boost::variant < bool, unsigned int, std::string, boost::asio::ip::address, std::vector from C++ to Python module.

I have written a code which is implementing the functionality but I have got a number of errors. I will appreciate if someone help me in resolving the issues. I have followed the approach mentioned in answer given on the following link.

https://blind.guru/boost_python-and-boost_variant.html

variantEx.h
---------

    #include <boost/variant.hpp>
    #include <vector>
    #include <boost/asio.hpp> // for ip::address

    struct intType {
    int i; 
    int get()
    {
    std::cout<<"Enter integer number: "<<std::endl; 
    std::cin>>i; 
    }
    void printget()
    {
    std::cout<<"Entered integer number is: "<< i;
    }
    };

    struct stringType { 
    std::string s; 
    std::string  get()
    {
    std::cout<<"Enter your name: "; 
    getline(std::cin,s);
    return std::string("Your name is: ").append(s);
    }
    void printget(std::string n)
    {
    std::cout<<"Entered name is: "<< n;
    }
    };
    struct boolType 
    {
    bool b;
    bool get()
    {
    std::cout<<"Enter boolean value : "<<std::endl; 
    std::cin>> b;
    }
    bool printget()
    {
    std::cout<<"Entered boolean value is: "<<b; 
    }
    };
    struct uintType {unsigned int  ui;};
    struct vecType {std::vector<unsigned int> vui; };
    struct ipType {boost::asio::ip::address ipa;};
    typedef boost::variant<boolType, stringType, intType, uintType, vecType, ipType> variant;
    typedef std::vector<variant> vector;

    variant make_variant() { return variant(); }
    vector make_vector() { return vector{boolType(), stringType(), intType(),  uintType(), vecType(), ipType()}; }
    bool operator==(intType const &lhs, intType const &rhs) { return lhs.i == rhs.i; }
    bool operator==(stringType const &lhs, stringType const &rhs) { return !std::strcmp(lhs.s.c_str(), rhs.s.c_str()); }
    bool operator==(boolType const &lhs, boolType const &rhs) { return lhs.b == rhs.b; }
    bool operator==(uintType const &lhs, uintType const &rhs) { return lhs.ui == rhs.ui; }
    bool operator==(vecType const &lhs, vecType const &rhs) { return lhs.vui == rhs.vui; }
    bool operator==(ipType const &lhs, ipType const &rhs) { return lhs.ipa == rhs.ipa; }

I created a shared library using following command:

When I tried to import pyintf.so from Python, I got the following error:

    >>> import pyintf
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: ./pyintf.so: undefined symbol: _ZN5boost6system15system_categoryEv

Aucun commentaire:

Enregistrer un commentaire