Building QuantLib on *nix

QuantLib and ruby/python bindings are available out-of-the-box for Ubuntu.
However, we have to build the Java bindings manually (through SWIG).

Building Java Bindings

  • Download the SWIG setup from QuantLib Extensions page
  • Run ./configure and make -C Java
  • That creates the Java files and the qunatlib_wrapper.cpp file, but does not actually build the wrapper itself
  • We need to build the shared wrapper library first. These are setup for Ubuntu:
    g++ -c quantlib_wrap.cpp -I/usr/lib/jvm/java-1.5.0-sun-1.5.0.08/include/linux \
    -I/tmp/toli/quantlib/include -I/usr/local/include \
    -I/usr/lib/jvm/java-1.5.0-sun-1.5.0.08/include
    
  • That creates the quantlib_wrap.o file which we now need to link into a static library. The following command creates the wrapper .so while linking to the QuantLib library in /tmp/toli/quantlib/lib (you may need to re-point it to a real .so)
    g++ -shared quantlib_wrap.o -Wl -o libquantlib_wrap.so -L/tmp/toli/quantlib/lib -lQuantLib
    
  • This creates the libquantlib_wrap.so file which you need to copy to your quantlib/lib directory (same place where libquantlib.so lives)
  • Create the jar file for all the java code in the QuantLib-SWIG-xxx/Java directory:
    md bin
    find org/quantlib -name *.java | xargs javac -d bin
    cd bin
    jar cf quantlib-wrapper.jar org
    
  • We now have the quantlib-wrapper.jar and the corresponding quantlib and quantlib-wrapper C++ libraries.
  • Set the LD_LIBRARY_PATH to point to the location of the new quantlib .so files
  • Try compiling the following program:
    public class Hello {
        static {
            System.loadLibrary("quantlib_wrap");
        }
        public static void main(String[] args)
        {
            QuantLib ql = new QuantLib();
            double nd = QuantLib.nullDouble();
            System.out.println("ql nullDouble is "+nd);
    
            System.out.println("and sunday is "+Weekday.Sunday.toString());
        }
    }
    
  • Compile it with:
    javac -cp quantlib-wrapper.jar Hello.java  
    
  • Run it with:
    java -cp quantlib-wrapper.jar:. Hello
    
  • You should get something along the lines of
    ql nullDouble is 3.4028234663852886E38
    and sunday is Sunday
    
  • If you get errors, make sure your LD_LIBRARY_PATH points to the location of .so files, and that in the System.loadLibrary() call you are loading the lib name with the leading lib

Building Ruby Bindings

Building Ruby actually works quite well with make -C Ruby.
However, if you build it by hand you need to copy the resulting files into

/usr/lib/ruby/1.8/QuantLib.rb		
/usr/lib/ruby/1.8/i486-linux/QuantLibc.so