How to use T4 code generation templates with VS C++ projects?
Asked Answered
H

4

11

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?

Higgledypiggledy answered 30/1, 2009 at 16:42 Comment(0)
F
16

T4 Template files can be integrated into C++ projects, but it's a bit more work than with a C#/VB 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 then needs further 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.

Fighterbomber answered 2/2, 2009 at 6:26 Comment(2)
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.Conjunct
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.Conjunct
H
6

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 to add a 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)

Hyoscyamine answered 27/6, 2009 at 0:10 Comment(1)
Even with this solution, don't you still have to manually add the cpp file to the vcproj file?Ludwog
M
5

For Visual Studio 2017 (and maybe 2015?)

The commands listed in the other answers are partly incorrect, as the file TextTransform.exe has been moved to a new directory: the devenv root folder, e.g.:

C:\Progra~1\Visual Studio 2017\Enterprise\Common7\IDE

The command should now be updated to the following:

"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)xxx.cpp" "$(ProjectDir)xxx.tt"

where xxx is the file name of yout .tt template file

Manvell answered 19/7, 2018 at 17:35 Comment(1)
C:\Program Files...\Common7\IDE is under the DevEnvDir environment variable in the developer console if anyone's looking.Circuity
E
0

Having experimented with some of the above (and found them unsuitable for my specific case), I took a different approach that others may find helpful.

Rather than fight to get VS to accept a T4 template within a C++ project, I added a separate (empty) C# project to my solution. I put my template file at the root of the solution and add a link to it from the C# project. I #include the generated .h file from my C++ project.

I used the template to generate a version string that updates with every build. I added the AutoT4 tool to VS so that the string is updated with every build of the solution.

Embryology answered 9/3, 2020 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.