Friday, 12 July 2013

Lazarus FreePascal IDE: How to compile 32-bit and 64-bit applications on one machine

I love Pascal. Pascal is a great programming language, since it has a simplicity of a lang-for-study and flexibility of C when it comes to pointers and memory operations. I beloved Delphi 7 many years but the lack of 64-bit support finally forced me to migrate to Lazarus. Lazarus is an open-source IDE for open-source FreePascal compiler capable of making binaries for Windows, Linux, Mac, Android and WinCE. It is free as a free speech and has a strong community of active developers and bug-hunters. You can find more info about it here.

Today I re-installed the latest build once more and found a problem - I cannot switch processor modes to create several configurations for simulataneous builds on Windows 32 bit and 64 bit. I started googling for a handy howto but got no luck. Finally I managed to get it working and here is how.

Let's assume you have a Windows 7 64-bit host machine.

1. Go to Lazarus downloads and grab a Windows64-bit version. for instance, http://sourceforge.net/projects/lazarus/files/Lazarus%20Windows%2064%20bits/Lazarus%201.0.10/lazarus-1.0.10-fpc-2.6.2-win64.exe/download. Please note on FreePascal compiler version - it is 2.6.2 in my example. You will need the corresponding Win32 compiler bundle in the next steps so remember this at once.

2. Install Lazarus to the folder of you choice (for example C:\Lazarus)

3. Go to FreePascal SourceForge download page, then select Win32 and the compiler version you just remember (2.6.2). It is crucial to have win32 and win64 compilers of the same version. Download the Win32 installer, like http://sourceforge.net/projects/freepascal/files/Win32/2.6.2/fpc-2.6.2.i386-win32.exe/download.

4. Start installing your new compiler, select Custom install, and point the installation folder to "lazarus-installtion-folder"\fpc\"lazarus-version", like C:\lazarus\fpc\2.6.2.



then answer Yes on a prompt.

5. Select Custom install and select the following checkmarks only:



then press Next button to let FPC install itself.

6. Now you have a working dual-architecture setup and it is time to configure Lazarus. Open Lazarus and go to Tools-Options. Select the path to FPC compiler (fpc.exe) as follows

$(LazarusDir)fpc\$(FPCVer)\bin\$(TargetCPU)-$(TargetOS)\fpc.exe



7. Finally, go to your Project settings, open Code generation tab and select your processor arch (i386 or x86_64) and target version (win32 or win64) then apply changes. You will be able to build Win32 and Win64 binaries at your choice.

You can even adjust the build profiles and activate them when necessary.

Enjoy!

PS: To use architecture-specific code and constants, one can use the following snippet:

{$IFNDEF WIN64} // x32 code {$ELSE} // x64 code {ENDIF}

and declare -dWIN64 in Project options - Miscellaneous - Special switches under appropriate build profile (e.g Win64):

-dWIN64

That will trigger platform-specific defines like in C++.