python - _ZSt28__throw_bad_array_new_lengthv 尝试在编译共享对象后在 Python 中导入 C++ 代码

标签 python c++ arrays g++ shared-libraries

在尝试导入 Python 库时遇到了令人沮丧的问题,该库本身调用了我用 C++ 编写并编译成 .so 的一些代码

C++代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>

std::vector<std::string> bigram(std::string initial_str) {
    int len = initial_str.size();
    std::vector<std::string> tokens;
    for (int i = 0; i < len-1; i += 1){
        tokens.push_back(initial_str.substr(i, 2));
    }
    return tokens;
}

std::vector<std::string> vsunion(std::vector<std::string> s1, std::vector<std::string> s2) {
    std::vector<std::string> union_str(s1);
    union_str.insert(union_str.end(), s2.begin(), s2.end());
    std::sort(union_str.begin(), union_str.end());
    union_str.erase(std::unique(union_str.begin(), union_str.end()), union_str.end());
    return union_str;
}

std::vector<int> ufreq(std::vector<std::string> u, std::vector<std::string> s) {
    int len = u.size();
    std::vector<int> vfreq;
    for (int i = 0; i < len; i += 1){
        int freq = std::count(s.begin(), s.end(), u[i]);
        vfreq.push_back(freq);
    }
    return vfreq;
}

float similarity(std::vector<int> f1, std::vector<int> f2) {
    float num = std::inner_product(f1.begin(), f1.end(), f2.begin(), 0.0);
    float den1 = std::inner_product(f1.begin(), f1.end(), f1.begin(), 0.0);
    float den2 = std::inner_product(f2.begin(), f2.end(), f2.begin(), 0.0);
    float similarity = num / std::sqrt(den1 * den2);
    return similarity;
}

float similarity(std::string string1, std::string string2) {
    std::vector<std::string> new_str = bigram(string1);
    std::vector<std::string> new_str2 = bigram(string2);
    std::vector<std::string> union_str = vsunion(new_str, new_str2);
    std::vector<int> freq1 = ufreq(union_str, new_str);
    std::vector<int> freq2 = ufreq(union_str, new_str2);
    float score = similarity(freq1, freq2);
    return score;
}

extern "C" {
    float gram(std::string str1, std::string str2)
    {
        return similarity(str1, str2);
    }
}

我使用以下方式编译:

g++ gram.cpp -shared -o gram.so

最后,我尝试导入以下脚本,该脚本在标题“_ZSt28__ throw_bad_array_new_lengthv 无法位于动态链接库中”中抛出错误:

import ctypes
import sys
import os 

dir_path = os.path.dirname(os.path.realpath(__file__))
handle = ctypes.CDLL(dir_path + "/gram.so")

handle.My_Function.argtypes = [ctypes.c_wchar_p] 
  
def gram(string1, string2):
    return handle.gram(string1, string2)

知道我哪里出了问题吗?我可以编译一个测试用例而不是“extern”位:

int main() {
    float score = similarity("John Smith", "Johnny John Smith");
    std::cout << score << " ";
    std::cin.get();
}

并作为exe运行,看起来没有问题;似乎出了点问题

handle = ctypes.CDLL(dir_path + "/gram.so")

gram 库中的阶段。

最佳答案

finally I'm trying to import the below script that's throwing the error in the title "_ZSt28__throw_bad_array_new_lengthv could not be located in the dynamic link library":

这个错误的意思是:libstdc++.so的版本在运行时可用没有 std::throw_bad_array_new_length符号,您的 gram.so正在使用。

虽然可以使用g++ gram.cpp -shared -o gram.so -fPIC -static-libstdc++ -static-libgcc来解决这个问题,你真的不应该——它迟早会在你脸上爆炸。

相反,您应该使用 Python已链接到 libstdc++.so.6 适合您的系统

关于python - _ZSt28__throw_bad_array_new_lengthv 尝试在编译共享对象后在 Python 中导入 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71331756/

相关文章:

python - Python抛出IndexError:列表索引超出范围,使用CSV

c++ - 保留一个大数字的一部分

c++ - 指向数据成员的指针数组

arrays - 在二维数组中查找邻居

arrays - 两个数组的元素相加

Python 日期时间减法包括 0 到小时部分

python - 为类属性生成随机数

Python:如何不等待线程完成继续?

c++ - 针对预定义种子列表进行字符串测试的最快 C++ 算法(不区分大小写)

c++ - 从 cuda 3D 内存复制到线性内存 : copied data is not where I expected