Search code examples
delphifpc

FPC does not define CPU386 symbol for 64-bit processors


The next application

program Project1;

{$IFDEF FPC}
  {$mode delphi}
{$ENDIF}

begin
  {$IFDEF CPU386}
    Writeln('CPU386');
  {$ENDIF}
  Readln;
end.

produce different output in Delphi(XE) and FPC(2.6.2) on my system (Win7 64 bit, CPU intel core i3). Delphi outputs CPU386 string, FPC does not.

Seems like FPC does not define CPU386 symbol for my CPU (see the list of FPC defines); it defines CPUX86_64 symbol instead.

So far I am using the next workaround:

{$IFDEF FPC}
  {$IFNDEF CPU386}
    {$IFDEF CPUX86_64}
      {$DEFINE CPU386}
    {$ENDIF}
  {$ENDIF}
{$ENDIF}

Solution

  • That is as expected. The CPU386 conditional is for 32 bit Intel x86 targets. The CPUX86_64 conditional is for 64 bit Intel x64 targets.

    When you compile your program with the 64 bit Delphi compiler, it does not produce any output either. Because the CPU386 conditional is not defined for the 64 bit Delphi compiler either.

    It is incorrect to do this:

    {$IFDEF CPUX86_64}
      {$DEFINE CPU386}
    {$ENDIF}
    

    The CPU386 means that the target architecture is 32 bit Intel x86. And 64 bit Intel x64 is not.

    The Delphi documentation for predefined conditionals is here: http://docwiki.embarcadero.com/RADStudio/en/Conditional_compilation_(Delphi)#Predefined_Conditionals

    It lists the following under the CPU heading:

    • CPUARM
    • CPU386
    • CPUX86
    • CPUX64

    For 32 bit x86 targets Delphi and FPC both define CPU386. For 64 bit x64 targets Delphi defined CPUX64 and FPC defines CPUX86_64.

    If you need to use common conditionals across shared source code then you will likely need to do some extra work. Suppose that you settle on CPUX86 and CPUX64. Then you would include this:

    {$IFDEF FPC}
      {$IFNDEF CPU386}
        {$DEFINE CPUX86}
      {$ENDIF}
      {$IFNDEF CPUX86_64}
        {$DEFINE CPUX64}
      {$ENDIF}
    {$ENDIF}