Cross-Compiling to mingw32 terminates with ""memusage.c:72: undefined reference to `GetProcessMemoryInfo@12'""
Imported issue: Initially reported by Georg-Johann Lay in https://gforge.inria.fr/tracker/?group_id=135&aid=21836
This link fail occurs when configure + make with --host=i686-w64-mingw32, more specifically I used
../../source/ecm-7.0.4/configure --with-gmp=HOME/install/gmp-mingw32 --with-gnu-ld --prefix=
HOME/install/ecm-gmp --host=i686-w64-mingw32 --enable-static --disable-shared
The offending command is
/bin/bash ./libtool --tag=CC --mode=link i686-w64-mingw32-gcc -std=gnu99 -g -W -Wall -Wundef -m32 -O2 -pedantic -fomit-frame-pointer -mtune=pentiumpro -march=pentiumpro -msse2 -o ecm.exe ecm-auxi.o ecm-b1_ainc.o ecm-candi.o ecm-eval.o ecm-main.o ecm-resume.o ecm-getprime_r.o aprtcle/ecm-mpz_aprcl.o ecm-memusage.o libecm.la .../install/gmp-mingw32/lib/libgmp.a -lm -lm -lm -lm -lm
libtool: link: i686-w64-mingw32-gcc -std=gnu99 -g -W -Wall -Wundef -m32 -O2 -pedantic -fomit-frame-pointer -mtune=pentiumpro -march=pentiumpro -msse2 -o ecm.exe ecm-auxi.o ecm-b1_ainc.o ecm-candi.o ecm-eval.o ecm-main.o ecm-resume.o ecm-getprime_r.o aprtcle/ecm-mpz_aprcl.o ecm-memusage.o ./.libs/libecm.a .../install/gmp-mingw32/lib/libgmp.a
ecm-memusage.o: In function `PeakMemusage':
.../build/ecm-gmp/../../source/ecm-7.0.4/memusage.c:72: undefined reference to `GetProcessMemoryInfo@12'
collect2: error: ld returned 1 exit status
Makefile:962: recipe for target 'ecm.exe' failed
make[2]: *** [ecm.exe] Error 1
GetProcessMemoryInfo is provided by libpsapi.a, and running
/bin/bash ./libtool --tag=CC --mode=link i686-w64-mingw32-gcc -std=gnu99 -g -W -Wall -Wundef -m32 -O2 -pedantic -fomit-frame-pointer -mtune=pentiumpro -march=pentiumpro -msse2 -o ecm.exe ecm-auxi.o ecm-b1_ainc.o ecm-candi.o ecm-eval.o ecm-main.o ecm-resume.o ecm-getprime_r.o aprtcle/ecm-mpz_aprcl.o ecm-memusage.o libecm.la .../install/gmp-mingw32/lib/libgmp.a -lm -lpsapi
by hand terminates without error. Thus the following addendum for configure.ac might do the trick:
AC_MSG_CHECKING([whether we can link without -lpsapi])
AC_SEARCH_LIBS won't work here, so do it by hand...
AC_DEFUN([ECM_PSAPI_PROGRAM],
[AC_LANG_PROGRAM([[
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
int testfunc ()
{
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (int) (info.PeakWorkingSetSize >> 10);
}
#endif
]])])
AC_LINK_IFELSE([ECM_PSAPI_PROGRAM],
[AC_MSG_RESULT(yes)],
[AC_MSG_RESULT(no)
try_link_psapi=yes
])
if test x""$try_link_psapi"" = ""xyes""; then
AC_MSG_CHECKING([whether we can link with -lpsapi])
LIBS=""-lpsapi $LIBS""
AC_LINK_IFELSE([ECM_PSAPI_PROGRAM],
[AC_MSG_RESULT(yes)],
[AC_MSG_RESULT(no)
AC_MSG_ERROR([memusage.c using unresolved GetProcessMemoryInfo])
])
fi
I added this in the case $host in --cygwin* | --mingw* ... section and it logs
checking whether we can link without -lpsapi... no
checking whether we can link with -lpsapi... yes
make succeeds without error.