Showing posts with label VisualStudio. Show all posts
Showing posts with label VisualStudio. Show all posts

Tuesday, 12 August 2014

Set a single file to not use precompiled header

If you are working on a C++ project, occasionally you will come across one or more files that cannot include the precompiled header (often named stdafx.h). However when removing the include for this file you will get an error similar to the following:

"unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

Don't worry we don't have to resort to removing the precompiled header entirely. Doing so would often largely increase the compile time and in fact break compilation under certain conditions. Instead the preferred method is to opt an individual file out of the precompiled header.

We can do this by right clicking on the .cpp file under the Solution Explorer and selecting Properties. Next select C++ and then Precompiled Headers in the side menu.

Before we make any changes set the configuration combo select to All Configurations, otherwise any changes we make will only apply to you current configuration i.e. Debug or release and as soon as you switch between them, compilation will fail again.

You should see one of the options listed is Precompiled Header which should be set to Use (/Yu). We need to set it to the Not Using Precompiled Headers option to tell Visual Studio that this .cpp file will no longer be making use of the precompiled header when it is compiled.

Note that the only file that should be set to Create (/Yc) is the precompiled header .cpp file (usually stdafx.cpp). This is the file that visual studio will compile first in order to generate the precompiled header.



After making these changes the above error should be resolved and your code will hopefully compile.

Debugging native code from .NET

Occasionally I've been caught out where Visual Studio won't seem to let me step into a native method that I've Pinvoked from a managed language. I thought it might make a decent blog topic as the answer isn't exactly what it seems. Often your first reaction is to assume that either Visual Studio doesn't support this, or if you know its possible you may be left pondering why it is currently not working.

Fortunately it is often simply that by default when debugging a C# project, Visual Studio only attached a managed debugger meaning it cannot debug native code. To enable mixed debugging, you simply have to enable native code debugging.

To achieve this right click on your C# project in the Solution Explorer and select Debug from the side menu. Right at the bottom you should see a sub-heading entitled Enable Debuggers.

Simply tick Enable native code debugging, re-run the debugger and hopefully stepping into native methods should work fine again. Also any valid breakpoints in native code should fire too.



Setting tab options with Visual Studio 2013

Most shared code projects demand that you follow their convention when it comes to Tab spacing, i.e. whether to use tabs or space characters and how much indentation to use. With Visual Studio you can change this setting on a per language basis. This means that if you work on different projects across programming languages that require different settings you won't get caught out.

To alter this setting go to Tools -> Options and select Text Editor from the side bar. You can then select All Languages to set a global tab setting across languages or select a specific language to change. Once an option is selected, the Tabs option will display the settings available.

Here I am demonstrating my Tab setting for C#:


As you can see the main options I am interested in are Tab and Indent size which set how many white space characters a tab represents. Also you have the option of using Tab characters or inserting white space characters into the document when the Tab key is pressed. The number inserted will be resolved using the Tab and Indent size options.

As long as you remember to set this up correctly you should never find yourself falling foul of your projects tabbing conventions.