使用 pthread.h 的 Python C 扩展会导致 Windows 上的段错误

问题描述

我为一个测试项目创建了一个 Python C 扩展。我已经为 Linux 平台构建了它,并且运行良好。我目前正在尝试为 Windows 构建它,但问题来了。 这个扩展引用了一个 .so 文件(在 Linux 上)或一个 .dll(在 Windows 上),它们已经过广泛的测试并且可以在 Windows 10(64 位)上运行。此 .dll 是从使用 pthread.h 的 C 代码构建的,该代码已静态链接以使 .dll 在 Windows 上运行。

我知道 pthread 在 Windows 上不存在,但是在我为测试而编写的 C 应用程序中使用时,引用的 .dll 工作正常。

当我用我的 setup.py 构建 python 扩展时,使用 VS2019 编译器,egg 扩展包被正确创建,它也使用 easy_install 在 Windows 上正确安装,但是当我在一个 python 应用程序,我在 pthread_mutex_lock调用 pthread 导致段错误

我也尝试使用 MingW 编译器(64 位)来构建它,但问题只是在应用程序代码中稍晚发生,而且 - 再次 - 它与 pthread 相关。

这是我的 setup.py,非常感谢您提供有关如何解决此问题的任何建议。为简洁起见,我省略了作者、电子邮件等元数据。

import sys

from setuptools import setup,Extension,find_packages
import ez_setup_v3 as ez_setup

ez_setup.use_setuptools()
compile_args = ['-DMS_WIN64']


setup(name='mymodule',version='1.0',description="Description",url="https://some-url",packages=find_packages('src'),package_dir={'': 'src'},ext_modules=[Extension('mymodule.cext',['src/mymodule/cext.c'],# this contains the .h of the referenced .dll/.so
                             library_dirs=['install_dir'],# this contains the .dll and the .lib of 'mylib' referenced by the C extension
                             include_dirs=['include_dir'],libraries=['mylib'],extra_compile_args=compile_args,)
                   ],install_requires=["argparse","pyYAML"],zip_safe=False,)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)