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

三世轮回 提交于 2021-01-28 16:50:21

问题


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

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;
}

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)

The error I get is on the line

output.compute.restype = POINTER(c_float)

and the traceback is below:

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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/33908356/attributeerror-python-undefined-symbol-when-accessing-c-function-from-pytho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!