Share via


Native C++ Code Coverage reports using Visual Studio 2008 Team System

The code coverage tool in Visual Studio 2008 Team System is quite easy to use from within the IDE unless you want code coverage for your native C++ code. In order to generate a code coverage report for native C++ you have to use the command line tools. This is how you do it:

  1. First of all your project must be compiled using the /PROFILE link option. If you bring up your project properties it can be found here:
    Configuration Properties -> Linker -> Advanced -> Profile
  2. The profiler tools can then be found in the following directory:
    C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools
  3. You need to add some instrumentation code to your EXE or DLL file and that is done with this command:
    vsinstr.exe <YOUR_EXE_OR_DLL> /COVERAGE
    This will copy the original file to an ".orig"-file and create a new file with the original name that contains instrumentation code needed to gather coverage data.
  4. Now start the listener with this command:
    VSPerfMon.exe /COVERAGE /OUTPUT:<REPORT_FILE_NAME>
  5. Now run your EXE or some test suite that uses the file you want to calculate coverage for.
  6. The listener started in step four (4) will not stop by it self once your test suite is finished so you have to stop in manually using this command (from a second command prompt):
    VSPerfCmd.exe /SHUTDOWN
  7. When the listener has stopped you just drag-n-drop the created ".coverage"-file into Visual Studio and you can view the results.

Comments

  • Anonymous
    November 18, 2008
    So now you know how to get coverage reports for native C++ using Visual Studio 2008 Team System (if not
  • Anonymous
    December 04, 2008
    Thanks,This was extremely helpful and your instructions were spot on.CheersKaran
  • Anonymous
    January 06, 2009
    Nice work.  This really helped.One thing that I have found:  you can seemingly get overall numbers in Release builds, but you will get more itemized results (per class/method) in Debug mode.  Is this correct?
  • Anonymous
    January 06, 2009
    @ Kirk:I guess your observation is correct, typicaly since releasebuilds are optimized while Debug builds are not (since you in Debug builds want to preserve the code structure to make step by step debugging meaningful).
  • Anonymous
    January 08, 2009
    I think the tool for instrumenting is vsinstr.exe rather than vsinst.exe.  That's what I've got on my machine, anyway.Eric
  • Anonymous
    January 08, 2009
    Eric, you're right. I have updated the text.
  • Anonymous
    March 18, 2010
    I found out  to get the code coverage with visual studio for c++, I have to use the two files "vinstr" and "vsperfmon". I cannot find these files in my local installed version of visual studio ("performance tool" is not existing).Where can I get these two files?andreas.bittel@cdn-automotive.comtiaRegardsAndreas Bittel
  • Anonymous
    March 18, 2010
    @Andreas Bittel: Assuming VS2008 you need either the team suite or the test version (I think the code coverage is part of the test version as weel as the team suite). You cannot use any version of VC++.I guess that even if you have the team suite, there is a possibility to not install the code coverage tool too.
  • Anonymous
    April 28, 2010
    Thanks for the explanation.I finally got it setup in my project. My project has multiple unit tests, with each test file compiled into an executable.  After I run those files, I got a single .coverage file. When I loaded into Visual Studio, it displays the coverage under each executable. I could not find a way to display coverage in a merged manner. For example, a.cpp have a coverage of 20% under test1.exe, 30% under test2.exe. I am interest to see the overall coverage of a.cpp.
  • Anonymous
    April 28, 2010
    @Sherwood: Sorry no experience there. I'm using Bullseye for coverage wich has a better way of showing a merged result.
  • Anonymous
    May 09, 2010
    Hi when i am running the 5th step i am getting error saying"This application has failed to start because VSCover90.dll was not found.Re-intalling the application may fix this problem."How to fix the above error.RegardsPavan
  • Anonymous
    May 17, 2011
    The comment has been removed
  • Anonymous
    July 25, 2013
    I tried to get code coverage of my C++ exe that use my few dlls.Followed above steps. Expected the coverage report to show the blocks covered in both modules(exe and dlls) but it shows only the blocks covered in exe. Is there anything additional should I do to get the dll module coverage in that report?
  • Anonymous
    July 25, 2013
    @Anand: Don't know. This is a 5 year old post. I don't even know which version of VS you are using. Did you do step #3 for all your DLLs? Otherwise the VS forums or stack overflow might be a good place to search.
  • Anonymous
    July 25, 2013
    Thanks for quick repily!I'm using VS2010 and did step #3 for all my dlls. I did quick search in stack overflow but no luck.Any tips would be great help.
  • Anonymous
    July 25, 2013
    I am using VS 2010. Code covergage report shows blocks covered and their percentage. Is there anyway I can see lines of code covered and their percentage in the report.I opened the report in VS 2010.
  • Anonymous
    July 31, 2013
    @Anand; Not sure if lines are available for C++ coverage. Doesn't really matter since you can have lines with a lot of statements or spreat a statement over several lines. Blocks is really the only kind of useful number anyway for you.I'm sorry StackOverflow did not show anything because I really don't have an idea what could be wrong of all DLLs are instrumented but still not showing coverage. Maybe you can try instrumenting one DLL/EXE at a time and get coverage for each of them in separate runs. If that doe snot give you DLL coverage numbers I suspect the EXE is using DLLs from a different location than you think.