Using WSL2 to cross-compile from Linux to Windows

johanneskauffmann at hotmail.com johanneskauffmann at hotmail.com
Fri Jul 1 00:30:42 CEST 2022


At the risk of repeating myself, I am sending this mail to the list a second
time. I am afraid that the first time, it has not properly been sent - I hadn't
noticed it on the online archives. It is my first time sending mail to an open
source mailing list, and I might have used the tools incorrect.

So, here goes...

---

A while back someone messaged about cross-compiling from Linux to Windows [1].
That person had a problem with the GMP script detecting the wrong build
compiler:

> checking build system compiler x86_64-w64-mingw32-gcc... yes

While that message was sent a while ago, it appears not to have been fixed, as
I'm having the same issue right now using Windows Subsystem for Linux 2 (WSL 2)
as host (build) system. My configure command:

./configure \
  --disable-static \
  --enable-shared \
  --build=x86_64-linux-gnu \
  --host=x86_64-w64-mingw32

While cross-compiling on a regular Linux VM for Windows using this command works,
it does not on WSL2. It picks the MinGW cross-compiler as CC_FOR_BUILD and then
fails with:

> checking build system compiler x86_64-w64-mingw32-gcc... yes
> checking for build system preprocessor... x86_64-w64-mingw32-gcc -E
> checking for build system executable suffix... configure: error: Cannot determine executable suffix

To make GMP use the system compiler and preprocessor as build system compiler,
one could of course pass CC_FOR_BUILD and CPP_FOR_BUILD. But that should not be
needed, since --buiild and --host are correctly supplied.

Below, I implemented the patch suggested in the referenced message, and it works
wonderfully.


diff -r 58bb9b017366 acinclude.m4
--- a/acinclude.m4      Sun May 29 18:08:56 2022 +0200
+++ b/acinclude.m4      Sun Jun 12 23:34:56 2022 +0200
@@ -3794,11 +3794,15 @@
    [CC_FOR_BUILD=$HOST_CC],
    [AC_MSG_ERROR([Specified HOST_CC doesn't seem to work])])
else
-  for i in "$CC" "$CC $CFLAGS $CPPFLAGS" cc gcc c89 c99; do
-    GMP_PROG_CC_FOR_BUILD_WORKS($i,
-      [CC_FOR_BUILD=$i
-       break])
-  done
+  if test $cross_compiling = no ; then
+    CC_FOR_BUILD="$CC"
+  else
+    for i in cc gcc c89 c99; do
+      GMP_PROG_CC_FOR_BUILD_WORKS($i,
+        [CC_FOR_BUILD=$i
+         break])
+    done
+  fi
   if test -z "$CC_FOR_BUILD"; then
    AC_MSG_ERROR([Cannot find a build system compiler])
   fi


[1] https://gmplib.org/list-archives/gmp-bugs/2020-January/004715.html



More information about the gmp-bugs mailing list