Unable to generate .pyd file using swig

Question:

I am trying to implement the example given in swig. I am using windows 11 64-bit pc. It has 3 files

I file

/* File: example.i */
%module example

%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

int fact(int n);

C file

/* File: example.c */

#include "example.h"

int fact(int n) {
    if (n < 0){ /* This should probably return an error, but this is simpler */
        return 0;
    }
    if (n == 0) {
        return 1;
    }
    else {
        /* testing for overflow would be a good idea here */
        return n * fact(n-1);
    }
}

h file

/* File: example.h */

int fact(int n);

I wanted to do hand compiling. So tried the following commands on cmd

swig -python example.i

This created two file namely 1)example_wrap.c and 2)example.py

gcc -O2 -fPIC -c example.c

This created an example.o file

gcc -O2 -fPIC -c example_wrap.c -I"D:Program FilesPython310include"

I think -I is for including the directory for python header files. And this created example_wrap.o file.

gcc -shared example.o example_wrap.o -o _example.so

When I gave this commmand I got the following error.

d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x3): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x23): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x5d): undefined reference to `__imp_PyLong_AsLong'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x66): undefined reference to `__imp_PyErr_Occurred'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x82): undefined reference to `__imp_PyLong_FromLong'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x93): undefined reference to `__imp_PyExc_TypeError'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xa3): undefined reference to `__imp_PyErr_SetString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xb2): undefined reference to `__imp_PyErr_Clear'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xb9): undefined reference to `__imp_PyExc_OverflowError'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x181): undefined reference to `__imp_PyObject_GenericGetAttr'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1c3): undefined reference to `__imp_PyType_Ready'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x215): undefined reference to `__imp_PyArg_UnpackTuple'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x222): undefined reference to `__imp_PyBool_FromLong'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x235): undefined reference to `__imp_PyObject_IsTrue'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x240): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x263): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x287): undefined reference to `__imp_PyLong_FromVoidPtr'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x2a3): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x325): undefined reference to `__imp_PyObject_GenericGetAttr'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x34b): undefined reference to `__imp_PyType_Ready'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x3b4): undefined reference to `__imp_PyObject_Free'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x406): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x423): undefined reference to `__imp_PyExc_TypeError'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x433): undefined reference to `__imp_PyErr_SetString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x47b): undefined reference to `__imp_PyBool_FromLong'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x483): undefined reference to `__imp__Py_NotImplementedStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x62a): undefined reference to `__imp_PyUnicode_FromFormat'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x64c): undefined reference to `__imp_PyUnicode_FromFormat'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x69a): undefined reference to `__imp_PyUnicode_FromFormat'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x6b5): undefined reference to `__imp_PyUnicode_FromString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x6e0): undefined reference to `__imp_PyCFunction_Type'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x6eb): undefined reference to `__imp_PyType_IsSubtype'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x713): undefined reference to `__imp_PyCMethod_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x726): undefined reference to `__imp_PyInstanceMethod_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x781): undefined reference to `__imp_PyUnicode_FromFormat'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x7a1): undefined reference to `__imp_PyUnicode_Concat'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x7ab): undefined reference to `__imp_Py_DecRef'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x819): undefined reference to `__imp_PyCapsule_GetPointer'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x82d): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x8da): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x8e9): undefined reference to `__imp_PyUnicode_FromString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x93f): undefined reference to `__imp_PyObject_Free'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x99b): undefined reference to `__imp_PyErr_Fetch'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x9e1): undefined reference to `__imp_PyErr_Restore'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x9f4): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xa05): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xaa7): undefined reference to `__imp__PyObject_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xad5): undefined reference to `__imp_PyObject_CallFunctionObjArgs'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xaeb): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xafd): undefined reference to `__imp_PyErr_WriteUnraisable'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xb12): undefined reference to `__imp_PyErr_Restore'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xd20): undefined reference to `__imp_PyModule_Create2'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xd2e): undefined reference to `__imp_PyModule_GetDict'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xd83): undefined reference to `__imp_PyDict_SetItemString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xd91): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xddf): undefined reference to `__imp__PyObject_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xe7c): undefined reference to `__imp__PyObject_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xee5): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xefa): undefined reference to `__imp__PyObject_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0xf3a): undefined reference to `__imp__PyObject_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1120): undefined reference to `__imp__PyObject_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1166): undefined reference to `__imp_PyObject_Call'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1198): undefined reference to `__imp_PyObject_SetAttr'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x11b9): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x123e): undefined reference to `__imp_PyCapsule_Import'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x124b): undefined reference to `__imp_PyErr_Occurred'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x125a): undefined reference to `__imp_PyErr_Clear'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1272): undefined reference to `__imp_PyImport_AddModule'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x128f): undefined reference to `__imp_PyCapsule_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x12b4): undefined reference to `__imp_PyModule_AddObject'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x12c0): undefined reference to `__imp__Py_NoneStruct'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x12da): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x12f2): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1309): undefined reference to `__imp_PyUnicode_FromString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1324): undefined reference to `__imp_PyDict_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1369): undefined reference to `__imp_PyTuple_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1380): undefined reference to `__imp_PyDict_New'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x13d9): undefined reference to `__imp_PyObject_SetAttr'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1430): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1443): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1457): undefined reference to `__imp_PyUnicode_FromString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1474): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x1490): undefined reference to `__imp_PyUnicode_FromString'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x14b7): undefined reference to `__imp__Py_Dealloc'
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: example_wrap.o:example_wrap.c:(.text+0x14ca): undefined reference to `__imp_PyObject_Free'

1)Is this linker error?

2)In the D:Program FilesPython310libs there are two lib files python3.lib and python310.lib . What the difference between the two files and which one to use?

3)How to link .lib file in g++?

Asked By: Kishore Kumar S

||

Answers:

My doubt was how to link the .lib file in g++. I thought g++ only links .a file. Also got confused by @Mathieu’s answer here. A clear description for linking .lib is given here.

  1. Yes, it is a linker error.
  2. This is answered here.
  3. In the command add -L followed by location of the python3XX.lib file in python installation directory and add -l and library file name without .lib extension. Here is an example g++ -shared -L"D:Program FilesPython310libs" example.o example_wrap.o -o _example.pyd -lpython310
Answered By: Kishore Kumar S
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.