[Distutils] (no subject)

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Aug 18 21:51:06 CEST 2015


Should the function be called init_spam rather than initspam?


On Tue, 18 Aug 2015 19:19 garyr <garyr at fidalgo.net> wrote:

I posted this on comp.lang.python but received no replies.

I tried building the spammodule.c example described in the documentation
section "Extending Python with C or C++." As shown the code compiles OK but
generates a link error:

LINK : error LNK2001: unresolved external symbol init_spam
build\temp.win32-2.7\Release\_spam.lib : fatal error LNK1120: 1 unresolved
externals

I tried changing the name of the initialization function spam_system to
init_spam and removed the static declaration. This compiled and linked
without errors but generated a system error when _spam was imported.

I'm using Python 2.7.9. The same error occurs with Python 2.6.9.

The code and the setup.py file are shown below. What do I need to do to fix
this?

setup.py:
-----------------------------------------------------------------------------
from setuptools import setup, Extension

setup(name='spam',
    version='0.1',
    description='test module',
    ext_modules=[Extension('_spam', ['spammodule.c'],
                    include_dirs=[C:\Documents and
Settings\Owner\Miniconda\include],
                    )],
    )

sammodule.c
--------------------------------------------------
#include <python.h>
static PyObject *SpamError;

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    if (sts < 0) {
        PyErr_SetString(SpamError, "System command failed");
        return NULL;
    }
    return PyLong_FromLong(sts);
}

static PyMethodDef SpamMethods[] = {

    {"system",  spam_system, METH_VARARGS,
     "Execute a shell command."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initspam(void)
{
    PyObject *m;

    m = Py_InitModule("spam", SpamMethods);
    if (m == NULL)
        return;

    SpamError = PyErr_NewException("spam.error", NULL, NULL);
    Py_INCREF(SpamError);
    PyModule_AddObject(m, "error", SpamError);
}







_______________________________________________
Distutils-SIG maillist  -  Distutils-SIG at python.org
https://mail.python.org/mailman/listinfo/distutils-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/distutils-sig/attachments/20150818/b7531e6e/attachment.html>


More information about the Distutils-SIG mailing list