[英]Python interpreter exits when loading DLL
I want to test wrapping a dll with ctypes . 我想测试用ctypes包装一个dll。 I've written the following test code and compiled it using Code::Blocks and Cygwin to a dll.
我编写了以下测试代码,并使用Code :: Blocks和Cygwin将其编译为dll。
#define DLL_EXPORT extern "C" __declspec(dllexport)
DLL_EXPORT int sum(int a, int b) {
return a + b;
}
Note: This is the whole code. 注意:这是整个代码。 Maybe there is something missing ?
也许有什么遗失?
Now, I copy the TestDll.dll
to my Desktop and start the Python interpreter. 现在,我将
TestDll.dll
复制到我的桌面并启动Python解释器。 But when I want to load it, the interpreter just exits ! 但是当我想加载它时,解释器就会退出!
C:\Users\niklas\Desktop>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> dll = ctypes.WinDLL('TestDll')
C:\Users\niklas\Desktop>
However, loading any other library works, or giving an error if the library could not be found. 但是,加载任何其他库工作,或者如果找不到库则会出错。
Can you tell me what I'm doing wrong here ? 你能告诉我这里我做错了什么吗?
Using Cygwin g++ 3.4.4 使用Cygwin g ++ 3.4.4
As a complete guess, I'd say that the Cygwin-built DLL is incompatible with the MSVC-built interpreter, either because of a ABI issue or just because you can't use two different libcs. 作为一个完整的猜测,我会说Cygwin构建的DLL与MSVC构建的解释器不兼容,要么是因为ABI问题,要么是因为你不能使用两个不同的libc。 Build the DLL with MinGW instead.
用MinGW构建DLL。
Check objdump -p TestDll.dll | grep dll
检查
objdump -p TestDll.dll | grep dll
objdump -p TestDll.dll | grep dll
to see if you've linked in "cygwin1.dll" and nm TestDll.dll | grep Dll
objdump -p TestDll.dll | grep dll
看看你是否链接了“cygwin1.dll”和nm TestDll.dll | grep Dll
nm TestDll.dll | grep Dll
to see if you have a DllMain
. nm TestDll.dll | grep Dll
看看你是否有DllMain
。 The following command should build the DLL correctly: 以下命令应正确构建DLL:
g++ testdll.c -mno-cygwin -shared -o TestDll.dll
Also, you need to use CDLL for the cdecl calling convention, not WinDLL: 此外,您需要将CDLL用于cdecl调用约定,而不是WinDLL:
>>> import ctypes
>>> dll = ctypes.CDLL('TestDll')
>>> dll.sum(4, 5)
9
Edit: I compiled with i686-w64-mingw32-g++.exe
(4.5.3) from the Cygwin repository, but I used to use the default Cygwin gcc without a problem, given the -mno-cygwin option. 编辑:我使用来自Cygwin存储库的
i686-w64-mingw32-g++.exe
(4.5.3)编译,但是我曾经使用默认的Cygwin gcc而没有问题,给定-mno-cygwin选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.