Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Quantlib via SWIG for C#

Anyone have any experience using SWIG? I am currently researching QuantLib and saw that C# code can be generated using SWIG. We are exploring options to create a combined library of financial functions using QuantLib and a proprietary closed source library (which will probably be made available as .Net dlls). The idea is to combine both of these to create a unified super library. I have seen the .Net port of QuantLib, but it seems that it is not actively maintained (and not entirely sure of how much was actually ported), so I am avoiding it.

Step 1 of this is to evaluate the difficulty in producing a library that can be used 'anywhere' i.e. MS office applications ( via VBA), console apps as well server-side (eg web apps). I assume that this involves COM Interop, but I have no clue where to start or if I am even on the right track.

I have no experience with C++; and COM is something (a buzzword to me at the moment) that I have glazed over. I am aware of the relevant MSDN articles related to this topic.

I am looking for help along the following lines:

  1. Are there alternatives to using QuantLib in C#?
  2. With respect to my development environment, what would I need?
  3. Does anyone know of a ready-to-use QuantLib C# library compiled via SWIG? (first prize = less work for me)

Any help appreciated?

Edit: I have accepted my answer as the correct one unless there is a better one provided.

like image 332
Ahmad Avatar asked Jul 26 '10 10:07

Ahmad


1 Answers

So it would seem that the C# bindings for SWIG are available. One needs to browse the SVN repository or list of files on SouceForge to find them.

The SWIG folder contains several subfolders, depending on your language of choice, in my case C# was the one that interested me. You will need to download SWIG first and it the executable to system PATH variable.

Make sure to read the Readme.txt as it contains details about the QL_DIR environment variable that also needs to be created.

This may be a limitation of VS Express - but the solution file contains both the C# project and C++ project, of which only either the C# or C++ portions can be opened at any time. My suggestion would be to created separate solution files for the C++ and the C# projects.

Coming from a .Net background, lib files, obj files and the all the other jazz associated with c++ takes some time to getting use to.

Steps taken to get a working C# library

  1. Download QuantLib and Boost as described here
  2. Download the tar ball of the QuantLib-SWIG folder from SourceForge. Edit: There is a download of the zip available on the home page. Its listed within the subfolders. Check the v1.0 folders under Bindings
  3. Run the swig.cmd file located in QuantLib-SWIG\CSharp folder (you need to download SWIG first() --> this generates the cs wrapper files for Quantlib.
  4. Build the c++ project NQuantLibc - note the dll created here needs to be always shipped with the NQuantlib C# built dll. It needs to be placed in a location that is readable by the DLLImport attribute. (See Dynamic-Link Library Search Order on MSDN for list of locations)
  5. Build the C# project NQuantLib

With respect to my development environment, what would I need? It seems that the current version of QuantLib only builds with VS 2008. There have been some changes to VS 2010 which causes some issues with compilation. The C# project builds fine, however just remember to change the Target Framework from v4 to v3.5 to lower.

Note: As the time of writing this there was an issue with the QL_HEX_VERSION number in the source files of the C++ SWIG project. After running the swig.cmd open the VS project, and change Line 344 of quantlib_wrap.cpp to

#if QL_HEX_VERSION < 0x010001f0//0x010100f0

I have emailed the project author, and awaiting feedback. Edit: Luigi, correctly informed me that this is any issue with the code in SVN (which I used). He suggested that the QuantLib-SWIG-1.0.zip bindings should be used.

Quantlib also build on VC++ 2010 with the latest release.

like image 130
Ahmad Avatar answered Nov 07 '22 13:11

Ahmad