Compile Boost libraries for Matlab MEX programming in Windows

Compile Boost with MinGW compiler compatible with Matlab in Windows

For setting up a Matlab compatible MinGW compiler, read the following note:
MinGW compiler for Matlab (MEX): Manual installation and configuration in Windows.

I describe the compilation procedure for boost version 1.66.0 in Windows 10.

(1) Make sure that your compiler path (where g++.exe is located) is on the top of other compilers in Environmental variables, System variables, Path.
R2015b up to R2017a -> Compiler: "MinGW-w64/TDM64", e.g. address in system path "C:\mingw-w64\x86_64-5.3.0-posix-seh-rt_v4-rev0\mingw64\bin".
R2017b -> Compiler: "MinGW 5.3", e.g. address in system path "C:\TDM-GCC-64\bin".

(2) Open "project-config.jam" with a text-editor and replace "using msvc ;" with "using gcc ;".

(3) Download and unzip the boost package -> link: boost_1_66_0.zip

(4) Open Windows "Command Prompt" and navigate to boost folder.

(5) Run "bootstrap.bat gcc". After finishing, you should see "b2.exe" in the boost folder.

(6) In the Windows Command Prompt, write the following script:

b2 toolset=gcc --build-type=complete stage link=shared threading=multi architecture=x86 address-model=64 -j 8
where "-j" indicate the number of cores by which you can compile the code and reduce the compilation time.

(7) Compiled libraries are located in ./stage/lib.
To link libraries in your mex compilation, you need to rename the library with ".a" format to ".lib" format.
For example, if you need to link boost_system to your mex function, you need "libboost_system-mgw49-mt-1_65_1.dll", in addition to "libboost_system-mgw49-mt-1_65_1.lib" which was renamed from "libboost_system-mgw49-mt-1_65_1.a".

(8) To build your mex function in Matlab, you need to link to the boost library like the following example:
mex -V -IC:\boost_1_65_1\ -LC:\boost_1_65_1\stage\lib -lboost_system-mgw49-mt-1_65_1 example.cpp other_related_files.cpp
Note that "-l" in the script above is replacing the first "lib" keyword in the name of the library.

(9) To call the compiled mex function in Matlab, copy the required library with extension ".dll" to the path where your mex-function is located and called.