32bit cross-compiling on Windows 64bit.
Suppose that you run Windows 10 64bit and that you want to create a Lazarus/Free Pascal application that runs on a 32bit Windows platform. Globally, there are 2 possibilities to do:
- Installing Lazarus 64bit and Lazarus 32bit 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.
- Creating the 32bit applications using a Windows 64bit to Windows 32bit 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 64bit and wish to develop applications for Windows 32bit, 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 64bit (that must be installed) and will allow to build and debug 32bit 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 64bit 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.
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 64bit 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 64bit Intel compatible CPU.
How must we proceed to build an executable for Windows 32bit? 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.
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 64bit binary for Windows 32bit. Trying to build an application with these settings will result in an error message as shown on the screenshot below.
The screenshot below shows the successful cross-compilation with target OS=Win32, CPU=i386 on my Windows 10 64bit. 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 32bit application intended to run on Windows 32bit with an i368 CPU.
Remains to test the executable. I did it by running my "Clock" on Windows XP 32bit. Success - as you can see on the screenshot.
Note: If building 32bit 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.