I have some relative path in my *.vcxproj
<ItemGroup>
<ClCompile Include="myFile.cpp" />
<ClCompile Include="..\..\Some\Reference\file.cpp" />
<ClCompile Include="..\..\Some\Other.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\SomeOther\Headers.h" />
...
</ItemGroup>
When I compile this, it always complaints as follows for both files
fatal error C1083: Cannot open include file: 'StdAfx.h': No such file or directory
fatal error C1083: Cannot open include file: 'StdAfx.h': No such file or directory
When i copy these files manually to directory containing myFile.cpp, update proj file for path, it compiles.
So Any clue why does this happen? and How to fix it?
Probably because there is no file named "StdAfx.h" in ..\..\Some\Reference. When you #include "StdAfx.h", the first place it looks is in the current directory; then, it checks all the directories configured in the include path.
Further, I bet you DO have a StdAfx.h in the same directory as myFile.cpp. When you move those CPP files to the directory containing myFile.cpp, then when it checks for StdAfx.h in the current directory, it succeeds and includes it. That's why moving the file fixes the problem.
If you want to make it work with the files in ..\..\Some\Reference, then you need to add myFile.cpp's directory to the include paths (in your project settings).
Related
In my project file I include all of the .cpp and .h files in a directory by using a wildcard as shown in the code snippet below. Everything compiles just fine.
The annoying issue I run into is I have to add files to the Solution Explorer manually via the Add Existing Item dialog. If I don't use a wildcard and spell out every file, I don't have the same problem. Not something I want to do with a boatload of source files though.
Is there a way to get these wildcard includes into the Solution Explorer?
<ItemGroup>
<ClCompile Include="$(SrcDir)\*.cpp" >
<Link>%(FileName)</Link>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="$(SrcDir)\*.h">
<Link>%(RecursiveDir)\%(FileName)</Link>
</ClInclude>
</ItemGroup>
I have a bunch of TypeScript files in my project, and I want them all to be copied to the output directory on each build, preserving their structure. Here's what I've tried, but it does not work:
<ItemGroup>
<TypeScriptFiles Include="Scripts\*.ts" />
</ItemGroup>
<Target Name="CopyTypeScriptsToOutput">
<Copy SourceFiles="#(TypeScriptFiles)" DestinationFolder="$(OutputDir)\Scripts" />
</Target>
I've also used Include="Scripts\**\*.ts" but no success. What could be wrong?
I've also used Include="Scripts***.ts" but no success. What could be
wrong?
The contents of the Include are the relative path of the files in your project.
The main problem is that you did not specify how the target runs. If you only use the Build UI to build your project, the target will not run. You should add build dependencies to the target, usually like BeforeTargets and AfterTargets, so that you run the target at build time.
Second, you have a problem with the properties of the target generated path like $(OutputDir). I tried to test this property in vs2015,2017,2019, MSBuild does not have this property by default. If the property is not defined by yourself, the value will never be reached. So I recommend that you can use $(OutputPath) and $(OutputDir).
In addition, please place TypeScriptFiles in the target to prevent confusion when the csproj file is first loaded. If you define it globally, it will be recognized by the system and mapped to the project again.
Sample
This is the target that I successfully completed.
<Target Name="CopyTypeScriptsToOutput" AfterTargets="Build">
<ItemGroup>
<TypeScriptFiles Include="Scripts\*.ts" />
</ItemGroup>
<Copy SourceFiles="#(TypeScriptFiles)" DestinationFolder="$(OutputPath)\Scripts" />
</Target>
Hope it could help you.
in my package.json, I ma using cpx to copy files in the dist folder
"scripts": {
"copy-media": "$(npm bin)/cpx media/**/*.* dist/media",
"build": "tsc && yarn copy-media",
},
So it builds first tsc, and then copy all files from folder media to dist
If I have a project structure like this:
\MySolution
\MyProject
ReadMe.md
\build
MyProject.targets
What would the value of $(MSBuildThisFileDirectory) be when used in the MyProject.targets file?
Assuming my solution folder is in the root of C: drive, would it be?..
c:\MySolution\MyProject\build\
In the MyProject.targets file, how would I reference the ReadMe.md file using the $(MSBuildThisFileDirectory)?
Additional information:
MyProject.targets looks like:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\xxx\ReadMe.md">
<Link>FrameworkTests.feature</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CustomToolNamespace></CustomToolNamespace>
</None>
</ItemGroup>
</Project>
What is the value of MSBuildThisFileDirectory?
It depends on your MyProject.targets. According to the literal meaning of this variable, you could to know ThisFileDirectory means "This File Directory".
Since you have use this argument in the file MyProject.targets, the path should be related to the location of the "this file" MyProject.targets. So the value of this argument should be the directory of this file MyProject.targets.
After install the nuget, the file MyProject.targets should be added to the path:
c:\MySolution\packages\MyProject.1.0.0<YouPackagefolder>\build
You can use a target to output that value in your project file, to accomplish this, unload your project. Then at the very end of the project, just before the end-tag </project>, place below scripts:
<Target Name="TestValue" AfterTargets="build">
<Message Text="#(none)">
</Message>
</Target>
I want a specific directory to be copied to output folder ("bin") on every build. I think it can be handled via post build scripts. But I'm not sure how to copy a directory itself. I know how to handle specific files.
For eg, this works for a file:
In
Project > Properties > Build Events> Post Build
COPY "$(SolutionDir)Resources\Release Notes.pdf" "$(TargetDir)"
But suppose I have a directory Template, now I need everything under Template to come to bin folder upon successful build maintaining the folder structure.
I tried this:
COPY "$(SolutionDir)Resources\Template\" "$(TargetDir)"
Only the files in Template directory gets copied this way and not the sub directories and the files inside Template folder. I want the folder Template itself to come inside my output bin folder. In other words, bin should look like:
bin > Template > abc.xxx
xxx.yyy
Subdirectory1 > asd.qwe
zxc.qwe
Subdirectory2 > ...
This could be a duplicate, but I couldn't find a relevant thread. Thanks.
I just added this to my *.csproj file (right click Edit Project File)
<ItemGroup>
<Content Include="MYCUSTOMFOLDER\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
I think for this the directory needs to be on same hierarchy level as *.csproj file or bellow that.
This worked for me. /S is the key which copies everything recursively.
XCOPY "$(SolutionDir)Resources\Template" "$(TargetDir)\Template\" /S
Since I wanted files to be overwritten every time without a prompt, I added a /Y switch as well.
XCOPY "$(SolutionDir)Resources\Template" "$(TargetDir)\Template\" /S /Y
Try XCOPY instead of COPY; e.g.
XCOPY "$(SolutionDir)Resources\Template\" "$(TargetDir)\Template" /s /i /y
More info on XCOPY here...
http://www.computerhope.com/xcopyhlp.htm
Here's an additional solution working on Visual Studio 2019 as of the date of this post. This will copy the folder structure recursively and all files within. Tested on a C++ .vcxproj in a multi-project solution.
First, start by editing your [ .proj / .vcxproj / .csproj ] file. Once open, find your project scoped tag. If you already have ItemGroups within, then paste the code below directly after the existing ones. Otherwise, add it in before the PropertyGroup tags. Then modify the Include & Link parameters for the folder structure you wish to copy to the output path.
<ItemGroup>
<Content Include="..\Assets\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DeploymentContent>true</DeploymentContent>
<Link>Assets\%(RecursiveDir)\%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>
Note: If you have multiple top level folders, like JS, IMG, BIN, etc., then create a new entry for each one.
The solution by CodingYourLife almost worked for me, but I found out that PreserveNewest was not being respected. I found a solution on the Visual Studio forums that works correctly. My .CSPROJ now looks like this:
<Content Include="assets\**">
<Link>assets\%(RecursiveDir)\%(Filename)%(Extension)</Link>
<TargetPath>assets\%(RecursiveDir)\%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Note: This solution requires Visual Studio 16.10 or newer.
I have a working solution of this question:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<ItemGroup>
<CommonFont Include="..\common\src\Web\wwwroot\css\fonts\*.*" />
</ItemGroup>
<Copy SourceFiles="#(CommonFont)" DestinationFolder="wwwroot\css\fonts" SkipUnchangedFiles="true" />
</Target>
This is the only solution that worked for me (VS2022, .Net Framework):
<ItemGroup>
<ContentWithTargetPath Include="..\..\..\Libraries\Core\Business\Vodovoz.Reports\Reports\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>Reports\%(RecursiveDir)\%(Filename)%(Extension)</TargetPath>
</ContentWithTargetPath>
</ItemGroup>
I have got a solution with 30 projects. All these projects have the same custom MSBuild script which copies all the compiled files (from the _PackageTempDir) to another location on the hard drive.
<ItemGroup>
<DeployFilesBin Include="$(_PackageTempDir)\bin\*.*" />
</ItemGroup>
<Copy SourceFiles="#(DeployFilesBin)" DestinationFiles="#(DeployFilesBin->'$(DeployDestinationBin)\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="True" />
When I change the name of a file, this file will not be removed from the destination directory which is unwanted because I could get an ambiguous reference exception.
I do not want a global MSBuild script because I would like to use Visual Studio's build shortcut for executing csproj files.
Is there any way to delete all files in the destination directory and then execute all csproj files? Maybe a target which is fired once of a way to declare a global variable to check if that specific cleanup target has already ran?