AttributeError:python:未定义符号:使用ctypes从Python访问C ++函数时 [英] AttributeError: python: undefined symbol: when accessing C++ function from Python using ctypes

查看:88
本文介绍了AttributeError:python:未定义符号:使用ctypes从Python访问C ++函数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从Python调用C ++方法以返回2D数组.Python文件名是:BaxterArm1.py,而C ++文件名是:baxIK.cpp.下面是我编译c ++程序的方式:

What I am trying to do is call a C++ method from Python to return a 2D array. The Python filename is: BaxterArm1.py and the C++ filename is: baxIK.cpp. Below is how I compiled my c++ program:

g++ -c -fPIC baxIK.cpp -o baxIK.o
g++ -shared -Wl,-soname,baxIK.so -o baxIK.so baxIK.o

以下是C ++程序的相关部分:

Below is the relevant part of the C++ program:

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

}

extern "C" float** compute(float x, float y, float z, float q1, float q2, float q3, float q4)
{
    unsigned int num_of_solutions = (int)solutions.GetNumSolutions();
    std::vector<IKREAL_TYPE> solvalues(num_of_joints);
    float** sols;
    sols = new float*[(int)num_of_solutions];

    for(std::size_t i = 0; i < num_of_solutions; ++i) {
        sols[i] = new float[7];
        for( std::size_t j = 0; j < solvalues.size(); ++j)
        {
            printf("%.15f, ", solvalues[j]);
            sols[i][j] = solvalues[j];
        }
        printf("\n");
    }   
    return sols;
}

这个想法是返回一个Nx7数组供Python接收.Python代码如下:

The idea is to return a Nx7 array for Python to receive. The Python code is as follows:

def sendArm(xGoal, yGoal, zGoal, right, lj):
print "Goals %f %f %f" % (xGoal, yGoal, zGoal)
output = ctypes.CDLL(os.path.dirname('baxIK.so'))
output.compute.restype = POINTER(c_float)
output.compute.argtypes = [c_float, c_float, c_float, c_float, c_float, c_float, c_float]   
res = output.compute(xGoal, yGoal, zGoal, 0.707, 0, 0.7, 0)
print(res)

我得到的错误就在线上

output.compute.restype = POINTER(c_float)

,回溯如下:

File "/home/eadom/ros_ws/src/baxter_examples/scripts/BaxterArm1.py", line 60, in sendArm
    output.compute.restype = POINTER(c_float)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: python: undefined symbol: compute

我对如何解决此问题感到困惑.另外,有人可以对此进行检查,看看我是否正确发送了2D阵列?因为它只是发送一个1D数组,但是我在发送2D数组时找不到任何东西.感谢您的帮助.

I'm confused as to how I'd fix this. Also, could somebody do a check on this and see if I'm sending the 2D array properly? As it is it's only sending a 1D array but I couldn't find anything on sending a 2D array. I'm grateful for any help.

推荐答案

终于成功了!问题是(os.path.dirname('baxIK.so')返回一个空字符串,因此我将其放在完整目录中.我通过用llapack编译g ++解决了前面提到的另一个问题现在我要解决最后一个细节,希望这对其他人有帮助.

Finally got it working! Problem is that (os.path.dirname('baxIK.so')was returning an empty string, so I put in the full directory. I fixed the other issue I mentioned earlier by compiling g++ with llapack and now I'm fixing up one last detail. I hope this helps somebody else.

这篇关于AttributeError:python:未定义符号:使用ctypes从Python访问C ++函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆