如何在python中导入.so文件而不会导致%1不是有效的Win32应用程序错误?

发布于 2025-02-09 07:20:01 字数 570 浏览 0 评论 0原文

我一直在尝试将一些编译的C代码包含在我的python代码中,如下所示:

from ctypes import CDLL
import os

absolute_path = os.path.dirname(os.path.abspath(__file__))
# imports the c library
test_lib_path = absolute_path + '/theories/test.so'
test = CDLL(test_lib_path)

文件是通过编译文件test.c(其中包含代码)创建的。

gcc -o theories/test.so -shared -fPIC -O2 test.c

.SO 代码到Python程序中,以便我可以使用它,但是我会收到以下错误:

OSError: [WinError 193] %1 is not a valid Win32 application

据我所知,这是指我所编译的代码与我的操作系统不兼容,但是我不知道那在哪里会出错(如果有帮助,请使用vs 2022)。

I have been attempting to include some compiled c code into my python code as follows:

from ctypes import CDLL
import os

absolute_path = os.path.dirname(os.path.abspath(__file__))
# imports the c library
test_lib_path = absolute_path + '/theories/test.so'
test = CDLL(test_lib_path)

The .so file was created by compiling the file test.c (which contains no code) and executing the following command:

gcc -o theories/test.so -shared -fPIC -O2 test.c

I would expect this to load the compiled code into the python program so that I can use it, but I get the following error:

OSError: [WinError 193] %1 is not a valid Win32 application

As far as I can tell, this is meant to indicate that my compiled code is not compatible with my operating system, but I have no clue where that would go wrong (using VS 2022 if that helps).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2025-02-16 07:20:01

应该是您的Python版本为64位,.SO文件为32位。

首先检查Python版本。

  1. 将Python版本更改为与.so文件相同。
  2. 重新编译并生成与Python版本一致的.So文件。

您可以尝试

# 64bit
gcc -m64 
# 32bit 
gcc -m32

It should be that your python version is 64 bit and the .so file is 32 bit.

Check the python version first.

  1. Change the python version to be the same as the .so file.
  2. Recompile and generate a .so file consistent with the python version.

You can try

# 64bit
gcc -m64 
# 32bit 
gcc -m32
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文