Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic copy files to output during application building

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

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?

like image 408
Loom Avatar asked Apr 25 '11 15:04

Loom


People also ask

What is Copy to Output Directory?

"Copy to Output Directory" is the property of the files within a Visual Studio project, which defines if the file will be copied to the project's built path as it is. Coping the file as it is allows us to use relative path to files within the project.

What is output DIR?

output directory. [ESRI software] In ArcIMS, the folder designated during installation to hold files being served to users for display in a browser.


1 Answers

Can MSBuild do something to help in this case?

Using MSVC 2012, this worked for me:

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

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.

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

<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.

Why duplicating the 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.

like image 91
jochen Avatar answered Sep 28 '22 06:09

jochen