The website of Lei Chen

Sunshine and rain of a developer

Archive for the ‘CPP’ Category

Virtual Studio Smart Operator ‘=’. Add Spaces Around Every ‘=’.

Posted by hide1713 on June 22, 2009

I find it’s really hard to extend the environemnt under VS 2008. Anyway, I have to learn some VB.

I really like the SmartOperator.el in Emacs. When I press ‘=’ key, it automatically insert two spaces around ‘=’.  The following code can do the same thing. You need to create a new macro in VS and put the code in the new macro and bind ‘=’ key to this function.  I hope the code could inspire someone.


Sub space_around_eq()
 Dim s As TextSelection = DTE.ActiveDocument.Selection
 s.CharLeft(True, 2)
 Dim selection = s.Text
 If selection.StartsWith("=") Then
 DTE.ActiveDocument.Selection.Text = "== "
 ElseIf selection.EndsWith("-") Or selection.EndsWith("+") Or selection.EndsWith("*") Or selection.EndsWith("=") Or selection.EndsWith(" ") Then
 DTE.ActiveDocument.Selection.Text = selection.ToString() + "= "
 Else
 DTE.ActiveDocument.Selection.Text = selection.ToString() + " = "
 End If
 End Sub

Posted in C, CPP | Leave a Comment »

Configure Boost library in Visual Studio 2008

Posted by hide1713 on June 18, 2009

This tutorial teaches you how to setup boost library in Visual Stuido 2008. There’s a perbuild version you can download from Boost pro . The perbuild package can save you lots of time and effort. Believe me.

The boost pro downloader is not smart enough to detect old version; therefore, I highly recommend you install all the packages. There are many dependency within boost library. For example, the boost filesystem is depend on boost system and boost regular expression. If you miss one of them, you have to install the boost library again :(

Now, let’s setup Visual Studio so that it can find the file and library. Go to Tools->Option->Projects and Solutions->VC++ Directories.

Select “Include files” and Add a new directory. In my computer, it is

C:\Program Files (x86)\boost\boost_1_38\

Select “Library files” and Add a new directory

C:\Program Files (x86)\boost\boost_1_38\lib

That’s it.  Enjoy your life with Boost

Posted in CPP | Tagged: | Leave a Comment »

Intergrade CPPUNIT in Visual Studio 2008

Posted by hide1713 on June 15, 2009

This tutorial helps you configure the CPPUNIT  unit test environment in Visual Studio 2008. If you have any questions, please look at the trouble shooting section at the end of tutorial.

Firstly, There’s a bug in CPPUNIT. If you compile cppunit-1.12.1 under VS2008.You will either fail to compile the code or generate cppunit library without correct debug information.

Let’s fix the bug first. According to this post

I just tried to compile CPP Unit Under Visual C++ 2008. Compiler. There was one problem When I tried to compile MFC Test Runner.

MsDevCallerListCtrl.cpp

.\MsDevCallerListCtrl.cpp(67) : error C3505: cannot load type library ‘{80CC9F66-E7D8-4DDD-85B6-D9E6CD0E93E2}’

.\MsDevCallerListCtrl.cpp(67) : fatal error C1104: fatal error importing libid: ‘80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2′

CPP Unit using IDE Automation component to track the source lines in the MFCTest runner. Version cppunit-1.12.0 currently support till version 7 of IDE Automation. I’ve only version 2008 of Visual Studio installed in my machine. So just correct the version number in MsDevCallerListCtrl.cpp file

#pragma warning( disable : 4278 )

#pragma warning( disable : 4146 )

#import “libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2″ version(”9.0″) lcid(”0″) raw_interfaces_only named_guids

#pragma warning( default : 4146 )

#pragma warning( default : 4278 )

When you build the source code. You should see some assertion errors. That’s fine. They are not real errors, just some unit test code. Don’t worry about it. You should look into ./lib directory. There should be many files including cppunitd.dll, cppunit.dll,cppunitd_dll.pdb.

Now, you have code compiled. It won’t be difficult to setup right environemnt. Follow the money tutorial

In the following document, $CPPUNIT is the directory where you unpacked CppUnit: $CPPUNIT/: include/ lib/ src/ cppunit/First, you need to compile CppUnit libraries:

  • Open the $CPPUNIT/src/CppUnitLibraries.dsw workspace in VC++.
  • In the ‘Build’ menu, select ‘Batch Build…’
  • In the batch build dialog, select all projects and press the build button.
  • The resulting libraries can be found in the $CPPUNIT/lib/ directory.

Once it is done, you need to tell VC++ where are the includes and librairies to use them in other projects. Open the ‘Tools/Options…’ dialog, and in the ‘Directories’ tab, select ‘include files’ in the combo. Add a new entry that points to $CPPUNIT/include/. Change to ‘libraries files’ in the combo and add a new entry for $CPPUNIT/lib/. Repeat the process with ’source files’ and add $CPPUNIT/src/cppunit/.

Getting started

Creates a new console application (‘a simple application’ template will do). Let’s link CppUnit library to our project. In the project settings:

  • In tab ‘C++’, combo ‘Code generation’, set the combo to ‘Multithreaded DLL’ for the release configuration, and ‘Debug Multithreaded DLL’ for the debug configure,
  • In tab ‘C++’, combo ‘C++ langage’, for All Configurations, check ‘enable Run-Time Type Information (RTTI)’,
  • In tab ‘Link’, in the ‘Object/library modules’ field, add cppunitd.lib for the debug configuration, and cppunit.lib for the release configuration.

We’re done !

We are not done yet. There are three more things you should know.

1. By default, the cppunit is liked to Dll with /MDd option. Make sure other projects are using the same option. You will get many weird errors if you link other parts with static library /MTd

If you compile your code with MTd, you should link your code with cppunitd_dll.lib. otherwise, you should link with cppunitd.dll.

2. There are many ways to manage your unit test. You can setup  a subproject in a solution which generates unit test binary.  I spent few hours to figure out that I need to add cpp files to the unit test project so that  linker could find those functions.  If you see “Symbol unsolved” errors,  this is the right direction to look into.

3. Look at your project path. The default project path contain space. So Visual Studio postcompile command can not correctly execute if you follow the “Money” tutorial.

Posted in CPP | Leave a Comment »

Magic C++ comment trick

Posted by hide1713 on June 8, 2009

A fast swith to comment/ uncomment block of code


/*   <---
 return 1;
 /*/
 return 0;
 //*/

There are bugs in word press cpp highlight plugin. copy the following code to your IDE and try it yourself 

//*   <----

 return 1;
 /*/
 return 0;
 //*/

Posted in CPP | Leave a Comment »

c++ output wrap round styles

Posted by hide1713 on January 11, 2009

If you want to send a very long string to cout or something, there are 3 options for you.


//style 1
    cout<<"hello";
    cout<<"world";
//style 2
    cout<<"hello"\
    <<"world";
//style 3   
    cout<<"hello"
    <<"world";

Posted in CPP | Leave a Comment »