Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent adding new csproj from adding AnyCPU back to solution file

We have a solution that we only want to have the x86 platform but every time we add a new project to the solution it adds AnyCPU back for every single project in the solution. This is tedious to remove all the AnyCPU lines in the solution file because we have 70+ projects in the solution. Is their any way to configure Visual Studio to prevent this from being added?

Not sure if this is relevant but we are on the legacy project system and only use csproj in our solution.

EDIT 1:

The reason I would like to keep AnyCPU from being added back to the solution is because of warnings and issues with building with certain nuget packages.

  1. Some of our third party dependencies are built against x86 and it produces warnings with no codes when we reference them so I am unable to suppress them.
  2. The nuget package I am specifically aware causes issues is CefSharp. It will fail to build our desktop application that references it if the developer selects AnyCPU. It uses the platform to determine if it should copy its unmanaged x86 or x64 dll.

EDIT 2:

Here is the section of the solution that causes issues when we go to build. From what I have read Visual Studio looks through this list alphabetically for a platform if one is not provided. This example is from an unrelated solution.

GlobalSection(SolutionConfigurationPlatforms) = preSolution
    Debug|Any CPU = Debug|Any CPU
    QA|Any CPU = QA|Any CPU
    Release|Any CPU = Release|Any CPU
EndGlobalSection

EDIT 3:

As far as I can tell Hans' answer is the correct way to handle this. I have looked for other ways to handle this but after looking on uservoice was able to find where this was suggested in 2011.

like image 860
Max Young Avatar asked Jul 10 '18 13:07

Max Young


1 Answers

This is a very common mistake. VS2010 is for a large part responsible for it, its project templates chose x86 instead of AnyCPU. Fixed again in VS2012 but not otherwise repairing any damage done by solutions that were once exposed to VS2010. Or helping programmers to get it right.

The platform selection is meaningless for C# projects. You use the exact same build tools for any platform, the generated code is truly compatible with "any cpu". It is the just-in-time compiler that locks in the target processor, it does so at runtime. The only setting that matters at all to affect what the jitter does is present in the Project > Properties > Build tab. Only the settings for the EXE project matter, libraries don't have a choice but to be compatible with the bitness of the process.

It does matter for C++ projects. A lot, they use a completely different compiler and linker for each platform. Necessarily so, C++ projects generate machine code up front and that code must be compatible with the user's machine. Also the reason this got fumbled at VS2010, that's when the C++ build system moved to MSBuild.

The typical reason AnyCPU pops back into the solution is adding a new project. Since they default to AnyCPU again, it needs to be added back to the solution platforms.

By far the best solution is to stop fighting the machine. AnyCPU should be your preference. Use Build > Configuration Manager > Active Solution combobox > Edit. Remove x86 so only AnyCPU remains. And do focus on what you want to accomplish, it is the EXE project settings that matter. Beware of yet another trap, even though the default platform is AnyCPU, a project template turns on the "Prefer 32-bit" checkbox by default. Not any cpu anymore. High time Microsoft fixes this btw, the 64-bit debugger and jitter have been stable and capable long enough to no longer need this.

like image 72
Hans Passant Avatar answered Nov 27 '22 10:11

Hans Passant