Compiling extension module (linker error)

MRAB python at mrabarnett.plus.com
Mon Oct 22 14:03:05 EDT 2012


On 2012-10-22 11:55, Paul Volkov wrote:
> I am trying to compile an extension module with C++ Builder 6 for Python 3.3.
> I converted python33.lib using coff2omf.exe and added this library
> into my project.
> I wonder why I get this error message while building:
>
> [Linker Error] Unresolved external '_PyModule_Create2TraceRefs'
> referenced from 'D:\WORK\FROMAGE\OUT\ROSE_UNIT.OBJ'
>
> My source file:
>
> //---------------------------------------------------------------------------
>
> #include <Python.h>
> #include <windows.h>
> #pragma argsused
>
It's a Python module with its own initialiser, so you don't need this:

> BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
> {
>          return 1;
> }
>
You don't need to export this function because it's listed in the
module's function table:

> static PyObject* __declspec(dllexport) testik(PyObject* self, PyObject* args)
> {

Returning NULL says that an exception was raised. You should set the
exception.

> return NULL;
> }
>
> static PyMethodDef FundRoseMethods[] = {
> {"testik", testik, METH_VARARGS, "perform a test"},
> {NULL, NULL, 0, NULL}
> };
>
> static struct PyModuleDef FundRoseModule = {
> PyModuleDef_HEAD_INIT,
> "FundRose",
> NULL,
> -1,
> FundRoseMethods
> };
>
> PyMODINIT_FUNC
> PyInit_FundRose(void)
> {
> return PyModule_Create(&FundRoseModule);
> }
>
> //---------------------------------------------------------------------------
>
By the way, the recommendation is for module names to be lowercase with
underscores, so "fund_rose" instead of "FundRose".

Try this code:

#include <Python.h>
#include <windows.h>
#pragma argsused

static PyObject* testik(PyObject* self, PyObject* args)
{
     PyErr_SetString(PyExc_RuntimeError, "testik exception");
     return NULL;
}

static PyMethodDef FundRoseMethods[] = {
     {"testik", testik, METH_VARARGS, "perform a test"},
     {NULL, NULL, 0, NULL}
};

static struct PyModuleDef FundRoseModule = {
     PyModuleDef_HEAD_INIT,
     "FundRose",
     NULL,
     -1,
     FundRoseMethods
};

PyMODINIT_FUNC PyInit_FundRose(void) {
     return PyModule_Create(&FundRoseModule);
}




More information about the Python-list mailing list