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
.