Friday, June 21, 2013

Add your own folder structure in your VSC++ Solution

When you first create a new project(of course a solution is created at the same time) in Visual Studio, a single default folder is added. But if you are building a big project containing many .h and .cpp files, it will be more organized if you could create your own folder structure and throw the .h and .cpp files into different folders accordingly. While gaining a more clean and logic folder structure, referring to .h and .cpp files in different folders will become a new issue.

Before adding your own folder structure, all the .h and .cpp files in the same project are put in one single default folder. And you can freely add any header files you want by using #include "headerfileName.h".  But now you couldn't. So one possible solution is to put the absolute path of your project under Properties->Configuration Properties->C/C++->General->Include additional directories.  The negative side of this method is that others trying to run your program need to reconfigure the absolute path since the absolute path in your machine is not necessary the same as that in your frined's. It will become a trouble if your friend doesn't want to configure such basic environment parameters. So, a more favorable solution is to use relative path which is independent of the terminal on which your program runs.

Configuring a relative path will become a piece of cake if you understand some basic rules. And the basic rules turn out to be really basic and simple enough. Putting one dot "." in Include Additional Directories meas going to the folder where the "ProjectName.vcxproj" is located. Putting two dots ".." in Include Additional Directories means going one step up from the folder where the "ProjectName.vcxproj" is located. Now you can refer to the .h and .cpp files in your own folder structures by using #include <folder/subfolder/fileName.h>.

Wednesday, June 12, 2013

Set and Vector

<Set>
1. more efficient to add and move objects from the set.
2. keep all the elements in order by itself.
3. No duplicate elements.

<Vector>
 1. more time is required to add and move.
2. keep elements in the order you inserted them.
3. Possible to have duplicated elements.

When inherited from a class with virtual methods, those virtual method are not inherited automatically; you still need to declare virtual methods in the child classes.

Return Reference and Return Value

1. It is a good thing to return the reference of objects instead of a copy of the object. This is done to save RAM resource.

2. Returning reference is most used in operator overloading += -= + and so on.

Thursday, June 6, 2013

Compile and Run Quantlib1.2.1 Examples on VS2010

Steps to compile and run Quantlib 1.2.1 on VS2010:

1. Download Boost 1.53.0 to your local system.
A link to Download Boost 1.53.0: http://www.boost.org/users/download/

2. Download  Quantlib 1.2.1 to your local machine and run the VS2010 solution in Quantlib folder.
A link to downloading Quantlib: http://sourceforge.net/projects/quantlib/files/QuantLib/1.2.1/

3. Select the first project in the solution, Right click ->Properties->Configuration Properties -> C/C++ -> General-> Additional Include Directories.
Add the full path of the boost folder here. In my laptop, it is E:\Program Files\Boost\boost_1_53_0\boost_1_53_0.

4. Repeat step 3 for all the projects and everything is done. Now Quantlib can compile and run in your machine now.

Attention:
1. Add getchar() before the end of each main method so that you could see the information display on the terminal. If not, the terminal will just pop out and then disappear.
2. You also could write your own examples to use the Quantlib library. Just reverse-engineer the examples.
3. Quantlib 1.2.1 can now only support VS2010. You will get an "unknown Microsoft compiler" error if you use VS2012.

Best,

Link Static Library to Console Project in VSC++

This is the second time I conquer a configuration issue in building my C++ pricing library for quantitative finance. The problem is:

1.   I create a solution named P2 in VS2012 and then add one console project named TestSuite and another static library P2PricingLibrary to the solution. After choosing TestSuite as the startup project and run the whole solution I get fatal error LNK 1120 and LNK2109 unresolved externals.  It seems I have to add additional dependency for my TestSuite. Then I go to Properties->Configuration Properties->Linker->Input->Additional Dependency and enter the full path of P2PricingLibrary.lib.

So, to link your library to other projects, you always need to link the path of the library in addition to the #include statement.

It should be working now.  But it isn't.


2.   I get an fatal errror LNK1104 ": cannot open file  'C:\Users\JinhuaHuang\Desktop\P2\P2PricingLibrary\Date.obj ". This time it turns out to be that  theP2PricingLibrary.lib has been generated yet.
When I build the P2PricingLibrary, the following error pops out:
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source.  Because I select using Precompiled Header files for the P2PricingLibrary, I need to add #include "stdafx.h" to my cpp files, particularly, the first line of my cpp files. After fixing this. I rebuilt the library and then run the solution. It works!!