38

I'm trying to organize my workspace and want my intermediate objects to be put in the ..\build\obj folder in relation to my .csproj file. So I put:

<IntermediateOutputPath>..\build\obj\Debug</IntermediateOutputPath>

in the .csproj file. The intermediate objects are now put in that location when the solution is built, but the problem is that an obj directory is still created in the directory the .csproj file is in (something to the effect of obj\Debug\TempPE) when the solution is opened. What is this directory for, and how can I relocate it?

4
  • 4
    I've just switched over to C# from C++ and have gotten very used to having my workspace constructed a very specific way. I would like to maintain the same workspace structure in C#. Commented Jul 22, 2010 at 17:09
  • 1
    duplicate of stackoverflow.com/questions/815056/… Commented Mar 2, 2011 at 16:09
  • The way that certain 'enterprisey' version control systems make it difficult to deal with non-versioned objects inside a versioned folder. Beyond that, I've never cared for having build objects mixed in with my source tree. Commented Mar 15, 2012 at 17:15
  • I posted a solution for VS2013 at the dupe Commented May 29, 2015 at 1:17

4 Answers 4

34

You could try to do this (don't forget that there are Debug and Release sections which will be used depending on what type of build you are targeting):

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    <BaseIntermediateOutputPath>..\build\obj</BaseIntermediateOutputPath>
    <IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <BaseIntermediateOutputPath>..\build\obj</BaseIntermediateOutputPath>
    <IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\</IntermediateOutputPath>
</PropertyGroup>
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried this. An obj\ directory is still being generated in the same directory as the .csproj file. When I build, the intermediates go to the directory specified by the .csproj file, but for some reason this obj\ directory is still being generated in the .csproj file directory as well.
@blachniet - Visual Studio creates that obj directory. Its annoying, but if you don't use VS and just use MSBuild from the command line, it works fine. I have no idea why Microsoft thinks we want to put build output into our source tree.
11

Do this like Microsoft:

  <PropertyGroup>
    <IntermediateOutputPath Condition=" '$(PlatformName)' == 'AnyCPU' ">$(BaseIntermediateOutputPath)$(Configuration)\</IntermediateOutputPath>
    <IntermediateOutputPath Condition=" '$(PlatformName)' != 'AnyCPU' ">$(BaseIntermediateOutputPath)$(PlatformName)\$(Configuration)\</IntermediateOutputPath>
  </PropertyGroup>

1 Comment

For what it's worth, all these years later, I attempted these conditional statements and they seem to be ignored. It just applies the last one in the list, regardless of the condition. Note I did that in the Directory.Build.Props file at the solution level.
5

According to this post, a file named Directory.Build.props can be created in the folder that containes the .csproj project file. In order to relocate the obj folder, we can add

<BaseOutputPath>..\Build</BaseOutputPath>

to the PropertyGroup section in .csproj project file, and write

<?xml version="1.0"?>
<Project>
    <PropertyGroup>
        <BaseIntermediateOutputPath>..\Intermediate\obj</BaseIntermediateOutputPath>
        <IntermediateOutputPath>$(BaseIntermediateOutputPath)\$(Configuration)</IntermediateOutputPath>
        <!--The following is the default configuration for "MSBuildProjectExtensionsPath"-->
        <!--<MSBuildProjectExtensionsPath>$(BaseIntermediateOutputPath)</MSBuildProjectExtensionsPath>-->
    </PropertyGroup>
</Project>

to the Directory.Build.props file.

1 Comment

Thanks you! This should actually be the top answer because it's the only way to get rid of annoying obj directory VS creates in the project dir. The BaseOutputPath can also be set in Directory.Build.props (just to keep everything in one place). In this case, specifying paths relative to the solution or project dir will be preferable, e.g.<BaseIntermediateOutputPath>$(SolutionDir)Obj\</BaseIntermediateOutputPath>.
3

I've used:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>$(OBJDIR)\$(SolutionName)\bin\$(Configuration)\</OutputPath>
    <BaseIntermediateOutputPath>$(OBJDIR)\$(SolutionName)\obj\$(Configuration)\</BaseIntermediateOutputPath>
    <IntermediateOutputPath>$(BaseIntermediateOutputPath)\</IntermediateOutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
</PropertyGroup>

(In Visual Studio 2012 Beta, FWIW), and it works fine.

The OBJDIR on my machine points to E:\BuildOutput.

1 Comment

I believe the whole purpose of the BaseIntermediateOutputPath is to not include $(Configuration) at that point (i.e., it's name, "base..."). You're supposed to use IntermediateOutputPath to tack that part on. Also, note that as you have it, you create a repetition of two backslashes at the end of your IntermediateOutputPath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.