Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Many years I have built my apps under "Any CPU" configuration. But I have update to .Net 5, decide to use the Publish Function, which asking to implicitly choose between "win-x86" and "win-x64" options for target runtime.

I thought the whole deal with the "Any CPU" was to built apps not choosing between x86 and x64? 🤓 What should I do now?

This is probably because self-contained means that the runtime is embedded, so you have to pick which runtime should be embedded. riQQ Aug 21, 2021 at 17:33 @riQQ But "framework-dependent" version also asks about target runtime :) The only difference is that the "portable" option appears in that list. 0xothik Aug 21, 2021 at 20:13 @Vega Programming Tools (like Visual Studio) are on topic here. See stackoverflow.com/help/on-topic : "software tools commonly used by programmers". Julian Aug 22, 2021 at 8:22

When publishing the app, a target architecture needs to be specified, because the compiler will create an architecture specific stub loader for the project (in Windows, the "exe" file).

In .NET Core, all C# projects (irrespective of whether they're applications or libraries) compile to .dll files. These .dll files are platform independent, or AnyCPU (of course unless they contain code that would only run on certain operating systems, such as P/Invoke calls). The applications compiled this way can be run by using the dotnet <DllName> command.

Now this is not convenient, because people expect an "exe" file to start a program with. This exe file (or an equivalent "no-extension" file for unix) is created by the publish command, in addition to copying everything together. The exe file with the same name as the main .dll is the stub-loader that basically calls dotnet <myapp.dll> , even if publishing a framework-dependent application. That's why even a program that is targeting AnyCpu needs a platform-dependent publish step.

[Note: On Windows, the exe file might already be created in the build step, as VS uses it. But you'll notice that it always has about the same size, for all your apps.]

Sorry, still not get, why I must specify target runtime :) Can't the stub loader be Any CPU? But one more thing — I have tried dotnet publish , and it allows to publish framework-dependent version without specifying runtime — and throws an error if I try to do the same with self-contained flag. So it might be just the GUI problem?.. 0xothik Aug 22, 2021 at 14:52 The stub loader is an operating-system dependent native app. It is a (small) application that is written for that particular OS and CPU, since .NET binaries cannot be directly run on a CPU. They need an "interpreter", and that interpreter is the .NET runtime. But it must first be loaded in an operating-system dependent way. PMF Aug 22, 2021 at 15:01 dotnet publish without target probably just publishes for the current platform, implicitly assuming that's what you want. The GUI is just more explicit in that it does not have a "default" option. PMF Aug 22, 2021 at 15:03 But why then when I press "publish" on Any CPU app on .Net Framework 4.8 — it did not ask nothing about target runtime? :) 0xothik Aug 22, 2021 at 16:03 To make sure, you could use an x86 VM to check, but it's very unlikely there will be any problems. If the X86 version works on the X64 computer, it is quite certain that it will work on pure X86, too. PMF Aug 23, 2021 at 7:13

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .