C++ 在应用程序构建期间自动复制文件到输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5779703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:52:27  来源:igfitidea点击:

Automatic copy files to output during application building

c++visual-studio-2010visual-c++msbuild

提问by Loom

There is Copy to Output Directoryproperty for files in C# projects. But in VC++ projects it is absent. I know, that I can use Build eventsin VC++ and write there something like

C# 项目中的文件有复制到输出目录属性。但是在 VC++ 项目中它是不存在的。我知道,我可以在 VC++ 中使用Build 事件并在那里写一些类似的东西

xcopy /y /d %(FullPath) $(OutDir)

Is there a way to avoid the use of CMD (and other scripting methods)? Can msbuild do something to help in this case?

有没有办法避免使用 CMD(和其他脚本方法)?在这种情况下,msbuild 可以提供帮助吗?

采纳答案by Anton K

It depends on what version of Visual Studio you are using. Format of VC++ project file in Visual Studio 2008 is not MSBuild and so using xcopy in PostBuildStep is a good choice.

这取决于您使用的 Visual Studio 版本。Visual Studio 2008 中 VC++ 项目文件的格式不是 MSBuild,因此在 PostBuildStep 中使用 xcopy 是一个不错的选择。

VC++ project in Visual Studio 2010 has MSBuild format. Thus, there is functionality of MSBuild Copy task.

Visual Studio 2010 中的 VC++ 项目具有 MSBuild 格式。因此,存在 MSBuild Copy 任务的功能。

Below is a sample:

下面是一个示例:

<Copy
    SourceFiles="%(FullPath)"
    DestinationFolder="$(OutDir)"
/>

If the destination directory does not exist, it is created automatically

如果目标目录不存在,则自动创建

An MSDN Copy task reference is here

MSDN Copy 任务参考在这里

回答by jochen

Can MSBuild do something to help in this case?

在这种情况下,MSBuild 可以做些什么来提供帮助吗?

Using MSVC 2012, this worked for me:

使用 MSVC 2012,这对我有用:

Assumed you have a file "Data/ThisIsData.txt" in your c++ Project.

假设您的 C++ 项目中有一个文件“Data/ThisIsData.txt”。

Unload the project (right click --> Unload Project).
Edit project XML (right click --> Edit .vcxproj)
Now you see the projects MSBuild file as XML in your editor.

卸载项目(右键单击 --> 卸载项目)。
编辑项目 XML(右键单击 --> 编辑 .vcxproj)
现在您可以在编辑器中看到项目 MSBuild 文件为 XML。

Find "ThisIsData.txt". It should look something like:

找到“ThisIsData.txt”。它应该看起来像:

<ItemGroup>
<None Include="Data\ThisIsData.txt" />
...
</ItemGroup>

Now add an other item group like this:

现在添加另一个项目组,如下所示:

<ItemGroup>
<Content Include="Data\ThisIsData.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
...
</ItemGroup>

Reload the project and build.
Your file "ThisIsData.txt" should get copied to $(OutDir)\Data\ThisIsData.txt.

重新加载项目并构建。
您的文件“ThisIsData.txt”应该被复制到 $(OutDir)\Data\ThisIsData.txt。

Why duplicating the ItemGroup?

为什么要复制 ItemGroup?

Well if you simply change the None include to a content include, the IDE does not seem to like it any more, and will not display it. So to keep a quick edit option for my data files, I decided to keep the duplicated entries.

好吧,如果您只是将 None include 更改为 content include,IDE 似乎不再喜欢它,并且不会显示它。因此,为了为我的数据文件保留一个快速编辑选项,我决定保留重复的条目。

回答by Henri Socha

In VS 2015 it is possible to give C projects the functionality that is in C#. (Idea from building off of jochen's answer.) Instead of adding another ItemGroup, modify the given itemgroup adding a CopyTo element. I.E, using his example, simply enhance the original entry to:

在 VS 2015 中,可以为 C 项目提供 C# 中的功能。(根据 jochen 的答案提出的想法。)不要添加另一个 ItemGroup,而是修改给定的 itemgroup,添加一个 CopyTo 元素。IE,使用他的例子,简单地将原始条目增强为:

<ItemGroup>
  <None Include="Data\ThisIsData.txt" />
    <DeploymentContent>true</DeploymentContent>
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
...
</ItemGroup>

No other ItemGroup required. By adding the CopyTo element, you add an "Included In Project" property.

不需要其他项目组。通过添加 CopyTo 元素,您可以添加“包含在项目中”属性。

VS 2015 C Property with Included In Project added

添加了包含在项目中的 VS 2015 C 属性

回答by Eric

In Visual Studio 2017 you can do this in the IDE. I am not sure about earlier versions.

在 Visual Studio 2017 中,您可以在 IDE 中执行此操作。我不确定早期版本。

Simply add the file as an included project file so it shows in the Solution Explorer. Then right click on the file and select the Properties menu.

只需将该文件添加为包含的项目文件,即可在解决方案资源管理器中显示。然后右键单击该文件并选择“属性”菜单。

Change the Content to "Yes" and change the Item Type to "Copy file"

将内容更改为“是”并将项目类型更改为“复制文件”

If you look at the changes it made to the project file you can see it added this:

如果您查看它对项目文件所做的更改,您可以看到它添加了以下内容:

<ItemGroup>
  <CopyFileToFolders Include="Filename.txt">
    <DeploymentContent>true</DeploymentContent>
    <FileType>Document</FileType>
  </CopyFileToFolders>
</ItemGroup>

回答by WaffleSouffle

Following henri-socha's answerabout VS2015 (and probably VS2013 and VS2012, or anything using MSBuild style projects), the ItemGroup item type is important.

henri-socha关于 VS2015(可能还有 VS2013 和 VS2012,或任何使用 MSBuild 样式项目的项目)的回答之后,ItemGroup 项目类型很重要。

Specifically <Text>items do not seem to be copied, whereas <Content>items do.

特别是<Text>项目似乎不会被复制,而<Content>项目会。

So, for a project directory Datacontaining a text file ThisIsData.txt, this will create a subdirectory Dataunder the $(OutDir)directory and copy the file ThisIsData.txtfrom the project into it if it's newer:

因此,对于包含文本文件ThisIsData.txt的项目目录Data,这将在该目录下创建一个子目录Data并将文件ThisIsData.txt从项目复制到其中(如果它是较新的):$(OutDir)

<ItemGroup>
  <Content Include="Data\ThisIsData.txt">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

This won't, although it is what the Visual Studio IDE will insert if you add the text file to your project, and set the Contentproperty to True.

这不会,尽管如果您将文本文件添加到项目并将Content属性设置为True.

<ItemGroup>
  <Text Include="Data\ThisIsData.txt">
    <DeploymentContent>true</DeploymentContent>
  </Text>
</ItemGroup>

So in other words you need to add the file via the IDE to make it realise the file is included in the project (which adds <Text>tag ItemGroup), and then open the project in a text editor and add the <Content>tag ItemGroup to get it to do what you want.

所以换句话说你需要通过IDE添加文件,使其意识到文件包含在项目中(添加<Text>标签ItemGroup),然后在文本编辑器中打开项目并添加<Content>标签ItemGroup来完成你想要什么。

I'm not sure what the <DeploymentContent>tag actually does. It may be a remnant since the only MSDN reference I could find considers it archived: https://msdn.microsoft.com/en-us/library/aa712517.aspx

我不确定<DeploymentContent>标签实际上是做什么的。它可能是一个残余,因为我能找到的唯一 MSDN 参考认为它已存档:https: //msdn.microsoft.com/en-us/library/aa712517.aspx

回答by kjhf

You can specify copying in the project file as Jeff G answeredin another question:

您可以在项目文件中指定复制,如Jeff G在另一个问题中的回答:

In the *.vcxprojfile, change:

<Text Include="Filename.txt" />

to:

<Content Include="Filename.txt">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

Then in the *.vcxproj.filtersfile, change:

<Text Include="Filename.txt">
    <Filter>Resource Files</Filter>
</Text>

to:

<Content Include="Filename.txt">
    <Filter>Resource Files</Filter>
</Content>

*.vcxproj文件中,更改:

<Text Include="Filename.txt" />

到:

<Content Include="Filename.txt">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

然后在*.vcxproj.filters文件中,更改:

<Text Include="Filename.txt">
    <Filter>Resource Files</Filter>
</Text>

到:

<Content Include="Filename.txt">
    <Filter>Resource Files</Filter>
</Content>

where the <Text ...>tag is for specified text files (it'll be <Image ...>for image files etc.)

其中,<Text ...>标签是指定的文本文件(这将是<Image ...>图像文件等)

回答by Jacq

In visual studio 2019 after setting the file as "Include in project" you can edit the properties an select as Item Type "Copy file" (as shown in https://i.stack.imgur.com/vac2b.png) This avoids the manual vcxproj file edition.

在 Visual Studio 2019 中将文件设置为“包含在项目中”后,您可以编辑属性并选择项目类型“复制文件”(如https://i.stack.imgur.com/vac2b.png所示 )这避免了手册 vcxproj 文件版本。

回答by Jacq

If it's a COM dll, you can add it to the root of your project, mark it as 'Content' and set copy to output directory to 'Always'. I had to do this for signature capture COM assembly.

如果它是 COM dll,您可以将其添加到项目的根目录,将其标记为“内容”并将复制到输出目录设置为“始终”。我必须为签名捕获 COM 程序集执行此操作。