# using quantlib function in my c++ program

I want to include the QuantLib function for option greeks calculations in my own C++ code. My question is: can I just include those functions? I don't want to use the rest of their stuff.

I obviously cannot just write:

QuantLib::europeanOption.delta()


#include <ql/quantlib.hpp>

using namespace QuantLib;


I wish they had some good documentation.

-
Since this is a programming-specific question, isn't this more appropriate for stack overflow? –  Tal Fishman Aug 26 '11 at 0:39
ok, I'll ask there.. –  quant_in_making Aug 26 '11 at 1:25
@sheegaon This question is about a specific quant library. The user is asking about how to include a single function. So if anyone knows which header that specific function is in, it would be an existing user of said library. –  chrisaycock Aug 26 '11 at 1:43
Maybe it's worth posting a Community Wiki post on how to actually use QuantLib in a new project? Can we do Wiki posts here on QF SE? –  Dmitri Nesteruk Aug 26 '11 at 14:03
I think this question is absolutely on topic here as it is. It is about a specific problem with a specific quant lib - so it fits in well in my opinion. –  vonjd Aug 26 '11 at 17:43

This is off-topic and maybe belongs to StackOverflow, but here goes.

1. Compile QuantLib

The best way is to open the Visual Studio Command Prompt using a shortcut under Programs→Microsoft Visual Studio→Visual Studio Tools. Now, you need to navigate to the QuantLib folder inside the folder where you have QuantLib (there are other folders such as QuantLibXL – that’s not the one you need.

Now, compile the whole of QuantLib by running the following command: msbuild /t:Rebuild /p:Configuration=Release QuantLib_vc10.sln.

Inside this folder, if you look in the lib folder, you’ll get a lib file called QuantLib-vc<VER>-mt.lib and a corresponding file with a -gd suffix. You need these libs as well as the headers.

Right-click the project and choose Properties. Now, make sure you choose All Configurations in the configuration drop-down list box on the top-left of the window. First, navigate to ConfigurationProperties→C/C++→General. In the Additional Include Directories, add the path to QuantLib’s directory. If you add the <QL Root>\QuantLib directory, you will be able to reference QuantLib headers with statements such as #include <ql/someheader.hpp>. If you did it correctly and you’re using VS2010, IntelliSense will kick in immediately, meaning if you type #include <ql/, VS will show you a helper list with headers (which is useful).

3. Configure library references

Now, navigate down to Configuration Properties→Linker→General. In the Additional Library Directories add the path to the QuantLib’s \lib directory.

Now move down to the Input header under the Linker header. Edit the Additional Dependencies field, prepending it with the name of the QuantLib library file — you need to specify the filename (without the full path) of the .lib file you need to include. The -mt.lib file is used for the Release build, and the -mt-gd.lib for the Debug build. If you mess these up, you'll get errors.

4. Run it

By now, assuming you have the boost libraries in your VS \include folder, the following should compile and run without problem:

//first line is required in .NET
#include "stdafx.h"
#include <iostream>
#include <ql/quantlib.hpp>

using namespace std;
using namespace QuantLib;

void main()
{
boost::shared_ptr<Money> m(new Money);
cout << m->value() << endl;

getchar();
}

-

How at ease are you with programming, dynamic linking, Makefiles and the rest?

In essence, there is no magic here. QL has a very liberal license, and you "merely" have to set up your project such that it finds

and both of those can be automated by calling quantlib-config with proper flags --clags and --libs, respectively.
from my linux shell, quantlib-config --libs prints out -L/usr/local/lib -lQuantLib telling me to g++ myQL.cpp -L/usr/local/lib -lQuantLib -o my_executable. Docs for quantlib-config helper program: linux.die.net/man/1/quantlib-config –  Pete Sep 12 '11 at 3:22