Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Intermediate Directory in C# Project

In VS 2008, is there a way to set the intermediate directory (where the .obj files go, not the final targets) in a C# project?

like image 919
Mike Ward Avatar asked Sep 29 '09 14:09

Mike Ward


People also ask

How do I change the build directory output?

Right-click on the project node in Solution Explorer and select Properties. Expand the Build section, and select the Output subsection. Find the Base output path for C#, and type in the path to generate output to (absolute or relative to the root project directory), or choose Browse to browse to that folder instead.

How do I change the solution path in Visual Studio?

In Visual Studio, click Tools > Options. Expand Projects and Solutions and click Locations. The Projects location field defines the default location for storing new projects. You can change this path if you are using a different working folder.

How do I change the output code in Visual Studio?

Open project properties. Set Output type to Console Application. Save and close.


2 Answers

You need to override BaseIntermediateOutputPath msbuild var, more about it here.

like image 169
Shay Erlichmen Avatar answered Oct 04 '22 00:10

Shay Erlichmen


I found a solution to this using Visual Studio Express 2013; I forget which version of VS introduced MSBuild, and I can't say for sure that this will work for any other version. But here goes:

In C:\Windows\Microsoft.NET\Framework are subdirectories corresponding to .NET versions; With VSX2013 on my system the folder of interest is v4.0.30319. (That's the .NET version used by MSBuild, not the one I'm targeting.) Within this folder are .targets files, which are collections of MSBuild rules.

Near the beginning of Microsoft.CSharp.targets are some rules that will import more rules files. The third of these import statements imports from the file named by the build variable CustomBeforeMicrosoftCSharpTargets. You might be able to figure out the name of this by reading the rules, but it's easier to add a custom build step of

echo $(CustomBeforeMicrosoftCSharpTargets)

Build and look at the output. In my case, the full path was

C:\Program Files (x86)\MSBuild\v12.0\Custom.Before.Microsoft.CSharp.targets

NOTE: in my case, there was already a folder C:\Program Files (x86)\MSBuild\12.0 but not v12.0.

So (as admin) I created the folder and placed a Custom.Before.Microsoft.CSharp.targets file that looks like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <BaseIntermediateOutputPath>$(SolutionDir)\Build\$(MSBuildProjectName)</BaseIntermediateOutputPath>
    </PropertyGroup>
</Project>

NOTE: $(ProjectName) does not work, use $(MSBuildProjectName).

When I built, the OBJs were placed under a Build directory under the solution, not under the project, in a subdirectory with the typical naming convention: Debug or Release or x86\Debug or x64\Release.

This did not work exactly as desired: I really wanted to place these in %TEMP% but the variable was not expanded, so I ended up with a build folder under the project called %TEMP%. Placing the expanded path in the rule worked just fine, but other subfolders of %APPDATA% failed due to permissions issues.

I assume this can be used to configure any of those variables; reading the default .TARGETS files is a chore but there are some useful comments therein.

like image 28
Mike C Avatar answered Oct 04 '22 01:10

Mike C