Separate or same folders for Debug/Release output

2019-06-18 19:05发布

What is the best practice for defining the target output folder for Debug/Release: Is it better to have separate folders for Debug/Release or should I use the same folder for both (when using .NET/C#)?

I have two separate solutions, so the projects of one solution can't have a reference by project to a project of the other solution. So you are forced to add a reference to the assembly file directly. This results in another problem: If you add a reference to another assembly via a file you can add only one instead of one for debug and one fore release (like you can do with libraries in C++). Another problem is that I have to add a reference, for example, to bin/Release/MyOtherProject/MyAssembly.dll. I think this is confusing, especially when building Debug and having referenced Release. => Build errors and version conflicts may occur.

Has anyone long experience in building into the same target folder in large projects and environments?

It is a more precise question related to Stack Overflow question Should we still make a difference between the release and debug output folders?.

3条回答
贪生不怕死
2楼-- · 2019-06-18 19:34

If they are two different solutions, I don't see a good reason why you would want to reference the Debug output. In fact, I don't think you should be referencing the output in the other projects directory at all. Your references could be broken if the code is moved to another machine and you don't perfectly replicate your project structure.

I think best practice would be to either

1) Have a Lib directory in Project / Solution A that you manually copy the output of Project / Solution B. Do this if you do not make changes to Project B very often.

2) Put both projects in the same solution, then add a reference to the project. Remember, you can have one project in multiple solutions. Do this if you are developing both projects at the same time.

查看更多
神经病院院长
3楼-- · 2019-06-18 19:36

We have been using the same directory for debug and release in .NET applications for 10 years and have never had a problem.

This approach makes numerous tasks substantially easier, such as building installs, copying custom DLL files to directories post-build, and versioning files in the output directories such as license files that are necessary for developers to run the application correctly.

查看更多
Evening l夕情丶
4楼-- · 2019-06-18 19:42

If you have two related projects that you care enough about them being in the same target type and configuration (which means it's not some third-party package you just use) - you better put them in the same solution.

If you can't for some reason, there is another (A bit ugly) solution.

You can edit your .csproj and "dynamically" reference assemblies based on the target. In the .csproj file the references are inside an XML element called "ItemGroup". In each itemgroup you have many "Reference" elements, and the path to the assembly is in "Hint" element. You can put $(Configuration) variable in the hint. For example:

<ItemGroup>
    <Reference Include="your assembly">
      <HintPath>..\..\$(Configuration)\blabla.dll</HintPath>
    </Reference>
</ItemGroup>

This way, the directory name will include your configuration name (they should be match - which means that if you change the names you break things).

Another option is to define three completely different item groups and use the "Condition" attribute:

  1. Item group with no condition that will use to reference System, System.xml, etc.
  2. Item group with attribute Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " that will include the references that will be used in Debug configuration.
  3. Item group with attribute " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " for Release configuration.

In each item group you'll have reference to the assemblies in the correct configuration.

It's a bit ugly, because the .csproj sometimes modified automatically plus you can easily forget and make a mess out of it. But anyway, it'll work.

查看更多
登录 后发表回答