0

I would like to use GMP on windows, and I need to build static programs. (which means that, if anyone can provide another solution to compile win32 x86_64 programs using libgmp, I will also appreciate it.)

Now I have msys2 installed, and in it I have executed

pacman -S base-devel gcc 

and then I downloaded gmp-6.2.0.tar.lz from gmplib.org and extracted it to somewhere, like

C:\Users\xxx\gmp-6.2.0

Then I, using msys2 mingw64, entered this directory and executed

./configure
make
make check
make install

The first two commands seems to be fine, the output seems to be right. However, make check didn't provide me with whether the make is right or not. At the end of its output it says,

make[5]: Leaving directory '/c/Users/Fred/gmp-6.2.0/tests/cxx'
make[4]: Leaving directory '/c/Users/Fred/gmp-6.2.0/tests/cxx'
make[3]: Leaving directory '/c/Users/Fred/gmp-6.2.0/tests/cxx'
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/tests'
Making check in mpn
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/mpn'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/mpn'
Making check in mpz
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/mpz'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/mpz'
Making check in mpq
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/mpq'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/mpq'
Making check in mpf
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/mpf'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/mpf'
Making check in printf
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/printf'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/printf'
Making check in scanf
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/scanf'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/scanf'
Making check in rand
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/rand'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/rand'
Making check in cxx
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/cxx'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/cxx'
Making check in demos
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/demos'
Making check in calc
make[3]: Entering directory '/c/Users/Fred/gmp-6.2.0/demos/calc'
make  check-am
make[4]: Entering directory '/c/Users/Fred/gmp-6.2.0/demos/calc'
make[4]: Nothing to be done for 'check-am'.
make[4]: Leaving directory '/c/Users/Fred/gmp-6.2.0/demos/calc'
make[3]: Leaving directory '/c/Users/Fred/gmp-6.2.0/demos/calc'
Making check in expr
make[3]: Entering directory '/c/Users/Fred/gmp-6.2.0/demos/expr'
make[3]: Nothing to be done for 'check'.
make[3]: Leaving directory '/c/Users/Fred/gmp-6.2.0/demos/expr'
make[3]: Entering directory '/c/Users/Fred/gmp-6.2.0/demos'
make[3]: Nothing to be done for 'check-am'.
make[3]: Leaving directory '/c/Users/Fred/gmp-6.2.0/demos'
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/demos'
Making check in tune
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/tune'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/tune'
Making check in doc
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0/doc'
make[2]: Nothing to be done for 'check'.
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0/doc'
make[2]: Entering directory '/c/Users/Fred/gmp-6.2.0'
make[2]: Leaving directory '/c/Users/Fred/gmp-6.2.0'
make[1]: Leaving directory '/c/Users/Fred/gmp-6.2.0'

And make install ended up in similar messages. At last when I tried to compile programs, it says,

/usr/lib/gcc/x86_64-pc-msys/9.3.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lgmp

As a matter of fact, I am not a professional programmer so I'm not quite sure about how to install GMP for mingw. I would be deeply grateful if anyone can tell me how to do this from scratch.

Yutsing
  • 126
  • 9
  • MSYS2 has a GMP package, called `mingw-w64-x86_64-gmp`. – HolyBlackCat Aug 02 '20 at 14:36
  • Thanks for your comment. However, after I installed this package, I can compile dynamic programs now but recently pacman only install the dynamic libs in default. How can I install static libs so that I can compile with -static? – Yutsing Aug 03 '20 at 00:38
  • What do you mean by *"recently pacman only install the dynamic libs"*? The package contains both static and dynamic libraries. To make the linker prefer the static one, build with `-static`. – HolyBlackCat Aug 03 '20 at 08:51
  • If I use the -static para, I will get this error message: /usr/lib/gcc/x86_64-pc-msys/9.3.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lgmp collect2: error: ld returned 1 exit status – Yutsing Aug 03 '20 at 10:37
  • 1
    I just noticed you're using the wrong compiler. Install `mingw-w64-x86_64-gcc` instead of `gcc`, and make sure it's in the `PATH` by running MSYS2 in "mingw64" mode (use `mingw64.exe`, if done correctly it'll show `MINGW64` in the terminal (in magenta), instead of `MSYS` you're seeing currently). – HolyBlackCat Aug 03 '20 at 10:51
  • 2
    Long story short, MSYS2 has 3 sets of compilers and libraries in its packages, targeting: 1. 32-bit windows (packages prefixed with `mingw-w64-i686-`), 2. 64-bit windows (packages prefixed with `mingw-w64-x86_64-`), and "MSYS" (unprefixed packages; essentially for 64-bit windows, *with* cygwin (their own fork of it, more specifically)). You want to be using 2 (or 1, if you need 32-bit). Stay away from 3, it's only used to port Linux utilities to Windows specifically for development purposes, e.g. for `bash` and `make` packages. Your compiler and libraries should come from prefixed packages. – HolyBlackCat Aug 03 '20 at 10:56
  • Well, things are slightly different, but what you said helped me solve the problem. I will describe how to do this in the answer shortly. Thanks for your help! – Yutsing Aug 03 '20 at 11:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219124/discussion-between-sffred-and-holyblackcat). – Yutsing Aug 03 '20 at 12:14

1 Answers1

0

With the help of @HolyBlackCat, I solved this problem and successfully compiled my programs. Here is the process:

Install msys2

  1. Download the executable file from msys2.org and install msys2. You will get three shortcuts in your opening menu: MSYS MinGW 32-bit, MSYS2 MinGW 64-bit and MSYS2 MSYS. The difference between them is the PATH. MSYS's path is /usr/bin, while MinGW 64-bit's path includes /mingw64/bin.
  2. Install necessary packages. Run the following commands:
$ pacman -Syu
$ pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-gmp

Compile your programs

  1. Open MSYS2 MinGW 64-bit
  2. Use x86_64-w64-mingw32-gcc rather than gcc to compile. (If you didn't install gcc, you can simply use gcc to compile.) If you are using other shell, use the full path /mingw64/bin/x86_64-w64-mingw32-gcc

Other info

If you needs to use autoconf tools, you can install them with

$ pacman -S base-devel
Yutsing
  • 126
  • 9
  • *"Use x86_64-w64-mingw32-gcc rather than gcc to compile"* This shouldn't be necessary as both are copies of the exact same executable. Also `/mingw64/bin` should be the first directory in the `PATH`, so a wrong `gcc` can't "shadow" this `gcc`. – HolyBlackCat Aug 03 '20 at 12:22
  • Also, a minor detail. If `pacman -Syu` asked you to restart the terminal (or did it automatically) you need to run the same command again to finish the update. – HolyBlackCat Aug 03 '20 at 12:23