gpt4 book ai didi

python - 属性错误 : python: undefined symbol: when accessing C++ function from Python using ctypes

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:13 26 4
gpt4 key购买 nike

我想做的是从 Python 调用 C++ 方法以返回二维数组。 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

我对如何解决这个问题感到困惑。另外,有人可以检查一下,看看我是否正确发送了二维数组?因为它只发送一维数组,但我在发送二维数组时找不到任何东西。我很感激任何帮助。

最佳答案

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

关于python - 属性错误 : python: undefined symbol: when accessing C++ function from Python using ctypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com