Asked  6 Months ago    Answers:  5   Viewed   34 times

A file named stdafx.h is automatically generated when I start a project in Visual Studio 2010. I need to make a cross-platform C++ library, so I don't/can't use this header file.

What is stdafx.h used for? Is it OK that I just remove this header file?

 Answers

91

All C++ compilers have one serious performance problem to deal with. Compiling C++ code is a long, slow process.

Compiling headers included on top of C++ files is a very long, slow process. Compiling the huge header structures that form part of Windows API and other large API libraries is a very, very long, slow process. To have to do it over, and over, and over for every single Cpp source file is a death knell.

This is not unique to Windows but an old problem faced by all compilers that have to compile against a large API like Windows.

The Microsoft compiler can ameliorate this problem with a simple trick called precompiled headers. The trick is pretty slick: although every CPP file can potentially and legally give a sligthly different meaning to the chain of header files included on top of each Cpp file (by things like having different macros #define'd in advance of the includes, or by including the headers in different order), that is most often not the case. Most of the time, we have dozens or hundreds of included files, but they all are intended to have the same meaning for all the Cpp files being compiled in your application.

The compiler can make huge time savings if it doesn't have to start to compile every Cpp file plus its dozens of includes literally from scratch every time.

The trick consists of designating a special header file as the starting point of all compilation chains, the so called 'precompiled header' file, which is commonly a file named stdafx.h simply for historical reasons.

Simply list all your big huge headers for your APIs in your stdafx.h file, in the appropriate order, and then start each of your CPP files at the very top with an #include "stdafx.h", before any meaningful content (just about the only thing allowed before is comments).

Under those conditions, instead of starting from scratch, the compiler starts compiling from the already saved results of compiling everything in stdafx.h.

I don't believe that this trick is unique to Microsoft compilers, nor do I think it was an original development.

For Microsoft compilers, the setting that controls the use of precompiled headers is controlled by a command line argument to the compiler: /Yu "stdafx.h". As you can imagine, the use of the stdafx.h file name is simply a convention; you can change the name if you so wish.

In Visual Studio 2010, this setting is controlled from the GUI via Right-clicking on a CPP Project, selecting 'Properties' and navigating to "Configuration PropertiesC/C++Precompiled Headers". For other versions of Visual Studio, the location in the GUI will be different.

Note that if you disable precompiled headers (or run your project through a tool that doesn't support them), it doesn't make your program illegal; it simply means that your tool will compile everything from scratch every time.

If you are creating a library with no Windows dependencies, you can easily comment out or remove #includes from the stdafx.h file. There is no need to remove the file per se, but clearly you may do so as well, by disabling the precompile header setting above.

Tuesday, June 1, 2021
 
Silfverstrom
answered 6 Months ago
78

The related_name attribute specifies the name of the reverse relation from the User model back to your model.

If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all().

If you do specify, e.g. related_name=maps on the User model, User.map_set will still work, but the User.maps. syntax is obviously a bit cleaner and less clunky; so for example, if you had a user object current_user, you could use current_user.maps.all() to get all instances of your Map model that have a relation to current_user.

The Django documentation has more details.

Tuesday, June 1, 2021
 
DCD
answered 6 Months ago
DCD
96

I have found this issue very easy to reproduce, and the fix for me is a variation on Richard Fors' answer. If I have a UserControl open in the designer, run the debugger, and then edit the UserControl, the subsequent rebuild will fail. If I close the UserControl before running the debugger I never get this error, so I just make sure to close the designer window before hitting F5.

Tuesday, June 22, 2021
 
braindamage
answered 6 Months ago
71

Updated Visual Studio documentation has more meaningful description:

Content - A file marked as Content can be retrieved as a stream by calling Application.GetContentStream. For ASP.NET projects, these files are included as part of the site when it's deployed.


Also after some testing and with a hint from What are the various "Build action" settings in Visual Studio project properties and what do they do?, I've found that the Content build action has this effect in WPF projects (possibly ASP too).

It adds

[assembly: System.Windows.Resources.AssemblyAssociatedContentFileAttribute("filename")]

to WpfApplication1_Content.g.cs. Read about the AssemblyAssociatedContentFileAttribute.

In console or WinForms applications, it does not do anything (neither in source code nor output binary).

Though in the comment, to previously mentioned question, there's a note about effect on deployment:

Also note that Content will be included when using one-click deploy, but None won't even if "copy if newer" is selected.

Possibly this works even for console and WinForms applications (I haven't tried).

Sunday, July 18, 2021
 
Jeff
answered 5 Months ago
73

This was working for me:

  1. C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEExtensions
  2. Find the correct subfolder. This can be tricky. I had a Xamarin folder, but that was not the right one. In my case it was folder wa13kl1p.m3s. However, open your folders and check it there is some Xamarin.Forms.Templates... files in it.
  3. Inside that folder navigate to the T/P/CSharp folder.
  4. Copy the Cross-Platform folder over to C:Users\DocumentsVisual Studio 2015TemplatesProjectTemplatesVisual C#

Same thing for the ItemTemplates:

  1. But this time navigate to T/I/CSharp folder.
  2. Copy the Cross-Platform folder over to C:Users\DocumentsVisual Studio 2015TemplatesItemTemplatesVisual C#
Friday, August 27, 2021
 
stand
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share