I am trying to build Math::GSL on MSYS2 and got some problems with creating shared objects. Here is a simplified version of my problem, consider this C program sample.c:
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
static void SWIG_croak_null()
{
SV *err = get_sv("@", GV_ADD);
croak("%s", SvPV_nolen(err));
}
On Ubuntu I can create a shared object sample.so like this using a run.sh shell script:
#! /bin/bash
perl_dir=$(perl -MConfig -e'print $Config{archlib}')"/CORE"
set -x
gcc -I"$perl_dir" -c -fPIC -g -o sample.o sample.c
gcc -o sample.so sample.o -shared -fPIC
The output is:
+ gcc -I/home/hakon/perlbrew/perls/perl-5.30.0/lib/5.30.0/x86_64-linux/CORE -c -fPIC -g -o sample.o sample.c
+ gcc -o sample.so sample.o -shared -fPIC
But if I run the same program on Windows 10 with MSYS2, perl version 5.32.0, I get this output:
+ gcc -I/usr/lib/perl5/core_perl/CORE -c -fPIC -g -o sample.o sample.c
+ gcc -o sample.so sample.o -shared -fPIC
/usr/lib/gcc/x86_64-pc-msys/9.3.0/../../../../x86_64-pc-msys/bin/ld: sample.o: in function `SWIG_croak_null':
/home/hakon/perl/test/perl_get_sv/sample.c:7: undefined reference to `Perl_get_sv'
/home/hakon/perl/test/perl_get_sv/sample.c:7:(.text+0x2b): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Perl_get_sv'
/usr/lib/gcc/x86_64-pc-msys/9.3.0/../../../../x86_64-pc-msys/bin/ld: /home/hakon/perl/test/perl_get_sv/sample.c:8: undefined reference to `Perl_sv_2pv_flags'
/home/hakon/perl/test/perl_get_sv/sample.c:8:(.text+0x79): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Perl_sv_2pv_flags'
/usr/lib/gcc/x86_64-pc-msys/9.3.0/../../../../x86_64-pc-msys/bin/ld: /home/hakon/perl/test/perl_get_sv/sample.c:8: undefined reference to `Perl_croak_nocontext'
/home/hakon/perl/test/perl_get_sv/sample.c:8:(.text+0x88): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Perl_croak_nocontext'
/usr/lib/gcc/x86_64-pc-msys/9.3.0/../../../../x86_64-pc-msys/bin/ld: sample.o:sample.c:(.rdata$.refptr.PL_thr_key[.refptr.PL_thr_key]+0x0): undefined reference to `PL_thr_key'
collect2: error: ld returned 1 exit status
However, if I explicitly link with libperl.dll.a it works fine:
gcc -o sample.so sample.o -shared -L"$perl_dir" -lperl
Why is it not possible to create a shared object with undefined symbols in MSYS2?