-2

Is it possible to build GMP for MSVC on Windows?

I need fully static solution (static library), without any DLL dependencies. So that my final EXE doesn't depend on any external (non-system) DLLs.

I'm alright if building GMP will need Cygwin or MSYS, as far as it can be used later in MSVC without any problems. But as far as I know at least Cygwin builds always depend on extra DLLs like cygwin1.dll which is not affordable for me, fully static-library solution is needed.

I'm aware there exists MPIR library that is more Windows friendly. But right now I need specifically GMP solution if possible.

Of course would be great if all optimizations and assembly is used when building for Windows. But if assembly is not possible then at least non-assembly (generic) variant of GMP is needed.

Of course I need 64-bit version.

If someone can post all steps needed to produce such static library for MSVC usage? Or maybe link some web-site that has such instructions?

Arty
  • 14,883
  • 6
  • 36
  • 69
  • Doesn't msys2 provide prebuilt static libraries? – Marc Glisse Sep 18 '21 at 07:28
  • @MarcGlisse Both MSYS/Cygwin have prebuilt libraries. But main idea of my question was how to have such build of GMP that it can be used directly in MSVC. Do you know if MSYS's version of library can be used directly in MSVC? At least it has `.a` extension, it means should be repacked to `.lib` for MSVC use. But is it enough to repack to use it without problems in MSVC? Also maybe I'm wrong ang MSYS/Cygwin builds can't be used directly in MSVC for some reasons. Also `.a` archive after unpacking has `.o` objects, are they compatible with MSVC if to pack them into `.lib`? – Arty Sep 18 '21 at 07:39

2 Answers2

1

Sorry for the short answer because I don't have time.

I downloaded MPIR source code from here

And the explanation on how to build library is given by great Thomas Kim a Korean tutor on various fields of computer language is here. I have been struggling building GMP x64 bit for Windows and MPIR library came to my rescue. I built using Microsoft Visual Studio (VC++).

The code I had written using GMP library almost got built with MPIR without any change except adding for library.

EDITED: In order to build GMP for windows you need to install MINGW32. It is a 32 bit application. I was not able to get MING64 a 64 bit application containing bash shell, with gcc support. You can open the shell and build as specified command.

./configure <parameters>
make
make install
make test
Ramnath
  • 11
  • 2
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34140413) – user16217248 Apr 04 '23 at 23:36
  • I answered it for others in future who come here. I might be helpful for them also. It was a struggle for me, and so thought others might benefit. Thank you. – Ramnath Apr 06 '23 at 07:42
0

I successfully managed to compile a working fully statically linked program with GMP using MSVC under Windows.

For that I used installation of MSYS, which is located in c:/bin/msys/ on my machine.

Then inside MSYS shell installed GMP packages mingw-w64-clang-x86_64-gmp and gmp-devel (pacman -S gmp-devel to install and pacman -Ss gmp to search).

In MSVC compiler I added include directory c:/bin/msys/clang64/include/.

Wrote an example of GMP usage program in C++, that implements Trial Division / Pollard's Rho / Pollard's P-1 factoring algorithms using long arithmetics. This program uses both mpz_...() C routines and mpz_class C++ wrapper class. For example this program is located in main.cpp.

To linker command line I added following libraries:

c:/bin/msys/clang64/lib/libgmp.a
c:/bin/msys/clang64/lib/libgmpxx.a
c:/bin/msys/mingw64/lib/gcc/x86_64-w64-mingw32/10.3.0/libgcc.a
c:/bin/msys/clang64/x86_64-w64-mingw32/lib/libmingwex.a

Also I had to add /FORCE flag (read about it here) to linker command, because libmingwex.a has some symbols overlapping with default MSVC's libraries, precisely without /FORCE I had following errors:

libucrt.lib(strnlen.obj) : error LNK2005: wcsnlen already defined in libmingwex.a(lib64_libmingwex_a-wcsnlen.o)
libucrt.lib(strnlen.obj) : error LNK2005: strnlen already defined in libmingwex.a(lib64_libmingwex_a-strnlen.o)
bin\win-msvc-m-64-release\drafts\gmp_int_msvc.exe : fatal error LNK1169: one or more multiply defined symbols found

All steps produced working (tested) final statically-linked program without any external DLL dependencies (of course except for default system DLLs of Windows).

It means MSYS's libraries .a are fully compatible with MSVC and link successfully in MSVC compilation.

Not to have /FORCE linker flag I also did extra following steps. Made a copy of c:/bin/msys/clang64/x86_64-w64-mingw32/lib/libmingwex.a library. Used c:/bin/msys/clang64/bin/objcopy.exe util, which probably was installed together with Clang. With objcopy renamed overlapping symbols:

objcopy --redefine-sym wcsnlen=msys_wcsnlen libmingwex.a
objcopy --redefine-sym strnlen=msys_strnlen libmingwex.a

which allowed me to successfully use this modified libmingwex.a library to link in MSVC without using /FORCE.

Arty
  • 14,883
  • 6
  • 36
  • 69