Lazarus - Chapter 05

Embed Size (px)

Citation preview

  • 7/28/2019 Lazarus - Chapter 05

    1/2

    Chapter 5Target Platforms

    By Felipe Monteiro de Carvalho

    Lazarus programs can be compiled to run on three major computer platforms:Windows, Linux/Unix, and MacOS X. That is fewer platforms than the FreePascal compiler is able to target. The reason for this is that appropriate compiler

    capability is not the only condition that must be met before you can create aLazarus program for a new platform.

    The following three conditions have to be fulfilled before you can install and use Lazaruson any platform:

    A current Free Pascal compiler for the must be available. At themoment these are available for Windows 32, Windows 64, Windows CE, MacOS X,Linux, FreeBSD and OS/2 (and eComStation) and (to a certain extent) for Solaris.

    A current Free Pascal compiler for the must be available. At the moment

    these are available for x86, x86_64, PowerPC, PowerPC64 and Arm(ARM4-instruction set only the THUMB instruction set is not supported).

    There must be a available for the operating system platform.Currently fully supported are Windows, Windows CE and MacOS X, and also theLinux/Unix widget-set Gtk versions 1 and 2, and the platform-independent Qt 4(Unix, MacOS, Windows).

    This third requirement is the reason it is not possible to create OS/2 programs withLazarus. A Free Pascal compiler for x86-OS/2 exists, but there is no support for theOS/2 presentation manager and for eComStation. In theory it should be possible toinstall Lazarus under XFree86/2 and create OS/2 programs for X. Nobody has tried thisyet, but all the requirements can be met: there is a compiler and there is a port of Gtkversion 2.

    There are huge differences between the four basic platforms Win32/64, WinCE, MacOSand Linux/FreeBSD/Solaris. For that reason it is important if you want to createapplications to run on multiple platforms (or if you intend to port software from oneplatform to another) to get acquainted with the details of using Lazarus on each of theseplatforms before making a start. That way you can investigate beforehand what problemsyou might encounter, and so make the right choices when designing software to run on

    all the desired platforms without problems. With some experience you can then developsoftware completely on one platform, and simply recompile your program to make itavailable on additional platforms.

    target platform

    target CPU

    GUI library

    Lazarus - the complete guide 279

  • 7/28/2019 Lazarus - Chapter 05

    2/2

    Chapter 5 Target Platforms

    This chapter gives a general overview of this theme, explaining the main features of thedifferent operating systems, and also shows you how to make use of the native APIs ifnecessary (although you should normally avoid doing this if you are trying to createportable software).

    At first sight it would seem very easy to make sure programs developed with Lazarus willrun on all platforms. If you use only libraries that exist on all target platforms, and if youuse only calls to the Free Pascal RTL and FCL and to the Lazarus LCL your programswill work on all supported platforms, in theory, without change. because thereare still many operating system differences to keep in mind. If everything is done in theright way the RTL, FCL and LCL will hide or minimise these differences, but we may alsohave to work around them (and if the need arises we may have to correct code manually).

    It gets more difficult if we need functionality that is not provided by Lazarus's libraries.It that case we will be forced to find another library that does offer such functionality onmultiple platforms (hopefully on all of them), or we will have to work with the low-levelAPIs of each operating system to implement a solution for each platform ourselves.In these cases we will have to use .When we do that, parts of the source code for the different operating systems will needto be separated. Free Pascal offers the sequence of commands

    to facilitate this.

    However, having to interlace source code for different operating systems in large routinesin this way is not ideal. A cleaner solution is to provide separate source files, each offeringa solution for one of the platforms. A good way to handle this in Free Pascal and Lazarusis to make use of the meta-command to include these parts of thecode into the source. A common convention is to create one subdirectory per platformand put all platform-specific code in the appropriate subdirectory.Anything between the conditional commands and will be used only if thecondition given after is True. This applies to the include statements too.We can include a file for just one operating system with a one-line construction in the

    code.

    A more elegant way is to use macros in search paths. If we include $(TargetOS) in thesearch path for include files, we can simply use:

    In theory

    conditional compilation

    $ifdef $endif $ifdef

    $ ifdef condition . . . $else . . . $endif

    $include filename.inc

    {$ifdef Linux} {$include linux.inc} {$endif}{$ifdef Win32} {$include win32.inc} {$endif} / / . . . etc

    {$include unit1.inc}

    .

    In that case the include file for Win32 might be located in: Win32/unit1.incwhile the include file for Linux could be found in:In this way all Linux-specific files can be stored together in one directory.Macros can also be used for unit search paths.

    Linux/unit1.inc

    .

    Lazarus - the complete guide280