Computing: Free Pascal Programming

32-bit cross-compiling on Windows 64-bit.

Suppose that you run Windows 10 64-bit and that you want to create a Lazarus/Free Pascal application that runs on a 32-bit Windows platform. Globally, there are 2 possibilities to do:

  1. Installing Lazarus 64-bit and Lazarus 32-bit side by side, taking advantage of the possibility to create a secondary installation of the development environment, as explained in my tutorial Installing Lazarus/Free Pascal on MS Windows.
  2. Creating the 32-bit applications using a Windows 64-bit to Windows 32-bit cross-compiler. That's what this tutorial is about.

The binaries, created by a compiler are normally platform specific, so that you need a different compiler for each platform, you want your application to run on. However, the fpc compiler is designed so that the distributed compiler can create machine code for a given CPU (different CPUs need different machine code) and it knows specific requirements for all supported platforms (operating systems) available on that particular CPU. This means that you can perform compilation with the same compiler used for native compilation as long as you stick to the same CPU. If you need to create binaries for a different CPU, you need a special cross-compiler, i.e. a compiler running on the host CPU, but being able to create machine code for a different target CPU (in the case of fpc, such a cross-compiler is again able to target all supported platforms available on the target CPU).

If you run Windows 64-bit and wish to develop applications for Windows 32-bit, you'll need an i386 Win32 cross-compiler. Such a cross-compiler is available for download from the Lazarus for Windows 64 bits folder at the SourceForge website. The cross-compiler will be installed as an add-on of the Lazarus 64-bit (that must be installed) and will allow to build and debug 32-bit Windows applications from within your usual Lazarus IDE. This tutorial uses Lazarus 2.2.6 (with fpc 3.2.2) running on Windows 10. The corresponding cross-compiler file is lazarus-2.2.6-fpc-3.2.2-cross-i386-win32-win64.exe. Be sure that the version of the cross-compiler is the same that the version of the 64-bit Lazarus that you are actually using! Here is the link to the Lazarus for Windows 64 bits folder at sourceorge.net.

The installation should be no problem. The installer detects the directory where Lazarus is installed and sets up the cross-compiler there, without that you have to input whatever information.

Installing the Lazarus 32-bit cross-compiler on Windows 10 64-bit [1]
Installing the Lazarus 32-bit cross-compiler on Windows 10 64-bit [2]

To test the cross-compiler, I chose to build my Free Pascal GUI application "Clock". Building the application with defaults settings (i.e. without making any configuration changes within the IDE) will create a 64-bit executable. You can verify this by having a look in the "lib" subdirectory of your project folder. You'll find there a folder called x86_64-win64. This means that the executable (that you find in the project folder itself) is intended for a Windows 64 operating system, running on a 64-bit Intel compatible CPU.

How must we proceed to build an executable for Windows 32-bit? In the Lazarus menu, choose Tools > Options and in the left pane of the opening Options for project: Clock, select Compiler Options > Config and Target. In the right pane, in the section Target platform, set Target OS = Win32 and change the default CPU by setting Target CPU family = i386.

Setting the project options for Windows 32-bit cross-compiling

Note: The settings for OS and CPU are independent in the sense that "default CPU" does not mean default CPU for the OS selected, but default CPU for the host operating system. Thus, if you set Target OS = Win32 and do not change Target CPU family = Default, you would ask fpc to generate a 64-bit binary for Windows 32-bit. Trying to build an application with these settings will result in an error message as shown on the screenshot below.

Setting the project options for Windows 32-bit cross-compiling

The screenshot below shows the successful cross-compilation with target OS=Win32, CPU=i386 on my Windows 10 64-bit. If you have a look in the "lib" subdirectory of the project folder, you'll find there a folder called i368-win32. The executable created in the project folder itself is a 32-bit application intended to run on Windows 32-bit with an i368 CPU.

Successful Win32 cross-compiling on Windows 10 64-bit

Remains to test the executable. I did it by running my "Clock" on Windows XP 32-bit. Success - as you can see on the screenshot.

Testing an application, created by the Win32 cross-compiler on Windows 10 64-bit, on Windows XP 32-bit

Note: If building 32-bit applications is something you intent to do "all the time" and you don't want to make a secondary Lazarus installation, it would be convenient to create build modes, that allow you to choose the target from the menu of the IDE (instead having to change the project options). For details about this feature, please, have a look at the article "Cross compiling" in the Lazarus wiki.


If you find this text helpful, please, support me and this website by signing my guestbook.