views:

272

answers:

3

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?

A: 

Update: Unfortunately, the C++ templates do not work in the same way.

I have left the text below as a reference to anyone who finds this, but it only works for C#/VB.


You can definitely edit the class template for C# (we have done it for exactly the same reason as you - to include a standard header), I would assume you can do it for C++ too.

Check out these two directories:

"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\"
"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\"

Somewhere in each of those directories will be a class template folder. For C# it's in a zip file here:

...\ItemTemplates\CSharp\Code\1033\Class.zip

Extract the zip, edit the template to include your header and re-zip it. You will also then need to place an unzipped copy under the ItemTemplatesCache folder, (following the same path - ...\ItemTemplatesCache\CSharp\Code\1033\Class)

There are more details here.

(Sorry, I'm on my Linux PC now so I can't check if these paths exist for C++. If you get it to work, post the correct C++ paths back here, and I'll update this answer to reference them)

Simon P Stevens
Unfortunately, the C++ templates do not seem to be organized in the same manner. Only the unit testing templates appear in either of those directories.
bsruth
@bsruth. Strange that. I've checked myself now, you are indeed right, the paths don't exist for c++. Sorry, I don't know how to do it in that case.
Simon P Stevens
A: 

Hi,

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.

Niklaos
Code Snippets are not supported in C++ ( http://msdn.microsoft.com/en-us/library/ms171421%28VS.100%29.aspx )
bsruth
+3  A: 

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

Mike Goatly