Problem with swig tutorial on recent version of cygwin

Brad Bell <[email protected]> Fri, 19 Mar 2021 07:02:31 -0700
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
I am having some trouble running the swig tutorial on a recent version of cygwin.
    http://www.swig.org/tutorial.html
I have included the error message and my exact files below.

If you put these files in an empty directory, I called swig, and
run the python.sh bash script python.sh (under cygwin),
it should reproduce this error.
You will have to change the version of python in python.sh to the one on your system.

This error has already been reproduced on two systems; see
https://github.com/coin-or/CppAD/discussions/101#discussioncomment-503104


pytyhon.sh Output
-----------------
mkdir build
cp my_example.cpp my_example.i build
cd build
swig -python -c++ my_example.i
mv my_example_wrap.cxx my_example_wrap.cpp
g++ my_example.cpp my_example_wrap.cpp -c -fPIC -I /usr/include/python3.8
g++ -shared my_example.o my_example_wrap.o -o _my_example.so
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: 
my_example_wrap.o:my_example_wrap.cpp:(.text+0x5b): undefined reference to `_Py_Dealloc'
my_example_wrap.o:my_example_wrap.cpp:(.text+0x5b): relocation truncated to fit: R_X86_64_PC32 
against undefined symbol `_Py_Dealloc'
... snip ...
_wrap.o:my_example_wrap.cpp:(.rdata$.refptr.PyObject_GenericGetAttr[.refptr.PyObject_GenericGetAttr]+0x0): 
undefined reference to `PyObject_GenericGetAttr'
collect2: error: ld returned 1 exit status


---------------
/* File : my_example.c */

# include <ctime>
double My_variable = 3.0;

int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
}

int my_mod(int x, int y) {
     return (x%y);
}

char *get_time()
{
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
}

my_example.i:
-------------
/* my_example.i */
  %module my_example
  %{
  /* Put header files here or function declarations like below */
  extern double My_variable;
  extern int fact(int n);
  extern int my_mod(int x, int y);
  extern char *get_time();
  %}

  extern double My_variable;
  extern int fact(int n);
  extern int my_mod(int x, int y);
  extern char *get_time();

python.sh
---------
#! /bin/bash -e
# -----------------------------------------------------------------------------
# bash function that echos and executes a command
echo_eval() {
     echo $*
     eval $*
}
# ---------------------------------------------------------------------------
# create new build directory
if [ -e build ]
then
     echo_eval rm -r build
fi
echo_eval mkdir build
echo_eval cp my_example.* build
echo_eval cd build
# create my_example_wrap.cpp
echo_eval swig -python -c++ my_example.i
echo_eval mv my_example_wrap.cxx my_example_wrap.cpp
# build module that is loadable by python
echo_eval g++ my_example.cpp my_example_wrap.cpp \
     -c \
     -fPIC \
     -I /usr/include/python3.9
echo_eval g++ -shared my_example.o my_example_wrap.o \
     -o _my_example.so
# ---------------------------------------------------------------------------
# text module
cat << EOF > run.py
# load the modules
import sys
import my_example
# initialze exit status as OK
error_count = 0;
# check factor function
if my_example.fact(4) == 24 :
     print("my_example.fact: OK")
else :
     print("my_example.fact: Error")
     error_count = error_count + 1;
# check my_mod function
if my_example.my_mod(4,3) == 1 :
     print("my_example.my_mod: OK")
else :
     print("my_example.my_mod: Error")
     error_count = error_count + 1;
#
# return error_count
sys.exit(error_count)
EOF
# ---------------------------------------------------------------------------
if python run.py
then
     echo 'All tests passed'
else
     echo 'At least one test failed'
fi