Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Request: Instructions to build Quantlib.a on Windows #2

Closed
chinsoon12 opened this issue Jul 18, 2018 · 9 comments
Closed

Request: Instructions to build Quantlib.a on Windows #2

chinsoon12 opened this issue Jul 18, 2018 · 9 comments

Comments

@chinsoon12
Copy link

chinsoon12 commented Jul 18, 2018

Thanks so much for all your time and hard work in all the R packages, Jeroen.

Request:
I understand that you are very busy and tied up.
Can you please kindly share the instructions on how you produce the quantlib lib for Windows?
Just some pointers or sharing your log for ./configure and make install will be helpful so that I can go and figure it out myself.

What I have tried:
I am trying to cross compile Quantlib on Win 7 via cygwin_64 using Rtools 3.4. Windows PATH has C:\Rtools\bin;C:\Rtools\mingw_64\bin
I am following the instructions from http://www.stats.ox.ac.uk/pub/Rtools/goodies/sources/README.txt. And have been facing various problems like

configure: error: Boost development files not found

even after installing Boost via cygwin setup and then using LDFLAGS & CPPFLAGS, and also --with-boost-include & --with-boost-lib

And you seems to be able to build Quantlib with only the header files from Boost.

@joshuaulrich
Copy link
Collaborator

Here's the script I use to build QuantLib and RQuantLib on Windows. Though I have to warn you that it often does not work flawlessly on the first run. There are usually changes I have to make after something fails and I investigate the error. But it should get you started.

#!/bin/sh
#set -x
set -e

###############################################################################
# NOTES:
#
# All directories must be specified as Windows paths (e.g. C:/Windows)
#
# This file has only been tested using MinGW's msys_1.0. Attempts to use msys2
# were not successful. Use msys.bat to call build_RQuantLib.sh
#
# Download QuantLib and boost sources and unzip them into $BUILD_ROOT/src.
# Update the $BOOST_VERSION and $QL_VERSION environment variables to reflect
# the versions used.
#
# The current RQuantLib Makefile deletes all object files and downloads the
# pre-built QuantLib library from https://github.com/rwinlib/quantlib. Update
# the Makefile to point to your local libQuantLib.a and remove/comment the
# 'make all' commands to prevent the deletion and download.

###############################################################################
# build environment
BOOST_VERSION=1_68_0
QL_VERSION=1.14
RQL_VERSION=0.4.5.2
BUILD_ROOT="E:/build"
R_TOOLS_DIR="E:/R/Rtools35"

BINPREF=$R_TOOLS_DIR/mingw_32/bin/
BINPREF64=$R_TOOLS_DIR/mingw_64/bin/

QUANTLIB_SRC="$BUILD_ROOT/src/QuantLib-$QL_VERSION"
BOOSTLIB="$BUILD_ROOT/src/boost_$BOOST_VERSION"
QUANTLIB_ROOT="$BUILD_ROOT/QuantLib"

###############################################################################
# Check Rtools locations
if [ ! -d "$R_TOOLS_DIR/mingw_32" ]; then
    echo "$R_TOOLS_DIR/mingw_32 not found"
    exit 1
fi
if [ ! -d "$R_TOOLS_DIR/mingw_64" ]; then
    echo "$R_TOOLS_DIR/mingw_64 not found"
    exit 1
fi
# Check for boost headers
if [ ! -d "$BOOSTLIB/boost" ]; then
    echo "Did not find boost headers in $BOOSTLIB/boost"
    exit 1
fi
# Check for quantlib source
if [ ! -d "$QUANTLIB_SRC" ]; then
    echo "Did not find QuantLib source in $QUANTLIB_SRC"
    exit 1
fi
# Ensure there is an /etc/fstab
if [ -n "/etc/fstab" ]; then
    touch /etc/fstab
fi

# Install architecture-specific files into these directories via the
# --exec-prefix argument to QuantLib's configure script
mkdir -p $QUANTLIB_ROOT/lib/i386
mkdir -p $QUANTLIB_ROOT/lib/x64

# Ensure a mount point exists and add bin/ to PATH
mkdir -p /mingw
export PATH="$(cygpath -u $R_TOOLS_DIR)/bin":/mingw/bin:$PATH

# Use ccache if available
if [ -x "$(command -v ccache)" ]; then
        export PATH=/usr/lib/ccache/bin:$PATH
fi

################################################################################
## 32-bit QuantLib
mount --replace $R_TOOLS_DIR/mingw_32 /mingw

    ## Create and change to build directory
    #mkdir -p $BUILD_ROOT/ql32
    #cd $BUILD_ROOT/ql32

# QuantLib 'buglet' prevents building outside source directory
cd $QUANTLIB_SRC

# Clean prior build before (re-)running configure
if [ -f "Makefile" ]; then
  make -j6 distclean
fi

CXXFLAGS="-g0 -O2" $QUANTLIB_SRC/configure --prefix=$QUANTLIB_ROOT \
    --exec-prefix=$QUANTLIB_ROOT/lib/i386 --with-boost-include=$BOOSTLIB \
    --enable-intraday --enable-shared --enable-static --with-gnu-ld
make -j6
make install

# Shrink quantlib library
cp $QUANTLIB_ROOT/lib/i386/libQuantLib.a \
   $QUANTLIB_ROOT/lib/i386/libQuantLib.a.bak
strip --strip-unneeded $QUANTLIB_ROOT/lib/i386/libQuantLib.a

################################################################################
## 64-bit QuantLib
mount --replace $R_TOOLS_DIR/mingw_64 /mingw

  ## Create and change to build directory
  #mkdir -p $BUILD_ROOT/ql64
  #cd $BUILD_ROOT/ql64

# QuantLib 'bug' prevents building outside source directory
cd $QUANTLIB_SRC

# Clean prior build before (re-)running configure
if [ -f "Makefile" ]; then
  make -j6 distclean
fi

CXXFLAGS="-m64 -g0 -O2" $QUANTLIB_SRC/configure --prefix=$QUANTLIB_ROOT \
    --exec-prefix=$QUANTLIB_ROOT/lib/x64 --with-boost-include=$BOOSTLIB \
    --enable-intraday --enable-shared --enable-static --with-gnu-ld
make -j6
make install

## Shrink quantlib library
cp $QUANTLIB_ROOT/lib/x64/libQuantLib.a \
   $QUANTLIB_ROOT/lib/x64/libQuantLib.a.bak
strip --strip-unneeded $QUANTLIB_ROOT/lib/x64/libQuantLib.a

###############################################################################
# Install RQuantLib
#export QUANTLIB_ROOT
#export BOOSTLIB
# RQuantLib expects headers in $QUANTLIB_ROOT/ql, not $QUANTLIB_ROOT/include/ql
#ln -s $QUANTLIB_ROOT/include/ql $QUANTLIB_ROOT/.

#mkdir -p $BUILD_ROOT/library
#R_CMD="Rscript -e 'install.packages(c(\"Rcpp\",\"zoo\"), lib = \"$BUILD_ROOT/library\")'"
#Rscript -e $R_CMD
#
## Need to remove the OS_type: unix from DESCRIPTION
#cd $BUILD_ROOT
#R CMD INSTALL RQuantLib_${RQL_VERSION}.tar.gz --library=$BUILD_ROOT/library --no-clean-on-error --preclean --build
#
## run tests on each system
#Rscript --arch x64 -e 'source(system.file(package=\"RQuantLib\", \"unitTests/runTests.R\"))'
#Rscript --arch i386 -e 'source(system.file(package=\"RQuantLib\", \"unitTests/runTests.R\"))'

@chinsoon12
Copy link
Author

Thank you so much, @joshuaulrich! I will try to find my way. Cheers!

@shrektan
Copy link

shrektan commented Dec 2, 2018

Just to note that I need to change

CXXFLAGS="-g0 -O2" $QUANTLIB_SRC/configure --prefix=$QUANTLIB_ROOT \
    --exec-prefix=$QUANTLIB_ROOT/lib/i386 --with-boost-include=$BOOSTLIB \
    --enable-intraday --enable-shared --enable-static --with-gnu-ld

to

$QUANTLIB_SRC/configure CXXFLAGS="-g0 -O2 -std=gnu++11"  --prefix=$QUANTLIB_ROOT \
    --exec-prefix=$QUANTLIB_ROOT/lib/i386 --with-boost-include=$BOOSTLIB \
    --enable-intraday --enable-shared --enable-static --with-gnu-ld

Otherwise, it throws an error that boost library cannot be found. Moreover, the flag -std=gnu++11 may be necessary, or linking against a file compiled with c++11 leads to segfaults. See the discussion in https://groups.io/g/rquantlib/message/20

@shrektan
Copy link

shrektan commented Dec 2, 2018

My memory is still fresh now. I'd like to add step-by-step notes before I forget. Later on, we may post them together on the Wiki...

  1. Install the MinGW and MSYS
    • Rtools alone is not enough
    • install 'MinGW Installation Manager (mingw-get)' first and use 'mingw-get' to install components like MSYS, libtools, autoconf, etc -> whenever the later step reports missing tools, you should install it there.
  2. If you are using the source from git, instead of the released version, you should cd to the quantlib source directory and run './autogen.sh' to generate the configure file
  3. Modify and save the script above provided by Josh (the name may be build-quantlib.sh)
  4. Open the MSYS by calling the C:\MinGW\msys\1.0\mysy.bat, execute the file in the opened window.

@shrektan
Copy link

shrektan commented Dec 3, 2018

@joshuaulrich Forgive my verboseness...

But it's really my first time to know that I can call Rscript --arch i386 and Rscript --arch x64 to launch i386 and x64 version of R conveniently (before I was using C:/app/R/bin/i386/Rscript.exe... which is quite silly) . Thanks !!!

@gwd999
Copy link

gwd999 commented Oct 5, 2021

Here's the script I use to build QuantLib and RQuantLib on Windows.

Does this also do the trick in R-4?

@jeroen
Copy link
Contributor

jeroen commented Oct 6, 2021

Use the rtools-packages formula for R 4: https://github.com/r-windows/rtools-packages/tree/master/mingw-w64-quantlib

@gwd999
Copy link

gwd999 commented Oct 6, 2021

@jeroen thanks for the hint
I got quantlib on Rtools MinGW64 up and running (to use for compiling against it) via your instructions around the so called "R hackers only" tips and hints

[ 1. uncomment last two lines in /etc/pacman.conf then]
[ 2. in Rtools MinGW shell run pacman -Suy then]
I later realized that steps 1. and 2. are unnecessary b/c it is included in rtools-packages already
3. install via pacman -S --needed mingw-w64-{x86_64,i686}-quantlib which will also fetch boost (as a dependency) - as of today (Oct. 2021) this would be QuantLib-1.16 and boost_1_67
3. add your required R.exe to the PATH export PATH=/c/path/to/R/bin:$PATH and then (everything should just rock) via
4. R CMD INSTALL [build] ThePackageThatNeedsQuantLib
DO NOT FORGET TO ADJUST your Makefile.win to look something like

PKG_CPPFLAGS= -I/mingw64/include -I/mingw64/include/boost
PKG_LIBS= -lQuantLib

in case you installed w/o any customization into standard folders - in case you customized you probably know how to adjust anyway.

Thanx and keep up the great work ... on R for WINdows

@klin333
Copy link

klin333 commented Feb 7, 2022

FYI, I got "long double" related compiler errors when using boost_1_78_0. However this was fixed when I rolled back to boost_1_68_0

Also, in the sh script, I had to change $QUANTLIB_ROOT/lib/x64/libQuantLib.a to $QUANTLIB_ROOT/lib/x64/lib/libQuantLib.a (and similar with .bak and i386)

I was using RBuildTools 3.4 with QL 1.25

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants