用Cygwin编译并从Python调用的C程序挂起 [英] C Program Compiled with Cygwin and Called from Python Hangs

查看:97
本文介绍了用Cygwin编译并从Python调用的C程序挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ctypes 从Python调用c程序。我下面有一个最低限度的(非工作)示例。

I'm trying to call a c program from Python using ctypes. I've got a minimum (non-)working example below.

这里是C我试图调用的程序。只是您的标准hello world程序。我在Windows上使用eclipse和cygwin gcc编译器进行编译,以生成 .dll 文件。

Here is the C program I'm trying to call into. Just your standard hello world program. I compile this, on windows, using eclipse and the cygwin gcc compiler to produce a .dll file.

main.h

#ifndef INC_MAIN_H_
#define INC_MAIN_H_

void helloWorld();

unsigned char buf[] = "Hello World!";

#endif /* INC_MAIN_H_ */

main.c

#include <stdio.h>
#include "main.h"

void helloWorld(){
    printf("\n%s\n\n", buf);
}



Python程序



然后我编写一个Python脚本来加载我的 .dll 并调用 helloWorld 函数。重要的是,我要同时覆盖我创建的 .dll cygwin1.dll

Python Program

Then I write a python script to load my .dll and call the helloWorld function. Importantly, I do pull over both the .dll I created and the cygwin1.dll.

helloWorld.py

from ctypes import CDLL
import os

def loadDLL(file):
    file = file.replace('\\','/')
    if not os.path.exists(file):
        raise FileNotFoundError(file)

    print('Opening DLL File:', file)
    dll = CDLL(file)

    return dll

if __name__ == '__main__':
    dll = loadDLL(FILE_TO_LOAD)
    dll.helloWorld()

当我运行该程序时, loadDLL 可以正常工作并加载DLL。但是,从c程序调用 helloWorld 函数会使它挂起。

When I go to run this program, loadDLL works just fine and loads the DLL. However, calling the helloWorld function from the c program causes it to hang.

奇怪的是,如果我替换了 printf 行带有一些无害的内容(例如, int x = 0 ),它执行得很好,但会打印出一个看似随机的数字。

Oddly enough, if I replace the printf line with something innocuous (e.g., int x = 0), it executes fine but prints out a seemingly random number.

有人可以指出我做错了什么吗?甚至是找出问题出在哪里的方法?

Can anyone point me to what I'm doing wrong? Or even a way to figure out what's going wrong?

Btw,我能够获得几乎相同的设置以在Linux系统上正常工作,所以我猜测是由于我已经设置了Windows环境,但我无法开始猜测它的真正含义。

Btw, I was able to get a nearly identical setup to work just fine on a linux system, so I'm guessing it's due to the windows environment that I've setup, but I couldn't begin to guess what it really is.

更新

我之所以写此答案并不是因为它不能解决问题的实质,而只能解决精神问题。

I'm not writing this as an answer because it doesn't solve the letter of the problem, but only the spirit.

Jean-Francois Fabre的建议我放弃了cygwin来使用mingw,现在一切正常。显然,cygwin的工作方式很奇怪。 Ahmed Masud 能够找到有关cygwin程序如何编译的有用链接用于外部库,但这似乎比仅使用mingw麻烦得多(更不用说我在尝试使用cygwin时遇到的其他问题)。

At Jean-Francois Fabre's suggestion I dropped cygwin for mingw and things now work as expected. Apparently cygwin works in strange ways. Ahmed Masud was able to find a useful link about how cygwin programs should be compiled if they're to be used for external libraries, but that seemed like much more trouble that just using mingw (not to mention the other problems I'd already encountered trying to use cygwin for this).

FWIW,此程序也必须在命令行上运行才能查看c程序的输出。在python的IDLE中运行并没有捕获c程序的 printf 输出。

FWIW, this program must also be run in on the command line in order to see the output of the c program. Running in python's IDLE did not capture the printf output from the c program.

推荐答案

问题是您正在从NOT cygwin python调用cywgin程序,并且期望值错误。

The problem is that you are calling a cywgin program from a NOT cygwin python and having the wrong expectation.

cygwin程序与普通的Windows程序具有不同的范式(类似于Posix)。

cygwin programs have different paradigma (Posix-like) than normal windows programs.

建议进行测试同时使用cygwin python和编译器,或同时使用两个windows。

It is recommended to test with both cygwin python and compiler, or both windows one.

这篇关于用Cygwin编译并从Python调用的C程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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