I am trying to install gmp on windows. I have found the mingw way of compiling from sources on windows. But was unable to find binaries fro gmp 6.1.2 or visual studio project in order to compile from sources. So the question is: Where can I download the gmp 6.1.2 binaries or compile from sources using Visual Studio.
I'll describe three ways of compiling GMP in Windows.
First
Install Visual Studio 2022 Community from this page.
Install VCPKG package manager as described here, basically just do two steps:
git clone https://github.com/Microsoft/vcpkg --depth=1
inside vcpkg
directory run
cmd /c bootstrap-vcpkg.bat
Set system environment variable VCPKG_DEFAULT_TRIPLET=x64-windows-static
, for doing this press WinKey+Pause, then click "Advanced system settings", then "Environment variables", inside "System variables" click "New" and set value of VCPKG_DEFAULT_TRIPLET
to x64-windows-static
.
Instead of this step above (setting variable) you can just pass triple directly to all vcpkg commands like vcpkg install gmp --triplet=x64-windows-static
.
Inside git directory of vcpkg run following command:
vcpkg install gmp --triplet=x64-windows-static
(you may omit --triplet=x64-windows-static
if you set environment variable as I told above)
It will take quite a lot of time, it will compile many packages from sources.
After full compilation is finished it will show in console path to ZIP file with compiled GMP library. On my system ZIP file was created at C:\Users\user\AppData\Local\vcpkg\archives\8d\8d1c08fabf677187083dedd12d6accf7114d91580e75611c065f1674b600bee9.zip
.
Unpack this ZIP file and then you can compile your C++ program like following:
cl program.cpp /O2 /GL /EHsc /std:c++latest /Ipath_to_unpacked_zip/include/ path_to_unpacked_zip/lib/gmp.lib
As you might know cl
command should be run from "x64 Native Command Prompt" command shell which can be found in "Windows Start Menu / Visual Studio 2022 /".
Also you may install MPIR instead of GMP, this is a fork of GMP, with same interface, but more preferred by Windows users. Just do vcpkg install mpir
, but this can be done only if you delete GMP package first, only one of MPIR or GMP can be installed.
Second
This step doesn't compile GMP, but uses precompiled binaries from MinGW installation.
Install Visual Studio as in first step.
Go to home page of MSYS2. Download installer, link is located near "1. Download the installer:" phrase. Install it to any location, e.g. c:\bin\msys\
.
After installation in Windows Start Menu go to application "MSYS2 64bit" and inside it start program "MSYS2 MSYS", it will run Unix-like shell, from it do:
pacman -S msys/binutils msys/gcc msys/mingw-w64-cross-crt-git clang64/mingw-w64-clang-x86_64-gmp
This command above will install all needed packages to use GMP. If you need more packages use -Ss
option like pacman -Ss clang
, this will search for CLang, so -Ss
does search and -S
installs.
If you need some time later, pacmans -Syu
command updates all installed packages, run this command two times, one time updates base system files, second time all other packages (after first time you need to close and open MSYS shell again).
Now you need one tweak, rename two symbols inside library libmingwex.a
because of collisions with libucrt.lib
library of Visual Studio.
In following two commands I assume that your MSYS installation folder is c:\dev\msys\
, you can change to one that you installed to.
c:\bin\msys\usr\bin\objcopy.exe --redefine-sym wcsnlen=wcsnlen_renamed --redefine-sym strnlen=strnlen_renamed c:\bin\msys\opt\x86_64-w64-mingw32\lib\libmingwex.a c:\bin\msys\opt\x86_64-w64-mingw32\lib\libmingwex_renamed.a
(this will create file libmingwex_renamed.a
with renamed two symbols out of libmingwex.a
library)
Now everything is ready and you can compile your C++ program like following:
cl program.cpp /O2 /GL /EHsc /std:c++latest /Ic:\bin\msys\clang64\include\ c:\bin\msys\clang64\lib\libgmp.a c:\bin\msys\usr\lib\gcc\x86_64-pc-msys\11.3.0\libgcc.a c:\bin\msys\opt\x86_64-w64-mingw32\lib\libmingwex_renamed.a
See that in command above I used 3 libraries libgmp.a
and libgcc.a
and libmingwex_renamed.a
. Also notice that libgcc.a
is taked from sub-folder \11.3.0\
, it is current version of installed GCC, but when time passes MSYS2 updates GCC to later versions, so this version-subfolder should be changed accordingly.
Third
Install Visual Studio like in First and Second steps.
In this step we will use MPIR, it is a fork of GMP, really good fork more suitable for Windows.
Clone repository:
git clone https://github.com/BrianGladman/mpir --depth=1
Inside folder .\mpir\msvc\vs22\
run:
cmd /c msbuild.bat gc LIB x64 Release
Above command builds Generic version that is suitable for any CPU. After that do
cmd /c msbuild.bat skylake_avx LIB x64 Release
Which builds very optimized version, faster than generic.
Very Important. If second (skylake) builds with failure, then Generic (gc) version can be used but it can be even 5x times slower. If fast Skylake version has failed then better not to use this Third way of compiling GMP, unless you can't do others, or if slow version is enough for you.
This command above should be run as usual from "x64 Native Command Prompt" shell of Visual Studio in Start Menu.
After build is finished GMP (actually MPIR), you can compile your program as:
cl program.cpp /O2 /GL /EHsc /std:c++latest /Ipath_to_mpir_repo\msvc\vs22\lib_mpir_skylake_avx\x64\Release\ path_to_mpir_repo\msvc\vs22\lib_mpir_skylake_avx\x64\Release\mpir.lib
Note that in command above I used \lib_mpir_skylake_avx\
subfolder for optimized AVX version, please use \lib_mpir_gc\
subfolder if only Generic version is available.
Compiling GMP on Windows with VisualStudio might be tricky, however there are already some SO questions, that might help you (depending on your exact use-case):
- Building GMP library with Visual Studio?
- How to install MPFR and GMP for C++ on visual studio
- GMP on visual studio c++
Simple answer is, that there are no sources of GMP compilable directly using VisualStudio as GMP is developed with UNIX in mind.
Summary of your options:
- Use GMP version provided in your MinGW distribution
- Compile own GMP using MinGW/Cygwin
- Use MPIR fork of GMP compilable using VisualStudio
- Try to solve all the compilation problems yourself, some hints for older GMP versions are here:
I faced the same problem in Windows 11 when I needed both gmp and gmpxx for which only the first option works, thanks to @Arty.
The only thing to add is that vcpkg install gmp --triplet=x64-windows-static
command should be run in a terminal with admin privilege, otherwise the following error will occur:
file RENAME failed to rename
C:/Users/Desktop/polycut/vcpkg/packages/gmp_x64-windows-static
to
C:/Users/Desktop/polycut/vcpkg/packages/gmp_x64-windows-static_tmp
because: Access is denied.
You can find MSVC '*.sln' for GMP in repo https://github.com/ShiftMediaProject/gmp in folder 'SMP'. Just you need open and build project in MSVC.
© 2022 - 2024 — McMap. All rights reserved.