windowsvisual-c++compilationcross-compilingcl

Question about compile and link stages and machine type


I am using the Visual Studio 2019 product for Windows development on a 64 bit Windows and if I investigate the differences between the compilation and linking of 32 or 64 bit programs I only notice a difference in the linking stage. 32 bit linking specifies /MACHINE:X86 whilst 64 bit linking specifies /MACHINE:X64

However, this Microsoft link:

https://learn.microsoft.com/en-us/cpp/build/reference/machine-specify-target-platform?view=msvc-170

has this comment about the /MACHINE option : Usually, you don't have to specify the /MACHINE option. LINK infers the machine type from the .obj files.

So I am confused about how the Microsoft compiler directs either 32 or 64 bit output.

Is it at the compilation stage that object files have a platform designation, ie 32 or 64 bit?

Or is the target platform only specified at the linker stage?

Or is the target platform somehow controlled via the location of cl.exe which is run to generate the executable? For example, on my PC, cl.exe is located in 2 separate places:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64

and

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.16.27023\bin\HostX64\x86


Solution

  • Each cl.exe only supports one architecture. Just running cl.exe should tell you its target or you can look at the options for /arch.

    Object files contain CPU specific code. .obj files are in COFF format and contain information about the CPU.