15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

396 ❘ ChaPTer 16 visuAl studiO 2010<br />

Debugger Symbols<br />

When you are debugging, you often have to look at values of variables, <strong>and</strong> you will specify them by their<br />

source code names. The trouble is that executable code generally does not contain those names — the<br />

compiler replaces the names with memory addresses. .<strong>NET</strong> has modified this situation somewhat to<br />

the extent that certain items in assemblies are stored with their names, but this is only true of a small<br />

minority of items — such as public classes <strong>and</strong> methods — <strong>and</strong> those names will still be removed when<br />

the assembly is JIT-compiled. Asking the debugger to tell you what the value is in the variable called<br />

HeightInInches is not going to get you very far if, when the debugger examines the executable code, it<br />

sees only addresses <strong>and</strong> no reference to the name HeightInInches anywhere. Therefore, to debug properly,<br />

you need to have extra debugging information made available in the executable. This information includes,<br />

among other things, names of variables <strong>and</strong> line information that allows the debugger to match up which<br />

executable machine assembly language instructions correspond to your original source code instructions.<br />

You will not, however, want that information in a release build, both for commercial reasons (debugging<br />

information makes it a lot easier for other people to disassemble your code) <strong>and</strong> because it increases the size<br />

of the executable.<br />

Extra Source Code Debugging Comm<strong>and</strong>s<br />

A related issue is that quite often while you are debugging there will be extra lines in your code to display<br />

crucial debugging-related information. Obviously, you want the relevant comm<strong>and</strong>s removed entirely<br />

from the executable before you ship the software. You could do this manually, but wouldn’t it be so much<br />

easier if you could simply mark those statements in some way so that the compiler ignores them when it is<br />

compiling your code to be shipped You’ve already seen in the first part of the book how this can be done<br />

in <strong>C#</strong> by defining a suitable processor symbol, <strong>and</strong> possibly using this in conjunction with the Conditional<br />

attribute, giving you what is known as conditional compilation.<br />

What all these factors add up to is that you need to compile almost all commercial software in a slightly<br />

different way when debugging than in the final product that is shipped. Visual Studio is able to consider<br />

this because, as you have already seen, it stores details of all the options that it is supposed to pass to the<br />

compiler when it has your code compiled. All that Visual Studio has to do to support different types of<br />

builds is to store more than one set of such details. The different sets of build information are referred to as<br />

configurations. When you create a project, Visual Studio automatically gives you two configurations, called<br />

Debug <strong>and</strong> Release:<br />

➤<br />

➤<br />

The Debug configuration commonly specifies that no optimizations are to take place, extra debugging<br />

information is to be present in the executable, <strong>and</strong> the compiler is to assume that the debug preprocessor<br />

symbol Debug is present unless it is explicitly #undefined in the source code.<br />

The Release configuration specifies that the compiler should optimize the compilation, that there<br />

should be no extra debugging information in the executable, <strong>and</strong> that the compiler should not assume<br />

that any particular preprocessor symbol is present.<br />

You can define your own configurations as well. You might want to do this, for example, if you want to set<br />

up professional-level builds <strong>and</strong> enterprise-level builds so that you can ship two versions of the software. In<br />

the past, because of issues concerning the Unicode character encodings being supported on Windows NT<br />

but not on Windows 95, it was common for C++ projects to feature a Unicode configuration <strong>and</strong> an MBCS<br />

(multi-byte character set) configuration.<br />

selecting a Configuration<br />

One obvious question is that, because Visual Studio stores details of more than one configuration, how does<br />

it determine which one to use when arranging for a project to be built The answer is that there is always<br />

an active configuration, which is the configuration that will be used when you ask Visual Studio to build a<br />

project. (Note that configurations are set for each project rather than for each solution.)<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!