Skip to content

Instantly share code, notes, and snippets.

@kdcro101
Forked from lucasea777/build.sh
Created December 18, 2020 19:31
Show Gist options
  • Select an option

  • Save kdcro101/9987b36f31fcd624e91e39ec7d5208aa to your computer and use it in GitHub Desktop.

Select an option

Save kdcro101/9987b36f31fcd624e91e39ec7d5208aa to your computer and use it in GitHub Desktop.

Revisions

  1. @lucasea777 lucasea777 revised this gist May 19, 2018. 3 changed files with 30 additions and 5 deletions.
    5 changes: 4 additions & 1 deletion build.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1,4 @@
    gcc -fpic --shared $(python-config --includes) greetmodule.c -o greetmodule.so
    gcc -fpic --shared $(python3-config --includes) greetmodule.c -o greet.abi3.so
    # can also use $(pkg-config --cflags python-3.5)
    # or
    # python3 setup.py install --record files.txt --user
    16 changes: 12 additions & 4 deletions greetmodule.c
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,16 @@ static PyMethodDef GreetMethods[] = {
    {NULL, NULL, 0, NULL}
    };

    PyMODINIT_FUNC
    initgreet(void)
    static struct PyModuleDef greet =
    {
    (void) Py_InitModule("greet", GreetMethods);
    }
    PyModuleDef_HEAD_INIT,
    "greet", /* name of module */
    "", /* module documentation, may be NULL */
    -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    GreetMethods
    };

    PyMODINIT_FUNC PyInit_greet(void)
    {
    return PyModule_Create(&greet);
    }
    14 changes: 14 additions & 0 deletions setup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    from setuptools import setup, Extension


    setup(
    name='greet',
    version='1.0',
    description='Python Package with Hello World C Extension',
    ext_modules=[
    Extension(
    'greet',
    sources=['greetmodule.c'],
    py_limited_api=True)
    ],
    )
  2. @Artanis Artanis created this gist Sep 17, 2011.
    1 change: 1 addition & 0 deletions build.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    gcc -fpic --shared $(python-config --includes) greetmodule.c -o greetmodule.so
    27 changes: 27 additions & 0 deletions greetmodule.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #include <Python.h>

    static PyObject *
    greet_name(PyObject *self, PyObject *args)
    {
    const char *name;

    if (!PyArg_ParseTuple(args, "s", &name))
    {
    return NULL;
    }

    printf("Hello %s!\n", name);

    Py_RETURN_NONE;
    }

    static PyMethodDef GreetMethods[] = {
    {"greet", greet_name, METH_VARARGS, "Greet an entity."},
    {NULL, NULL, 0, NULL}
    };

    PyMODINIT_FUNC
    initgreet(void)
    {
    (void) Py_InitModule("greet", GreetMethods);
    }
    7 changes: 7 additions & 0 deletions helloworld_in_c.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    import greet

    def main():
    greet.greet('World')

    if __name__ == "__main__":
    main()