0

I'm building a C library for python using "python.h", I succefully made one for adding two numbers, build-it and install-it. But now I want to add those numbers using inline asm code in C. But when I build the C file with my setup.py, it gives me an error. Does anyone made something like this and probabily have a solutions? Or do you have another ideas for making it.

Here is my 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);
}

And here is my 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]
)

Here is the error I got when running python setup.py build it says that 'asm' is not defined.

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
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 5
    Apparently you tried to use gcc inline asm syntax with visual studio. That won't work. PS: you know it's pointless to use asm for adding numbers, right? – Jester May 10 '19 at 13:41
  • I know hahaha, but it's a project that some teacher give me. I have to make a Python script that uses asm to make the basic operations, like adding two numbers... That's the problem – Sasukector May 10 '19 at 13:48
  • 1
    So compile it with a compiler that supports the syntax, e.g. GCC or clang. – Peter Cordes May 10 '19 at 14:02
  • I compile it for python, using that setup.py file. I know how to compile .c files normally, but I'm requiring to compile it as a Python library – Sasukector May 10 '19 at 14:06
  • 1
    If you're building Python extensions on Windows, you normally need to use MSVC so that the extension depends on the same C runtime library as Python itself. So that would mean you would need to use syntax that MSVC supports; you can't use syntax that is only supported by gcc or clang. – Daniel Pryden May 10 '19 at 14:31
  • 1
    It's worse than that: MSVC for x86-64 [doesn't support inline assembly _at all_](https://stackoverflow.com/questions/1295452/why-does-msvc-not-support-inline-assembly-for-amd64-and-itanium-targets?noredirect=1). I suspect OP's instructor expected them to use a Mac or Linux build environment, and gaining access to one of those will probably be easier than splitting out the assembly language into a separate function in an `.ASM` file, which is the only alternative I know of if you must use MSVC on Windows. – zwol May 10 '19 at 14:37
  • I finished it!! In a couple of minutes I'll update the post – Sasukector May 10 '19 at 15:00
  • @zwol: so target x86 instead and pretend you didn't notice :) – Seva Alekseyev May 10 '19 at 16:36
  • Please don't put answers as edits to your question. You should create an answer. I have made a community wiki answer with your solution and removed your last edit from your question. – Michael Petch May 10 '19 at 20:30
  • Sorry, this is my first post in stackoverflow – Sasukector May 10 '19 at 21:09

1 Answers1

0

This answer came from the OP as an edit to the question:


I found a solution and I compile and install the extension correctly.

As @peter-cordes said, I needed to change the compiler that uses distutils when executing python setup.py build. For that I made this things to correctly solve the problem (it'll be like a guide for someone that wants something similar on Windows):

1.- Check if you have MinGW installed, if so, add bin folder to path. You can test running gcc if you did it correctly.

2.- Create a file named distutils.cfg on your-Python-Version-folder\Lib\distutils (my python version is 3.6 so the folder was in AppData\Local\Programs\Python\Python36). The file content is:

[build]
compiler=mingw32

3.- Edit the file cygwinccompiler.py that is in the same level as the last file. There you have to add this lines of code for supporting another msc_ver:

(prev line code) :
return ['msvcr100']

(Add this lines) :

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']

(next line) :

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

4.- Download vcruntime140.dll and copy it to your-Python-Version-folder\libs

That's all!!

Now you can run python setup.py build and python setup.py install and add the library in C with inline asm() to your site-package folder in Python.

Now you only have to do in a Python project like example.py:

import hectorASM

n = hectorASM.ADD(3, 4)

print(n)

Hope this can help some other people.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198