Skip to content

Instantly share code, notes, and snippets.

@zrsmithson
Last active October 2, 2024 10:39
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

@mehhdiii
Copy link

Thank you for the concise instructions!
The build somehow took 5 hours tho.

@ayyuceates
Copy link

Thank you, it is very clear and useful !

@paramsiddharth
Copy link

Thank you very much for this tutorial! :)

@d4nnoyer
Copy link

Hello. Thank you for this manual!

I followed all your instructions and at the beginning of compiling libraries I got one "no" check:
Boos.Config Feature Check: cxx11_thread_local : no

Does it mean that Boost libraries, built by this way, are not supposed to support multithreading programming with gcc and also ?

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

Hi, installing boost on Windows works great following this tutorial, however, I have problems with missing "shared.cmake" files for the 32-bit part (I need it for boost log setup). My project needs dynamically linked libraries, so in CMakeLists.txt ”Boost_USE_STATIC_LIBS" must be set to OFF). All works well when compiling for x64, but trying to compile a 32-bit build fails with the following error messages:

C:/JTSDK64-Tools/tools/boost/1.77.0/lib/cmake/boost_log_setup-1.77.0/boost_log_setup-config.cmake
  but it set boost_log_setup_FOUND to FALSE so package "boost_log_setup" is  considered to be NOT FOUND.  Reason given by package:
  No suitable build variant has been found.
  The following variants have been tried and rejected:
  * libboost_log_setup-mgw8-mt-d-x32-1_77.a (static,  Boost_USE_STATIC_LIBS=OFF)
  * libboost_log_setup-mgw8-mt-s-x32-1_77.a (static,  Boost_USE_STATIC_LIBS=OFF)
  * libboost_log_setup-mgw8-mt-sd-x32-1_77.a (static,  Boost_USE_STATIC_LIBS=OFF)
  * libboost_log_setup-mgw8-mt-x32-1_77.a (static, Boost_USE_STATIC_LIBS=OFF)

I found out that at \boost[VERSION]\lib\cmake, 2 files are missing: libboost_log_setup-variant-mgw8-mt-d-x32-1_77-shared.cmake and libboost_log_setup-variant-mgw8-mt-x32-1_77-shared.cmake. The x64 version of these files are there, but NOT the x32 files, meaning that there is no support for shared libraries for 32-bit. No matter which boost version you use, it is always the same. (I have tested 1.74.0 and 1.77.0).
boost-log-setup

On the internet I found such files for boost built with other mgw versions, and tried to adopt them. They are leading to a file "libboost_atomic-mgw8-mt-d-x32-1_77.a", which is there, however, which obviously does NOT support shared libraries, too. No way to bring it to work.

My question: Does anybody know how I can add 32-bit support for the needed dynamically linked boost libraries? Was anyone successfully with it? If yes, can you provide me with the missing files, please? I would need such files for mgw8.
Thanks

@paramsiddharth
Copy link

@DG2YCB Have you tried using MSYS2? I have completely switched to using MSYS2 for my low-level development on Windows using GCC and I have no complaints. For 32-bit, this is the package you will need: mingw-w64-i686-boost.

I can guarantee you'll always get the latest package build on MSYS2 before anywhere else. Good luck! :)

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

@paramsiddharth No, I haven't yet. I don't have such an environment on my computer. Do you have a working MSYS2 environment in place? If yes, could you try to build for me boost 1.74 or 1.77 with this mingw-w64-i686-boost? That would be really great.

@paramsiddharth
Copy link

@paramsiddharth No, I haven't yet. I don't have such an environment on my computer. Do you have a working MSYS2 environment in place? If yes, could you try to build for me boost 1.74 or 1.77 with this mingw-w64-i686-boost? That would be really great.

You won't need to build, a prebuilt library will be made available to you via MSYS2. :) That's how I did it too.

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

How can I install the needed package? I only found this page: https://packages.msys2.org/package/mingw-w64-i686-gcc and this file: https://mirror.msys2.org/mingw/mingw32/mingw-w64-i686-gcc-10.3.0-5-any.pkg.tar.zst. But how to deal with .zst files on my Windows computer?

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

And another question: I need boost 1.74 or 1.77 for mgw8 (not 10 or 9). Does this package make such libraries available? My compilation project only works with mgw8 files.

@paramsiddharth
Copy link

@DG2YCB
The following versions are available:
image

Source: https://repo.msys2.org/mingw/mingw32/
Looks like Boost 1.74 is available! 1.77 is yet to be made available.
By default MSYS2 will install the latest GCC, so to use an older version i. e. 8, I myself never tried it before so I'll have to look. It must be possible.

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

How to use such ".pkg.tar.zst" files? Which program do I need to open or install them? 7zip is not able to open .zst files...

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

I managed to decompress the mingw-w64-i686-boost-1.74.0-1-any.pkg.tar.zst file on my Linux computer. Then I switched to my Windows machine, set the resulting boost libraries as default for my project. Unfortunately the bad new is: My project doesn't work with these boost files either. First it gives no error message, but after 99 % compilation it comes to about 200 error messages about numerous objects not beeing found.

@DG2YCB
Copy link

DG2YCB commented Aug 24, 2021

I suspect the reason why these boost files do not work for my project is that with my other self-compiled boost files there is an additional "cmake" sub-directory within the lib directory. This "cmake" sub-directory contains 457 .cmake files, As such a "cmake" sub-directory is missing here, for my project Cmake doesn't work properly with such boost libraries.

@tomay3000
Copy link

@DG2YCB,
Install this version of Mingw-W64 which has prebuilt boost in it:

http://mingw-w64.org/doku.php/download

image

https://gcc-mcf.lhmouse.com/

image

@paramsiddharth
Copy link

@tomay3000 Thank you very much for this! :) Though it seems like @DG2YCB is looking for 32-bit MinGW version 8, not 9 or above.

@DG2YCB
Copy link

DG2YCB commented Aug 25, 2021

Thanks a lot to all of you for trying to help me. Unfortunately also the boost files @tomay3000 recommended do not work for my project (not even for 64-bit). All the boost files I found on the internet have a slightly different name structure, and - very important - the 457 .cmake files within the "cmake" sub-directory are missing.

boost-issue
.

@DG2YCB
Copy link

DG2YCB commented Aug 25, 2021

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.

@DG2YCB
Copy link

DG2YCB commented Aug 26, 2021

Finally I managed to compile a 32-bit version of boost_1.77,0 which works for my project !!!!
How did I make it? Somewhere else on the internet (https://sourceforge.net/projects/mingw-w64/files/ ) I found this file: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/8.1.0/threads-posix/dwarf/i686-8.1.0-release-posix-dwarf-rt_v6-rev0.7z . It contains mingw32 v8.1. But in contrast to all other “mingw32” versions I have found so far, when compiling boost with this version it builds only “for x86”. And the resulting boost files contain both static and shared libraries. These files also include the missing “shared” files I was looking for, so that I now have the needed dynamic 32-bit libraries. For those of you who may need this too: Find my compiled boost_1.77.0_32 libraries here: https://sourceforge.net/projects/hamlib-sdk/files/Windows/JTSDK-3.2-x86-Patches/boost_1.77.0_32.zip/download.

@paramsiddharth
Copy link

Finally I managed to compile a 32-bit version of boost_1.77,0 which works for my project !!!!
How did I make it? Somewhere else on the internet (https://sourceforge.net/projects/mingw-w64/files/ ) I found this file: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/8.1.0/threads-posix/dwarf/i686-8.1.0-release-posix-dwarf-rt_v6-rev0.7z . It contains mingw32 v8.1. But in contrast to all other “mingw32” versions I have found so far, when compiling boost with this version it builds only “for x86”. And the resulting boost files contain both static and shared libraries. These files also include the missing “shared” files I was looking for, so that I now have the needed dynamic 32-bit libraries. For those of you who may need this too: Find my compiled boost_1.77.0_32 libraries here: https://sourceforge.net/projects/hamlib-sdk/files/Windows/JTSDK-3.2-x86-Patches/boost_1.77.0_32.zip/download.

Thank you so much for sharing! :)

@lifeinaglasshouse
Copy link

Took about 5 hours compiling the boost library and worth it. Thanks

@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