繁体   English   中英

AttributeError:python:未定义符号:使用ctypes从Python访问C ++函数时

[英]AttributeError: python: undefined symbol: when accessing C++ function from Python using ctypes

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

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

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

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代码如下:

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数组时找不到任何东西。 感谢您的帮助。

终于成功了! 问题是(os.path.dirname('baxIK.so')返回一个空字符串,所以我把它放在完整目录中。我用llapack编译g ++解决了我前面提到的另一个问题,现在我正在修复最后一个细节,希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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