Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a simple comment header template for all new classes in Visual C++ 2010?

This may be a duplicate, but I haven't found anything that answers it thus far. My company passed a resolution that all files need to have a boilerplate comment header, with file name and copyright date among other things. I was hoping there would be an easy way to just create a header template that is added to the top of every new class (.cpp and .h files) added to the project with a couple of variables that are replaced based on the date, file name, etc.

Unfortunately, it seems like this is a much larger task that it seems it should be. I've looked into Manipulating Code using the Visual C++ Code Model and Manually Creating an Item Template and can't seem to get any of them to do what I want.

Sorry if this sounds like a "do my work for me" post, but to me this just isn't worth spending that much time on. If it's going to take a day to figure out the subtleties of extending Visual Studio, I can just manually add and edit the header for each new file, as it isn't done that often. Is there an easier method than those I was looking at, or a simple example on how to utilize those methods for my purpose?

like image 805
bsruth Avatar asked Feb 23 '10 22:02

bsruth


People also ask

How do I create a class template in Visual Studio?

The default location is %USERPROFILE%\Documents\Visual Studio <version>\Templates\ItemTemplates. Close Visual Studio and then reopen it. Create a new project, or open an existing project, and then choose Project > Add New Item or press Ctrl+Shift+A. The item template appears in the Add New Item dialog box.

How do I create a header in Visual Studio?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.

What are header files in C#?

h extension. Header files contain the function prototypes or function declaration, whereas the source code contains the constants, macros, system-wide global variables. Whenever we require the definition of a function, then we simply include that header file in which function is declared.


2 Answers

This may not get you any further than you have already got, but Simon is close in that you can create C++ file templates in the way that he suggests, but the path to the C++ templates is C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcprojectitems (note that on 32-bit machine it will just be Program Files).

If you edit the file NewC++File.cpp, the next time you add a new C++ file to a project your template will be used. Alternatively you can create your own files in this folder and they will appear in the Add New Item dialog.

This won't solve your problem around inserting the current date in the header (assuming you want that to be automatically determined), but you could update the template that you're using once a year, and that would be slightly less of a chore.

HTH

like image 89
Mike Goatly Avatar answered Oct 13 '22 01:10

Mike Goatly


What you are looking for is called : Code Snippets

I personnaly use the snippets provided into VAssistX but it's a shareware so it's might not be a good solution for your company. By the way if you are developing application on Visual C++ without VAssistX you are wasting a lot of time ;)

There is also a code snippets manager into Visual Studio, i never used it but i found some documentation on google :

http://msdn.microsoft.com/en-us/library/d60kx75h(VS.80).aspx

and

http://blogs.microsoft.co.il/blogs/gilf/archive/2009/01/17/how-to-write-your-own-code-snippets.aspx

I hope it's will be helpfull.

like image 43
Nicolas Guillaume Avatar answered Oct 13 '22 00:10

Nicolas Guillaume