【问题标题】:creating a python binding of a function returning a pointer to a vector with boost创建一个函数的python绑定,返回一个指向带有boost的向量的指针
【发布时间】:2018-10-07 11:32:33
【问题描述】:

我是 python 本地人,最近开始研究 C++。为了加快某些事情的速度,我正在尝试为我用 C++ 编写的某些函数创建 python 绑定,并使用 boost 库。代码如下:

tools.h

#ifndef UEBUNG1_TOOLS_H
#define UEBUNG1_TOOLS_H

#include <string>
#include <vector>
#include <map>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

using namespace std;

vector<string> *product(string alphabet, int repeats);

vector<string> *product(vector<string> pools);

vector<string>* hammdist(string &pattern, int distance);

#endif //UEBUNG1_TOOLS_H

tools.cpp

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include "tools.h"
#include <vector>
#include <string>
#include <map>

using namespace std;

vector<string> *product(string alphabet, int repeats) {
    //initializing vector
    auto *results = new vector<string>();
    for(auto character : alphabet) {
        string tmpstr;
        tmpstr = character;
        results->push_back(tmpstr);
    }

    //cartesian product generation
    for(int i = 1; i < repeats; i++) {
        vector<string> tmp = *results;
        results->clear();

        //iterating over temporary list adding elements from pool to each contained string
        for(auto &it : tmp) {
            for(auto &character : alphabet) {
                results->push_back(it + character);
            }
        }
    }
    return results;
}

vector<string> product(vector<string> pools) {
    //initializing vector
    auto *results = new vector<string>();
    for(auto character : pools[0]) {
        string tmpstr;
        tmpstr = character;
        results->push_back(tmpstr);
    }

    //removing the first pool container
    pools.erase(pools.begin());

    //cartesian product generation
    for(const auto &pool : pools) {
        vector<string> tmp = *results;
        results->clear();

        //iterating over temporary list adding elements from pool to each contained string
        for(auto &it : tmp) {
            for(auto character : pool) {
                results->push_back(it + character);
            }
        }
    }
    return results;
}


vector<string>* hammdist(string &pattern, int distance) {
    map<char, string> possibles = {
            {'A', "CGT"},
            {'C', "AGT"},
            {'G', "ACT"},
            {'T', "ACG"}
    };
    auto *results = new vector<string>();
    vector<string> *masks = product("01", pattern.size());
    for(auto &mask : *masks) {
        auto *permute = new vector<string>();
        auto *tmp = new vector<string>();
        if(count(mask.begin(), mask.end(), '1') == distance) {
            for(int i = 0; i < pattern.size(); i++) {
                if(mask[i] != '1') {
                    string tmpstr;
                    tmpstr = pattern[i];
                    tmp->push_back(tmpstr);
                }
                else {
                    tmp->push_back(possibles[pattern[i]]);
                }
            }
            permute = product(*tmp);
            results->insert(results->end(), permute->begin(), permute->end());
        }
        delete permute;
        delete tmp;
    }
    return results;
}

BOOST_PYTHON_MODULE(tools) {
    def("hammdist", hammdist, return_value_policy<manage_new_object>());

    class_<vector<string>>("string_vector").def(vector_indexing_suite<vector<string>>());
}

BOOST_PYTHON_MODULE(tools) 调用及其所有包含的函数调用主要是使用这些源编译的:

但尝试使用调用 clang 和 cmake 的 CLion 编译此代码总是会出现链接器错误:

boost::python::objects::py_function_impl_base 的 vtable”,引用 来自:

boost::python::objects::py_function_impl_base::py_function_impl_base() 在 tools.cpp.o

注意:缺少 vtable 通常意味着第一个非内联虚拟 成员函数没有定义。

ld:未找到架构 x86_64 的符号

CMakeLists.txt 文件如下所示:

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(Uebung1)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(APPLE)
    set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)

find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})

set(PROJECT_SOURCE_DIR src/)
include_directories(${PROJECT_SOURCE_DIR})

set(BOOST_ROOT "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-py27_1")
set(BOOST_LIBRARYDIR "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-27_1/lib")

find_package(Boost COMPONENTS python REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

add_library(tools SHARED src/tools.cpp src/tools.h)
target_link_libraries(tools ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
set_target_properties(tools PROPERTIES PREFIX "")
add_executable(Uebung1 src/main.cpp src/tools.h src/tools.cpp)

main.cpp

#include <iostream>
#include "tools.h"
#include <vector>
#include <map>

using namespace std;

int main(int argc, char *argv[]) {

    string pattern = "AAAAAAAGGGGGG";

    auto *results = hammdist(pattern, 4);

    int i = 1;
    for(auto &res : *results) {
        cout << res << i << endl;
        i++;
    }

    return 0;
}

我真的不知道我做错了什么,我无法在互联网上找到任何我想要完成的工作示例。有人能告诉我问题是什么吗?

提前致谢!

【问题讨论】:

    标签: c++ containers c++17 boost-python


    【解决方案1】:

    我能够通过对代码进行以下更改来构建界面:

    tools.cpp

    /* 
       initializing function pointers in order to tell boost that we have 
       overloaded functions to expose to python
    */
    vector<string> *(*product1)(string, int) = &product;
    vector<string> *(*product2)(vector<string> pools) = &product;
    
    //include all functions that are used by the function to expose to the BOOST_PYTHON_MODULE call
    
    using namespace boost::python;
    
    BOOST_PYTHON_MODULE(tools) {
        //telling boost_python that we have overloaded functions which need to be called in the respective situations
        //return_value_policy<manage_new_objects> is required in order for the interface to handle the new invocation and the returned pointer
    
        def("product", product1, return_value_policy<manage_new_object>());
        def("product", product2, return_value_policy<manage_new_object>());
    
        def("hammdist", hammdist, return_value_policy<manage_new_object>());
    
        //vector_indexing_suite handles the wrapping of vector member functions
        //enables handling vector in a pythonic way when using in python
    
        class_<std::vector<string>>("string_vector")
            .def(vector_indexing_suite<std::vector<string>>());
    

    }

    仅在删除 add_executable 指令时重命名项目并将目标设置为所需的 .cpp 可实现无错误构建。

    CMakeLists.txt

    make_minimum_required(VERSION 3.12)
    project(tools)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    if(APPLE)
        set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
    endif(APPLE)
    
    find_package(PythonLibs 2.7 REQUIRED)
    include_directories(${PYTHON_INCLUDE_DIRS})
    
    set(PROJECT_SOURCE_DIR src/)
    include_directories(${PROJECT_SOURCE_DIR})
    
    set(BOOST_ROOT "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-py27_1")
    set(BOOST_LIBRARYDIR "/Users/DaniBook/miniconda3/pkgs/boost-1.66.0-27_1/lib")
    
    find_package(Boost COMPONENTS python REQUIRED)
    include_directories(${Boost_INCLUDE_DIRS})
    
    add_library(tools SHARED src/tools.cpp src/tools.h)
    target_link_libraries(tools ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
    set_target_properties(tools PROPERTIES PREFIX "")
    

    以上改动构建项目没有错误,但是创建的tools.so接口无法导入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2011-01-12
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多