views:

556

answers:

2

T4 template files are automatically recognizable by the IDE under C# projects, but I have no clue on how they can be integrated into C++ projects (other than using make files).

Any ideas?

+3  A: 

T4 Template files are integrated into C++ projects the same way as a C# project. Create a new text file in your C++ project and give it a .tt extension. Then write your template as normal. A C++ project needs a little extra work to get it to transform the templates. The quick and dirty way I got it to work was to add a custom build step and have it call "C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.1\TextTransform.exe" directly. Another way I found was to add a custom MSBuild task. Instructions can be found here

This page has more information and some good links to other pages on using T4 code generation.

Logan5
Somehow it does not seem to work. Try creating a new Win32 C++ project. Then add a simple .tt file and click build. Nothing gets generated.
Filip
oops, you're right. Editing my answer.
Logan5
Thanks for your answer. It can be made to work, but is definitively not as convenient as C#. Hopefully this will change in the upcoming VS 2010.
Filip
+1  A: 

MSBuild Task will not work as this is a vcproj file (C++) so vcbuild is used. The easiest way to get the tt compiled is so add custom build step like below..

"C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\1.1\TextTransform.exe" -out $(ProjectDir)\VSProject.cpp -I $(ProjectDir) $(ProjectDir)\VSProject.tt

I spent several hours investigating the MSBuild Task solution above and it's pretty good for managed code but I can't see any way to use it for C++ (bar converting the vcproj to csproj eek)

Even with this solution, don't you still have to manually add the cpp file to the vcproj file?
MrSlippers