Skip to content

Instantly share code, notes, and snippets.

@zrsmithson
Last active November 1, 2024 09:16
Show Gist options
  • Save zrsmithson/0b72e0cb58d0cb946fc48b5c88511da8 to your computer and use it in GitHub Desktop.
Save zrsmithson/0b72e0cb58d0cb946fc48b5c88511da8 to your computer and use it in GitHub Desktop.
Installing boost on Windows using MinGW-w64 (gcc 64-bit)

Installing boost on Windows using MinGW-w64 (gcc 64-bit)

Introduction

Boost is easy when you are using headers or pre-compiled binaries for visual studio, but it can be a pain to compile from source on windows, especially when you want the 64-bit version of MinGW to use gcc/g++. This installation process should be thorough enough to simply copy and paste commands, but robust enough to install everything you need.

Note: if you need to install any of the libraries that need dependencies, see this great answer from stack overflow

Get files needed for install

Get the MinGW installer mingw-w64-install.exe from Sourceforge
Get the boost_1_68_0.zip source from Sourceforge
Note: This should work perfectly with other versions of boost as well
Copy these to a new folder
C:\install
It should now contain the following two files

  • mingw-w64-install.exe
  • boost_1_68_0.zip

Install MinGW-w64

Run the installer

Run mingw-w64-install.exe
Click next
Change the Architecture from i868 to x86_64

Click next and keep the default install location
Click next to start the install
Click Finish to exit the installer

After the install, add a hard link (junction) to the folder

Open a command prompt AS ADMIN

  • windows key -> type "cmd"
  • right click "command prompt"
  • Run as administrator
    Enter the following command to create a link to MinGW folder in C:\
    mklink /J C:\MinGW "C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64"

Add MinGW to the system PATH

Add this to the session and system PATH environment variable
set PATH=%PATH%;C:\MinGW\bin
setx /M PATH "%PATH%"
Check to ensure proper install
g++ --version should return the following info

Install boost

Navigate to install

cd C:\install

unzip to "install/boost_1_68_0"

powershell -command "Expand-Archive C:\install\boost_1_68_0.zip C:\install"
This takes about 15 minutes
cd C:\install\boost_1_68_0

Make directories for building and install

mkdir C:\boost-build
mkdir C:\install\boost_1_68_0\boost-build
mkdir C:\boost

Boost.Build setup

cd C:\install\boost_1_68_0\tools\build
prepare b2
bootstrap.bat gcc
Build boost.build with b2
b2 --prefix="C:\boost-build" install
Add C:\boost-build\bin to your session PATH variable
set PATH=%PATH%;C:\boost-build\bin

Building Boost

navigate back up to the boost unzipped root directory
cd C:\install\boost_1_68_0
Build boost with b2
b2 --build-dir="C:\install\boost_1_68_0\build" --build-type=complete --prefix="C:\boost" toolset=gcc install
This is going to take awhile, so try to run this command right before beginning the director's cut of Lord of the Ring Return of the King.
When this is done you should see the following output

You can now delete "C:\install" and "C:\boost-build"

Adding to projects

Everything should now be installed
Include folder:
C:\boost\include\boost-1_68
Linker folder:
C:\boost\lib
Link required libraries:
e.g. libboost_atomic-mgw81-mt-d-x64-1_68.a

@jpy794
Copy link

jpy794 commented Dec 21, 2021

Thanks a lot !

@ziwenjie
Copy link

Hi:
I follow your method.
In my cmakelist, found_package(Boost ) is right.
However, found_package(Boost components python REQUIRED) and found_package(Boost components gragh REQUIRED) got error and so on.
image

@GaganDhanoa
Copy link

GaganDhanoa commented Jun 18, 2022

This is an excellent guide to build boost libs.

But I'm confused as to what does this do: https://packages.msys2.org/package/mingw-w64-x86_64-boost
It installs "mingw-w64-x86_64-boost" using pacman package manager on mingw console/terminal.

But compiler now doesnt complain about boost function/class decelarations not found but gives errors about boost function definitions not found.
Compile time errors example: undefined reference to `boost::log::v2s_mt_nt6::trivial::logger::get()

What is the purpose of installing boost via pacman in mingw? "pacman -S mingw-w64-x86_64-boost"

@parul1987
Copy link

FYI. I found boost files that do work for my project here: https://sourceforge.net/projects/boostmingw/files/. However, the needed 32-bit "shared.cmake" files (=dynamic libraries) are available only for mingw 9.2 and mingw 10, not for mingw8.x. I have tried the mgw9 files, but they do not work for my project (as the rest is beeing done with Qt 5.15.x and mingw8.1). If anybody has a further idea how I can get the needed 32-bit dynamic ("shared") files for mgw8 please let me know. Otherwise I can only hope that one day my project will work with Qt 6.x.x either which uses mingw 9.x. Thanks and all the best to all of you.

Hi can we please connect I need some help for the same scenario, Please share the steps what you did to connect the boost libraries to QT project (with mingw compiler)

@DG2YCB
Copy link

DG2YCB commented Sep 27, 2022

Hi parul1987, Sure. Are you familiar with QRZ.com? There you find my email address. (I am an amateur radio enthusiast, and my page on QRZ.com is https://www.qrz.com/db/DG2YCB.) Otherwise send me another message. Then we'll find other ways to connect.

@parul1987
Copy link

Hi parul1987, Sure. Are you familiar with QRZ.com? There you find my email address. (I am an amateur radio enthusiast, and my page on QRZ.com is https://www.qrz.com/db/DG2YCB.) Otherwise send me another message. Then we'll find other ways to connect.

I Created the account on QRZ and also I sent u an email please check or let me know if we can chat somewhere.

@mbialoru
Copy link

mbialoru commented Nov 8, 2022

For anyone interested, here is an automated script for powershell

# Warning: Run as administrator
param(
    [Parameter(Mandatory = $False, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
    [String]$boost_version = "1.80.0",
    [String]$toolset = "gcc"
)

$boost_version_underscore = $boost_version -replace "\.", "_"
$start_directory = (pwd).path

# performance counter
$timer = [Diagnostics.Stopwatch]::StartNew()

New-Item -p ('C:\install', 'C:\boost-build', 'C:\boost') -ItemType "directory"
Invoke-WebRequest ("https://boostorg.jfrog.io/artifactory/main/release/"+$boost_version+"/source/boost_"+$boost_version_underscore+".zip") -outfile ("C:\boost_"+$boost_version_underscore+".zip")

# performance fix for otherwise slow as molasses Expand-Archive
Add-MpPreference -ExclusionPath "C:\install"
Add-Type -Assembly "System.IO.Compression.Filesystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory(("C:\boost_"+$boost_version_underscore+".zip"), "C:\install")

Set-Location ("C:\install\boost_"+$boost_version_underscore+"\tools\build")
Start-Process -Wait -NoNewWindow (".\bootstrap.bat") -ArgumentList $toolset
Start-Process -Wait -NoNewWindow (".\b2") -ArgumentList ('--prefix="C:\boost-build" toolset='+$toolset+' install')
$env:PATH += ";C:\boost-build"
Set-Location ("C:\install\boost_"+$boost_version_underscore)
Start-Process -Wait -NoNewWindow ("b2") -ArgumentList ('--build-dir="C:\install\boost_'+$boost_version_underscore+'\build"'+' --build-type=complete --prefix="C:\boost" toolset='+$toolset+' install')

# cleanup
Set-Location $start_directory
Remove-Item -r -force ('C:\install', 'C:\boost-build')
$timer.Stop()
$timer.Elapsed

@orlovskyjavaprofi
Copy link

Yes it works!
it just take some quality time to compile!
For me it took like more then 5 hours to compile, but it works!
Thanks!

@rafetkavak
Copy link

In the -Build boost.build with b2- part, I got "warning: Configuring default toolset "msvc"" warning. Therefore instead of b2 --prefix="C:\boost-build" install I used b2 toolset=gcc --prefix="C:\boost-build" as mentioned on this website https://www.boost.org/doc/libs/1_62_0/tools/build/tutorial.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment