0

I've been trying to make my own template in VS for C++. I want to have a cpp and hpp file with the same name as the project. I have already read this link: How can I set a file name using a variable in a Visual Studio project template

Howerer, when using my template, the files are not in the project that I've created.

Here is my vstemplate file:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>My Template</Name>
    <Description>A test template</Description>
    <ProjectType>VC</ProjectType>
    <ProjectSubType>
    </ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <DefaultName>MyProject</DefaultName>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>__TemplateIcon.png</Icon>
    <PreviewImage>__PreviewImage.png</PreviewImage>
  </TemplateData>
  <TemplateContent>
    <Project TargetFileName="MyTemplate.vcxproj" File="MyTemplate.vcxproj" ReplaceParameters="true">
      <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.vcxproj.filters">MyTemplate.vcxproj.filters</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cpp">MyFile.cpp</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.hpp">MyFile.hpp</ProjectItem>
  </TemplateContent>
</VSTemplate>

What am I doing wrong?

Edit: Managed to figure it out, had to replace file names in vcxproj file itself. Thanks for helping!

aviad1
  • 354
  • 1
  • 10
  • `Project TargetFileName="MyTemplate.vcxproj"`->`Project TargetFileName="$safeprojectname$.vcxproj"`? And do you have the template files in the root directory of the template zip file? VS copies and renames the files, it doesn't create files out of nothing. – JHBonarius Sep 29 '20 at 08:00
  • Yes, of course I have the files there. I tried also using $safeprojectname$.vcxproj but then the solution is completely empty, with not even a vcxproj file in it. – aviad1 Sep 29 '20 at 13:48
  • Managed to figure it out, had to replace file names in ```vcxproj``` file itself. Thanks for helping! – aviad1 Sep 29 '20 at 20:50
  • Would you share what your changes to project file were? – user3461121 Dec 24 '21 at 23:53
  • @user3461121 I added an answer explaining what I did. I really should have done that way earlier, very sorry for that – aviad1 Dec 26 '21 at 02:35

2 Answers2

1

I ended up succeeding using the following method:

  • I added two files to my template, named $safeprojectname$.hpp and $safeprojectname$.cpp.
  • I edited my vcxproj file (using notepad++, or any other text editor for that matter), adding the following tags inside of the main Project tag:
  <ItemGroup>
    <ClInclude Include="%24safeprojectname%24.hpp" />
  </ItemGroup>
  <ItemGroup>
    <ClCompile Include="%24safeprojectname%24.cpp" />
  </ItemGroup>

I had to use %24 as $ is not a valid character in this context, and 24 is the ASCII (/unicode) value of that character.

aviad1
  • 354
  • 1
  • 10
0

Use the following method:

  1. Edit template file:
<ProjectItem 
  ReplaceParameters="false" 
  OpenInEditor="true" 
  TargetFileName="$safeprojectname$.cpp"
>
  task.cpp
</ProjectItem>

Replace the filename, not including the extension, with $safeprojectname$.

  1. Edit vcxproj file:
<ItemGroup>
  <ClCompile Include="$(RootNamespace).cpp" />
</ItemGroup>

Find the <ItemGroup> section with the file name and replace it, not including the extension, with $(RootNamespace).

File task.vcxproj.filters may be deleted.

Jack
  • 514
  • 3
  • 11
  • 30
Dmitry
  • 1