How to compile GMP for windows using Visual Studio
Asked Answered
B

4

5

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.

Badge answered 17/11, 2017 at 21:17 Comment(9)
Where to download gmp binaries for windows or compile them from sources using Visual Studio?Badge
I am looking for gmp libs or looking of a compilation way. Not sure if this is off-topic.Badge
This is off topic. Part asking for tutorial and part asking for library. Not sure how to salvage this question. Do you have the mingw code? If so have you tried to make a visual studio project around it?Fayefayette
I want to get the libs. I am trying to get this by googling for libs or compiling. The visual studio project creation is something I am working on now. Not sure why it is offtopic...Badge
You are running up against Rule 4, which you will find is markedly different from Rule 34. I have't lodged any down or close votes because I'm kinda hoping you do get an answer for this. I might need it one day soon.Fayefayette
If you install mingw, most likely it will come with a package manager and some version of GMP.Magistracy
OK. Thank you. I just would like to create VS project. And thinking how to deal with gmp.Badge
GMP is available already prebuilt on MinGW. Also available here.Swinge
Please put a look at my answer, just wrote it now, it describes in very details 3 ways of compiling GMP and MPIR under Windows VisualStudio.Federalism
F
6

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.

Federalism answered 26/9, 2022 at 9:52 Comment(0)
B
3

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):

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:

  1. Use GMP version provided in your MinGW distribution
  2. Compile own GMP using MinGW/Cygwin
  3. Use MPIR fork of GMP compilable using VisualStudio
  4. Try to solve all the compilation problems yourself, some hints for older GMP versions are here:
Blodgett answered 18/11, 2017 at 14:19 Comment(0)
A
0

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.

Arlinearlington answered 8/11, 2022 at 16:38 Comment(0)
C
0

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.

Creed answered 22/12, 2023 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.