Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know which cpu to use in build (x86 x64 or AnyCpu)?

I'm using Windows 7 and x64 cpu. Does it mean that I should always compile with "x64" option in Visual Studio? What about "dll", can I use x86 dll from x64 application? Probably there are other concerns?

like image 696
Oleg Vazhnev Avatar asked Feb 09 '12 20:02

Oleg Vazhnev


3 Answers

This article contains a brief overview of each option.

Here's a small quote:

The default setting, Any CPU, means that the assembly will run natively on the CPU is it currently running on. Meaning, it will run as 64-bit on a 64-bit machine and 32-bit on a 32-bit machine. If the assembly is called from a 64-bit application, it will perform as a 64-bit assembly and so on.

If the project is set to x86, this means the project is intended to run only as a 32-bit process. A 64-bit process will be unable to call into an assembly set as X86. Reasons to set your project as x86 include dependencies upon native DLLs that are only available in 32-bit or making native calls assuming 32-bit . Applications and assemblies marked for x86 can still run on 64-bit Windows. However they run under WOW64. Visual Studio itself runs under this emulation mode since it is a 32-bit application.

Setting the project to x64 will specify that the assembly must run under 64-bit Windows. Attempting to run the assembly on 32-bit Windows or call the assembly from a 32-bit process will result in a runtime error.

like image 180
James Hill Avatar answered Oct 27 '22 01:10

James Hill


Generally speaking, you should use AnyCpu all the time.

If you have specific reasons to expect compatibility problems with that, then you'll need to choose x86 or x64 as appropriate.

As noted in the comments, dll's built against specific architectures can require you to build your assembly a certain way. This will be something you'll want to do when you need to, not otherwise.

like image 31
Andrew Barber Avatar answered Oct 26 '22 23:10

Andrew Barber


No... the machine you write your code on/compile/build your software (EXE, DLL...) does not have anything to do with the question which target (x86 / x64 / Any).

IF you want the result of the build to run anywhere you use either x86 or AnyCPU. If you want the result to run on x64 only then you use x64.

There are some cases where you must use x86 or x64 - that is when you use some (3rd-party?) component/library/DLL/ActiveX etc. in your project which is 32 Bit only (then x86) or only 64 Bit only (then x64).

like image 20
Yahia Avatar answered Oct 27 '22 00:10

Yahia