【问题标题】:How to change the default C++ template file?如何更改默认的 C++ 模板文件?
【发布时间】:2019-04-09 15:48:56
【问题描述】:

我使用的是 Visual Studio 2019,每当我创建一个新的 C++ 项目时,它都会为我提供一个包含以下代码的默认文件:

// Template Test.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

int main()
{
    std::cout << "Hello World!\n"; 
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

这是太多不必要的信息,需要一分钟才能将其更改为我真正想要的;

// Template Test.cpp : This file contains the 'main' function. Program execution begins and ends there.


#include <iostream>

using namespace std;

int main() {
    cout << "Hello World!\n"; 
}

不需要太多时间,但每次在 VS 19 中创建新的 C++ 控制台项目时都必须这样做。

在我的电脑上快速搜索后,我在以下位置找到了一个名为“模板”的文件夹:

C:\Users\yale\Documents\Visual Studio 2019\Templates

这个文件有子目录,让我觉得它是正确的地方,但没有一个文件夹有任何模板文件,我可以看到。

如何使用 Visual Studio 2019 修改 C++ 中不同项目的模板文件?

【问题讨论】:

  • 你真的应该避免使用using namespace std;,坚持使用std::name_of thing,或者至少使用using std::name_of_thing;,然后只使用name_of_thing。您也可以创建一个空项目,因为不需要 #include &lt;iostream&gt; int main() {} 的所有内容。
  • 访问this页面可能会对您有所帮助。
  • 我建议设置一个轻量级 Cmake 项目基线,它会让您习惯 cmake,并允许您为每个要开发的新项目复制基线。
  • @LionKing 谢谢,这解决了我的问题!

标签: c++ visual-studio visual-studio-2019 file-templates


【解决方案1】:

有两种方法可供您选择。

使用导出模板向导:

Visual Studio 提供了一个导出模板向导,可用于更新现有模板:

  • 从菜单栏中选择文件 > 新建 > 项目。
  • 选择要更新的模板并继续 创建新项目的步骤。
  • 在 Visual Studio 中修改项目。例如,改变输出 为项目键入或添加新文件。
  • 在“项目”菜单上,选择“导出模板”。 导出模板向导打开。
  • 按照向导中的提示将模板导出为 .zip 文件。
  • (可选)将 .zip 文件放在以下目录中: %USERPROFILE%\Documents\Visual Studio \Templates\ProjectTemplates 使其可用于 选择。如果您没有选择 选项自动将模板导入到 Visual Studio 中 导出模板向导。
  • 删除旧的模板 .zip 文件。

手动更新现有模板:

  • 找到包含模板的 .zip 文件。用户项目 模板位于 %USERPROFILE%\Documents\Visual Studio \Templates\ProjectTemplates。
  • 解压 .zip 文件。
  • 修改或删除当前模板文件,或将新文件添加到 模板。
  • 打开、修改和保存 .vstemplate XML 文件以处理更新 行为或新文件。
  • 选择模板中的文件,然后右键单击或 上下文菜单,然后选择发送到 > 压缩(压缩)文件夹。这 您选择的文件被压缩为 .zip 文件。
  • 将新的 .zip 文件与旧的 .zip 文件放在同一目录中。
  • 删除提取的模板文件和旧的模板 .zip 文件。

另外,您可以访问source page了解更多详情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-03
    • 2010-09-29
    • 1970-01-01
    • 2016-08-07
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多