Archive

Archive for the ‘Visual Studio’ Category

How to make use of Property Sheets for better solution management for C++ projects in Visual Studio

Setting up a C++ project can take a lot of time and often we are setting the same properties for a lot of projects, so isn’t there a solution to this problem?

Maybe create a custom Project-Template and always create the projects from this template?

Would be feasible but we would need a lot of templates. But what happens if we have to change a property after the project was already created?
We would have to change it for all project by hand …

But there is a a better solution in visual studio for these kind of problems, Property Sheets.

This can help with managing large solutions, but a lot developers don’t know about this feature, because it’s hidden away under the “Other Windows” menu in the shell.

With this post I want to let more people know about this great “hidden” feature of Visual Studio for C++ projects.

What is a Property Sheet?

A Property sheet simply is a .xml file with the file extension .props in Visual Studio 2010 and .vsprops in older versions (2005 and 2008). These file contain property definitions which can than be added to a project so the Properties are automatically set on all projects using the Property Sheet.

But Property Sheets do not contain different configurations, because this would destroy the ease of use.
If you need different settings for different configuration just create multiple Property Sheets.

A Project can use any number of Property Sheets.

What can they be used for?

Property sheets can be used to solve a lot different problems.

I’m using them to place the settings for the different configurations (Debug, Release, Production) outside of the actual projects so I have only one file containing the debug properties, one for release and one for production.

Now every project just has to add these Property Sheets and they are ready to be compiled. With the same settings as every other project in the solution. Using the same Compiler Warning Level, optimization settings, Linker settings and so on.

An other possible use case would be one property sheet for every  external dependency your code is relying on.
For example when we are using boost, we could create a property sheet which contains just the path to header and library directory. So every project which would like to use boost just has to add this property sheet and not have to define the path itself.

Defining a path can take some time, keep in mind if you are compiling for x86 and x64 you have to use different libraries.
So your path has to be defined in a way, it will link to the correct location for the chosen platform which can easily contain an error. If this is the case and you are using a Property Sheet, there is just one place you need to change whether it’s being used in 1 or 20 projects.

These are just some examples, surely there are more problems they can help, just be creative.

How to use?

We talked about the what and why, but now it’s actually time talk about how we actually can use Property Sheets. I will show how to use them in the Visual Studio 2010 Beta 1, but nothing really changed since Visual Studio 2005( in this area) so they versions are mostly equal.

Everything starts with finding the correct window. It’s hidden away under
“View” –> “Other Windows”. There you will find the “Property Manager”.

image

clip_image001[5]

You will now see this unimpressive window. This is the Property Manager.

It contains every project in the Solution with a “Folder” for every Platform/Configuration Combination. Like Debug/x68 and Debug/x64.

There are some buttons, the first one (from left to right) opens the properties for the currently selected item in the list. If the project is selected, the configuration window for the project will be opened.
If a property Sheets is selected, the properties for the selected Sheet will be opened.

When opening the Properties of a Property Sheet you  have to pay attention that just the values which are bold are actually defined in the sheet. The inherited values are also visible but they are not bold.

clip_image001[7]The second button adds a new, empty Property Sheet. The button right from it, adds a already existing Sheet to the project.

With the arrays you can move the priority. The Property sheets inherit values from each other. The Property Sheet which is placed at the top has the highest priority and will override the settings from Property Sheets placed below.

My advise would be to open the Property Sheet in an editor after you have made changes and see if just the value you wanted to set are actually set.

If you have changed any values in a Property Sheet, my advice would be to open the file in an xml editor and have a look at the properties you have changed. So you can be sure that have set the correct values.

Make sure to always save the changes, as they will not be saved automatically.

Some Pitfalls

There are some pitfalls you have to keep an eye on.

The first pitfall is that changes to the Property Sheets are not saved automatically. You manually have to save every Property Sheet you have changed. If you are changing a lot Property Sheets it can happens quickly that you have forgotten to save the changes and you are left wondering, why nothing changed.

My Solution for this problem is: I always close the solution and reopen it after I made any changes. This way Visual Studio will ask, If you want to save you changes and will save all Property Sheets automatically. This takes some seconds but it’s a lot faster than recompiling you project just to find out that you have forgotten to save the changes.

The other one is actually a problem with the UI.
It’s really hard to see how the values for the final project are brought together.
There is no other way to see if a Property Sheet overrides settings from any other Sheet without opening both. It’s also not possible to have two Property Windows open at the same time. Which doesn’t make it any easier, as well.

Conclusion

So i hope I could show the potential of Property Sheets for better Project Management in C++ projects and I also could warn you about the potential problems you may encounter when you are using Property Sheets for the first time.

I think this is a great feature and deservers a better handling in the UI than it currently has. Nothing has changed in the Property windows since Visual Studio 2005 and I think it’s time for a rework of those windows.

Thanks for reading and I hope you learned something new.

Categories: C++, Visual Studio, all

Visual Studio 2010 Beta 1, first experiences

Today I finally had the chance to install Visual Studio 2010 Beta 1 on my PC. I had planned to it yesterday but the download rate got extremely slow yesterday, at the end it was at 7 kb/s and I stopped the download.

But now lets talk about my first experiences with the new Visual Studio 2010.

Visual Studio 2010 Startpage

After installing it, which took some time, I firstly started it. The new Start page is really a lot better than the old one, but the best part is it’s customizable. Have a look at the following links if you are interested in it:

I haven’t had the time myself to customize my startpage. I really firstly wanted to get a look at all the new features in VS2010.

For further testing I created a new Win32 Console Application. Having a look at the Default Settings I thought, lets try to import my setting from Visual Studio 2008. It worked without any problems. The Coding environment changed and everything looked like it had looked in 2008.

After I had restarted Visual Studio, the background color was back to white… This is the first bug I encountered in this Beta. You have to open up the “Colors and Fonts” option menu and just press “OK”, then everything will look normal again.

My IDE, showing my settings.

I already had read that intellisense for c++ had been improved. So i just started typing and directly found the first new feature in C++ intellisense.

#include file will now be checked, and will get a red wavy line under it, if the file couldn’t be opened.

include error line

include without error

One screenshot showing how intellisense looks by default.

intellisense

I made a change to the C++ settings for intellisense. Under “options”, “Text Editor”, “C/C++” i activated “Auto List members”, and “parameter information”. So I don’t have to hit a key to let intellisense show, I always want it to show up.

C++ Text Editor options

Now I wanted to test if anything had changed to the UnitTesting support for C++ projects. I only found a small difference. Under Visual C++ is now a TestProject which can be used directly, everything hasn’t changed and works like it does in Visual studio 2008. If your are interested in Unit Testing have a look at my post about it here.

Intellisense not available, for C++/CLI

Sadly intellisens doesn’t work in the test project.

I also found some changes which make debugging a bit faster. I don’t will talk about the Multithreading additions here I haven’t had the time to play with it. But that’s up next.

When hovering about a variable you get, the normal window showing the values as usual.

clip_image001[20]

But at the right side you can see a small node icon. Below is the icon highlighted in yellow.

clip_image001[22]

If you press this icon a yellow floating window will appear.

clip_image001[24]

clip_image001[26]

clip_image001[28]

This window shows the values of the variable as if you would hover about it. But you don’t have to. This window will update each time the variable updates. A matrix has a lot of values not all can be shown in the small window. But press the small "”+” at the left and all value will appear. Each value now has this small icon node icon at the right. If you press this button the value will be added to the floating window. (As in the window on the right)

All these setting will be saved and the next time you start debugging it will show up exactly with the same setting you left the last time

So that it. These are my experience i wanted to share. I will continue playing with it and write about my experiences.

Categories: Visual Studio

Unit Testing C++ with the Visual Studio Unit Testing Framework

As written in a previous article I was using WinUnit(http://msdn.microsoft.com/en-us/magazine/cc136757.aspx) for my Unit Tests in C++. At this time I was quite happy with the features the framework provided, but as I’m working more and more with it, I was more and more missing the direct integration in the IDE, the Visual Studio Unit Testing Framework provided.
Which is my Unit Testing framework of choice when writing managed code.

The integration in the IDE is really a big plus. Just having to press a key combination [add key combination] and having to run the test again makes a TDD style of programming much more productive and fun. Also debugging the Unit Test involved setting up the test project to use a custom tool for debugging. And than manually having to say “Debug”->”Start new instance” really slowed me down.

So once a day I read a post by Jamie Fristrom from Torpex Games,he had a problem using VSTest for a c++ project. (I sadly couldn’t provide any help). He talked about how easy it is to use unmanaged code with it. I started to try it out myself because, you already know, I like it a lot in managed code.

In the following I want to talk about how to get everything in place to test unmanaged c++ code with VSTest.

Creating a c++ test project

Finding the place where a test project for c++ code can be found, is the first challenge. Normally you would go to “File” –> “New” –> “Project” and there search for “Test Projects”. From here you can create a “Test Project”. But this is just a C# project and there is no way to change it to be a c++ test project.

So what now?

There is a way to create a c++ test project. But it’s a little bit hidden in the menus of Visual Studio.

 image

The only way, I know of, is hidden in the Test Menu of Visual Studio. When you open the Test dropdown menu, you see the option “New Test…”. If you click this you will get the window show below.

clip_image001

In this window you have the possibility to create new Unit Tests. To create a C++ test project you have to open the drop-down menu “Add to Test Project:”
In this menu you have the possibility to add a new test to an existing test project. In my case I have a test project, called “TestProject2”.

But we want to create a new “Visual C++ test project”, so we choose this option. After clicking “OK” we will be asked for a name of the project which will be created.

The project will be created and the UnitTest1.cpp file will be opened. This is our starting point for the newly created project.

The project is ready to be used and will compile. But we have to change the properties for this newly created project a little bit, to make it usable.

Open the properties page for the newly created test project.

image

 

 

 

 

 

 

 

 

 

 

 

Under the general tab, you will find the “Common Language Runtime support” option. This will show, by default, “Safe MSIL Common Language Runtime Support “. This option has to be changed because if this option is set to  “Safe …”, we cannot test any unmanaged code not also compiled with “/clr:safe”.

So change this option to  “Common Language Runtime Support (/clr)”.

Test C++ code

We got the most work done, now it’s finally time to get c++ code tested.

C++ code, cam be compiled in three different forms, as you already know. It can be compiled as .lib, .dll and .exe
All three forms can be tested using VSTest, but a application(.exe) is more limited.

The table below shows the features support for each type, once when compiled with CLR support and once without.

clip_image001[1]

As you can see just a .exe application doesn’t support stepping into the code. I always try to find the reason a test failed without having to debug, but if i really can’t find the problem I have to recompile the file as a lib.

As I’m talking about, there is one thing you have to do when trying to test code in .exe projects. You have to define the members you want to test with __declspec(dllexport). Like you do when compiling a .dll project. This has to be done, so we can link to the .lib file which will be created now.

Having linked the code, testing is easy. The c++ code can now be used like normal. Just look at the small example below.

Calculator calc;                      
int result = calc.AddInteger(5,5);    
Assert::AreEqual(10, result);         

But not everything is that straight forward, when working with c++ strings the hassle begins. If you are using c++ string in your public API’s have a look at http://www.codeproject.com/KB/string/StringConvertor.aspx

I’m new to testing c++ code in VSTest, this article just represents what I have found out and doesn’t have to represent the best way possible to achieve the result. So if you are testing c++ code with VSTest already and you have some tips and improvements I would like to here them.

Categories: UnitTesting, Visual Studio

Tips for Visual Studio 2008 Solution Configuration (C++)

February 6, 2009 Jendrik Illner 1 comment

Tip 1: Use relative paths instead of absolute ones.

When defining an include path in Visual Studio by default an absolute path will be used. So take the time and define the relative path.

Have a look at the different macros available to make your life easier at http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

Also have a look at the macro menu showing you what each path will be exactly. This menu can always be accessed when a path gets defined in the properties of a project.

image

 

Tip 2:  Take care that project properties are saved for all platforms and configurations.

Visual Studio allows a solution to build for a lot of platforms and configurations, just take a small example:

  1. Win32 Debug
  2. Win32 Release
  3. x64 Debug
  4. x64 Release

These platform + configuration combinations are very common. So you got already 4 different combinations you have to define the the directories and dependencies for.

So take this example a step further you have lets say 10 projects so you have manage combinations * project = numberOfConfigurationsToManager. This results in  40(!) configurations to take care of.

So always try to configure your projects using [All Platforms] + [All Configurations]. Take at the pictures below to see where you can define those.

configuration1 all_platforms all_configurations

 

Tip 3: When a new project gets added to a Solution it will not contain non default platforms or configurations. Always remember to add them to the project directly after you added it to the solution.

Categories: Visual Studio, all Tags: ,