在Windows上使用ctypes在python中包装c ++函数:未找到函数 [英] Wrapping c++ functions in python with ctypes on windows : function not found

查看:262
本文介绍了在Windows上使用ctypes在python中包装c ++函数:未找到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一系列python脚本来计算各种脚本,这些脚本运行良好,但是其中一个运行速度非常慢,必须使用C ++来完成。
C ++代码已准备就绪,但是我需要找到一种从python调用C ++函数并获取返回值的方法。

I need to run a series of python scripts calculating various scripts, that are working fine, but one of them runs very slowly and has to be done in C++. The C++ code is ready, but I need to find a way to call the C++ function from the python and get the return value.

我找到了有关SWIG,但没有在Visual Studio的Windows上运行(由于其他限制,我必须在Windows的VS中运行)。我发现对于具有标准输入和输出值的非常简单的功能,ctypes更加容易。

I found information about SWIG, but didn't get it to work on Windows with Visual Studio (I have to do it in VS on Windows, because of other constraints). I found ctypes much easier for my very simple function with standart input and output values.

因此,我对ctypes进行了许多测试,尝试了所有可以在网上找到的示例,并且每次,都会发生什么事,我可以加载dll(使用Visual Studio 2012编译器构建),但是当我尝试在python中调用该函数时,它就会消失:

So I did many tests with ctypes, tried all the examples I could find online, and each and every time, what happens is that I can load the dll (built with visual studio 2012 compiler) but when I try calling the function in python, it just goes :

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    print maLib.plop()
  File "C:\Python27\lib\ctypes\__init__.py", line 378, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 383, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'plop' not found

python代码是(test.py):

The python code is (test.py) :

from ctypes import cdll
lib = cdll.LoadLibrary('test')
print lib.plop()

C ++代码是(test.cpp,编译为test.dll,Visual Studio设置为dll ):

The C++ code is (test.cpp, compiled as test.dll, Visual Studio set to build as dll) :

extern "C"
{
    int plop();
}

int plop() { return 4; }

如您所见,我试图使其尽可能地简单,以避免出现细节失败。我已经阅读了python ctypes帮助,以及有关如何使用ctypes的教程,尝试使用与它们完全相同的代码,但是由于我使用的是Visual Studio / windows,而其他大多数用户都在使用linux,因此我不得不稍作调整。
所有文件都在同一个文件夹中。

As you can see, I tried to make it as simple as possible, to avoid details making it fail. I have read the python ctypes help, and tutorials on how to use ctypes, trying exactly the same codes as them, but I had to adapt a bit because I am using Visual Studio / windows and most of the other users are using linux. All of the files are in the same folder.

我尝试了多种加载库的方式:LoadLibrary('./ name.dll'), WinDLL('name.dll'),为函数指定其他名称,无效的返回类型等...

I have tried multiple ways of loading the library : LoadLibrary('./name.dll'), WinDLL('name.dll'), giving the function a different name, void return type, etc...

您认为我应该使用SWIG吗?我的问题是明显的初学者错误吗?我是一名学生,并且对大多数正在使用的东西都是陌生的,但是即使我只是为了完成某项任务,也为此付出了很多努力。
使用SWIG时,我不得不用函数指针或引用包装,所以我认为这是问题所在,这使我想尝试一个更简单的解决方案。

Do you think I should use SWIG ? Is my problem an obvious beginner mistake ? I am a student, and new to most of what I'm using, but I put a lot of effort in this, even if I just need it for a particular single task. When using SWIG, I had to make a wrapper with function pointers, or references, so I thought this was the problem, which made me want to try a simpler solution.

我正在使用:Python 2.7.7,Visual Studio 2012,Windows 8.1

I am using : Python 2.7.7, Visual Studio 2012, Windows 8.1

在此先感谢

推荐答案

您的Python报告的错误非常清楚。

The error reported by your Python is very clear.


函数'plop'不是找到

function 'plop' not found

这意味着DLL不会导出该名称的函数。因此,您需要导出功能。使用.def文件,或使用 __ declspec(dllexport)

This means that the DLL does not export a function of that name. So, you need to export the function. Either with a .def file, or using __declspec(dllexport):

extern "C"
{
    __declspec(dllexport) int plop();
}

要检查您的DLL以调试此类问题,请使用 dumpbin 或Dependency Walker。

To inspect your DLL to debug issues like this, use dumpbin or Dependency Walker.

请注意,由于未指定调用约定,因此默认值为 __ cdecl 被使用。这意味着 cdll ctypes 方面是正确的。

Note that since you do not specify calling convention, the default of __cdecl is used. That means that cdll is correct on the ctypes side.

这篇关于在Windows上使用ctypes在python中包装c ++函数:未找到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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