cwindowsvisual-c++buildgmp

Static build of GMP for MSVC (Windows)


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?


Solution

  • 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.