distutils error ?

garyr garyr at fidalgo.net
Sun Aug 16 21:25:53 CEST 2015


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.

The same error occurs with Python 2.6 and the current compiler.

The code and the setup.py file are shown below.

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);
}










More information about the Python-list mailing list