Skip to content

Building GMP and MPFR on Windows with Visual Studio

Mael Rouxel-Labbé edited this page Mar 28, 2024 · 1 revision

Compile GMP using vcpkg

  • Install VCPKG (also described here for more details) from the MSVC command prompt x64 Native Tools Command Prompt for VS 2022 (or another version of your choice):
git clone https://github.com/Microsoft/vcpkg --depth=1
cd vcpkg
cmd /c bootstrap-vcpkg.bat
  • Compile GMP with vcpkg (still in the Native VS command prompt) with
vcpkg install gmp --triplet=x64-windows

At the end of the compilation process, a message mentioning the path to a ZIP archive is displayed. In my setup, it was

Stored binary cache: "C:\Users\Me\AppData\Local\vcpkg\archives\da\da0a40b7e8a4e8d8e5bceb23a9367183e50f797aee81a97fde84886e5412bd1e.zip"

Unzip this archive, it contains the headers and libraries to use GMP.

Add custom compiler options

It is possible to add custom compiler and linker options in the compilation described above. To do so, modify the triplet file that is used for compilation. In the case mentioned above, modify vcpkg/triplets/x64-windows.cmake, by adding the following lines before running the vcpkg install command :

string(APPEND VCPKG_CXX_FLAGS_RELEASE " /ANOTHER_COMPILATION_FLAG")
string(APPEND VCPKG_C_FLAGS_RELEASE " /ANOTHER_COMPILATION_FLAG")
string(APPEND VCPKG_LINKER_FLAGS_RELEASE " /ANOTHER_LINK_FLAG")

string(APPEND VCPKG_CXX_FLAGS_DEBUG " /ANOTHER_COMPILATION_FLAG")
string(APPEND VCPKG_C_FLAGS_DEBUG " /ANOTHER_COMPILATION_FLAG")
string(APPEND VCPKG_LINKER_FLAGS_DEBUG " /ANOTHER_LINK_FLAG")

Static Vs Dynamic CRT

To have consistent compilation options between the CGAL examples (the /MD compilation option) and the GMP and MPFR libraries used, create a new triplet based on vcpkg/triplets/x64-windows.cmake. Add the following setup and save it as vcpkg/triplets/x64-windows-vc143.cmake (for example)

set(VCPKG_TARGET_ARCHITECTURE x64)

# `VCPKG_LIBRARY_LINKAGE` determines if a library/port is build as a static or dynamic library
set(VCPKG_LIBRARY_LINKAGE dynamic) #to get the dlls built

# `VCPKG_CRT_LINKAGE` determines if the static (`/MT(d)`) or dynamic (`/MD(d)`) CRT (C runtime) is used
set(VCPKG_CRT_LINKAGE dynamic)

# specify the MSVC toolset (optional)
set(VCPKG_PLATFORM_TOOLSET "v143")
set(VCPKG_DEP_INFO_OVERRIDE_VARS "v143")

To use this new triplet along with its customization, the installation command becomes

vcpkg install gmp --triplet=x64-windows-vc143

About MPIR

It is also possible to install MPIR instead of GMP. MPIR is a fork of GMP, with same interface. To install MPIR, run vcpkg install mpir. Note that this requires to delete GMP package first, because only one of MPIR or GMP can be installed.

Sources

Clone this wiki locally