python - 如何使用asm代码为Python构建C库?

标签 python c assembly nasm setup.py

我正在使用“python.h”为python构建一个C库,我成功地制作了一个用于添加两个数字的库,即build-it和install-it。但现在我想使用 C 中的内联 asm 代码添加这些数字。但是当我使用 setup.py 构建 C 文件时,它给了我一个错误。有没有人做过类似的事情并且可能有解决方案?或者您还有其他制作想法吗?

这是我的hectorASMmodule.c

#include <Python.h>
#include <stdio.h>

static PyObject *hectorASM_ADD(PyObject *self, PyObject *args) {
    int num1, num2;
    if (!PyArg_ParseTuple(args, "ii", &num1, &num2)) {
        return NULL;
    }
    // int res = num1 + num2;
    int res = 0;
    __asm__("add %%ebx, %%eax;" : "=a"(res) : "a"(num1), "b"(num2));
    return Py_BuildValue("i", res);
}

static PyMethodDef hectorASM_methods[] = {
    // "PythonName"     C-function Name     argument presentation       description
    {"ADD", hectorASM_ADD, METH_VARARGS, "Add two integers"},
    {NULL, NULL, 0, NULL}   /* Sentinel */

};

static PyModuleDef hectorASM_module = {
    PyModuleDef_HEAD_INIT,
    "hectorASM",                       
    "My own ASM functions for python",
    0,
    hectorASM_methods                 
};

PyMODINIT_FUNC PyInit_hectorASM() {
    return PyModule_Create(&hectorASM_module);
}

这是我的setup.py

from distutils.core import setup, Extension, DEBUG

module1 = Extension(
    'hectorASM',
    sources = ['hectorASMmodule.c']
)

setup (
    name = 'hectorASM',
    version = '1.0',
    description = 'My own ASM functions for python',
    author = 'hectorrdz98',
    url = 'http://sasukector.com',
    ext_modules = [module1]
)

这是我在运行 python setup.py build 时遇到的错误,它表示“asm”未定义。

hectorASMmodule.c
hectorASMmodule.c(11): warning C4013: '__asm__' sin definir; se supone que extern devuelve como resultado int
hectorASMmodule.c(11): error C2143: error de sintaxis: falta ')' delante de ':'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

最佳答案

这个答案来自OP,作为对问题的编辑:

<小时/>

我找到了一个解决方案,并且正确编译并安装了扩展。

正如@peter-cordes所说,我需要在执行python setup.py build时更改使用distutils的编译器。为此,我做了这个事情来正确解决问题(这就像为那些在 Windows 上想要类似东西的人提供的指南):

1.- 检查是否安装了 MinGW,如果安装了,请将 bin 文件夹添加到路径中。如果操作正确,您可以测试运行 gcc。

2.- 在 Python 版本文件夹\Lib\distutils 上创建一个名为 distutils.cfg 的文件(我的 python 版本是 3.6,因此该文件夹位于 AppData\Local\Programs\Python\Python36)。文件内容为:

[build]
compiler=mingw32

3.- 编辑与最后一个文件同级的文件cygwinccompiler.py。您必须在此处添加以下代码行以支持另一个 msc_ver:

(上一行代码):
返回['msvcr100']

(添加此行):

elif msc_ver == '1700':
   # Visual Studio 2012 / Visual C++ 11.0
   return ['msvcr110']
elif msc_ver == '1800':
   # Visual Studio 2013 / Visual C++ 12.0
   return ['msvcr120']
elif msc_ver == '1900':
   # Visual Studio 2015 / Visual C++ 14.0
   return ['vcruntime140']

(下一行):

else:   
   raise ValueError("Unknown MS Compiler version %s " % msc_ver)

4.- 下载 vcruntime140.dll 并将其复制到您的 Python 版本文件夹\libs

就这些了!!

现在您可以运行 python setup.py buildpython setup.py install 并使用内联 asm() 将 C 语言库添加到您的站点包文件夹中Python。

现在您只需在 example.py 这样的 Python 项目中执行以下操作:

import hectorASM

n = hectorASM.ADD(3, 4)

print(n)

希望这可以帮助其他人。

关于python - 如何使用asm代码为Python构建C库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56078875/

相关文章:

python - 在 tkinter 中添加文本时防止调整按钮大小

python - CSV 模块可以解析带有多字符分隔符的文件吗?

c - 如何找到正在运行的进程的平台

使用 ARM 编译器时的 C 死代码检测

c++ - 将 C 和 C++ 嵌入微芯片

c - Y86 1 步停止异常 HLT

python - 从计算中排除列,然后再次检索它

python - 在 Sublime Text 3 中访问快速面板用户输入

c - 如何防止用户在c中输入0或负数

assembly - %pcrel_hi 和 %pcrel_lo 实际上做了什么?