Lazarus for Cross-Platform Development

Lazarus may be the most native cross-platform development environment running on Linux, Windows and Mac OS X. Use it to create native applications with platform-independent code.
Code Completion and Cross-Editing

Most modern IDEs have some code-completion features. The IDE uses Codetools to parse the sources. Codetools is a library of parsers, search and refactoring tools and is independent of a specific compiler version. This allows the IDE to handle several versions of the compiler and to switch between them easily.

It also supports cross-editing. For example, it's possible to develop under Linux and write code for Windows. When a cross-platform compiler is installed, simply set the target OS to Windows in the IDE. The IDE code navigation and code-completion features now will work as if you were working under Windows. The following example illustrates this:

{$IFDEF Linux}
// write code for Linux here

The curly brackets are compiler directives and work the same as C preprocessor directives. The code between the directives will be skipped by the compiler except when compiling for Linux. The IDE is a little bit smarter. When a different target operating system is active, the enclosed code will in most instances act like a comment. However, tools such as find declaration still will work within this code so that you don't have to switch the target too often. Some other macros that may be of use are:

{$IFDEF MSWindows}
// code for all kinds of windows
// code when using GTK2 as widget set
{$IFDEF big_endian}
// code for big endian processors like the powerpc
// code for 64 bit processors

Generally though, high-level code doesn't need these macros, because the FPC system libraries provide most of the cross-platform functions and constants that you'll need. For example, the Lazarus IDE, with about 200,000 lines of code, uses them in less than 100 places.

Cross-Platform File Handling

There are numerous functions for cross-platform file handling that automatically use the correct path delimiter, case and other system specials. Instead of using the slash to separate directories, you should use the constant PathDelim. Under Linux, double path delimiters are treated as one, so you can concatenate filenames simply. This does not work on MS Windows, where empty directory names are allowed. Therefore, filenames should be normalized with one of the following functions:

  • TrimFilename: removes leading and trailing spaces, combines double-path delimiters and does some minor cleanup.

  • CleanAndExpandFilename: expands the ~ for the home directory under Linux, trims the file as above and chomps any trailing path delimiter.

  • CleanAndExpandDirectory: works the same as CleanAndExpandFilename, but appends a delimiter if missing.

A very useful function is CompareFilenames, which compares two strings encoded in UTF-8 in the usual manner for the operating system. Under Linux, it compares them case-sensitively and distinguishes the various encodings of an a-umlaut. Under Mac OS X, the filesystem is usually case-insensitive, and all a-umlauts are normalized and treated the same. CompareFilenames does not check the actual filesystem, which might be case-insensitive. It is a quick compare function for sorting filenames.

When your application needs to store some configuration files, use the function GetAppConfigFile to get the standard directory. Under Linux, this is /home/username/.config/projectname/. For configuration files, standard formats like XML or INI files are recommended, which can be created by the easy-to-use classes TXMLConfig in the unit xmlcfg and TIniFile in the unit INI files.

Every operating system has its own idea of an application. Windows embeds the Explorer icon and version information in the binary. Since Windows XP, a manifest file can be added to enable theme support. Under Mac OS X, an application is called an application bundle and is a directory with several XML and resource files. In the OS X Finder, the directory is shown as executable program, and the real files are hidden. A graphical application without this bundle file can be started but does not receive any input. The IDE automatically creates and updates these special files and structures for you.

Extending the IDE

Many packages extend the IDE with useful tools and graphical editors. Some examples follow.

For cross-platform OpenGL development, Lazarus provides a simple component named TOpenGLControl, which can be used on any LCL dialog. There are more-advanced third-party packages, like GLScene and Asmoday, that provide an object-oriented API for OpenGL.

You can write cross-platform dæmons that run under Linux as dæmons and under MS Windows as services with the lazdaemon package.

There are several cross-platform packages for databases. For example, the sqldblaz package provides cross-platform access and cross-database access to many common database systems.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Finnally, I have made a Puppy Linux remastered with Lazarus.

Alejandro Lavarello's picture

I have tried months to install Lazarus in Puppy Linux 4.3.1, and I finally have success!

The result is a .iso file , 330MB : just burn to CD , reboot your PC and start developing in Lazarus!!

The MD5 sum is fa94102ecf1bf5c29f629148667399fc , and you can download from one of these locations:


(user: puppy , password: linux)

You need a PC with at least 480MB of RAM, because all is loaded in memory; yes, I know, it is a disadvantage.

Another way is to install plain Puppy 4.3.1 (Frugal installation on disk), with devx_431.sfs and this .sfs created by gposil,

In this way, you can run Lazarus in a PC with 128MB of RAM.

I hope this will help people that have trouble installing Lazarus in Linux.

No garbage collection

Anonymous's picture

Free Pascal lacks garbage collection and you can't even have smart pointers like in C++. Manual memory reclamation is not a feature of a modern language.

Oh please

Anonymous's picture

garbage collection is only making mess. Who cares if you get a memory leak due to unhandled exception? Nobody. Good programmers know how to manage memory and don't need anything else thats only slowing app down...

Why is so difficult to install Lazarus in Puppy Linux?

Alejandro Lavarello's picture

I am a electronic technician and have used Borland Pascal for controlling devices. When Windows 95 becomes popular, I cant obtain Delphi, and have programmed in Visual Basic. Now, I am interested in programming in Lazarus. I am using the Puppy Linux distro, but I am tired to trying to make Lazarus work. Please, anybody can make a .pet or a .sfs or a Puppy remastered with Lazarus working?

Another question: why the Lazarus installer do not contain ALL the stuff? In Windows, Lazarus installs without problems. But I am disappointed with Linux, because each distro neeeds different installation procedures. This is a great Linux failure.

> why the Lazarus installer

Anonymous's picture

> why the Lazarus installer do not contain ALL the stuff?

Because the dependencies can be obtained easily from distro's repository, and it's of course a waste of space to include the installer along with them. Another problem, those dependencies depends on installed libc version which can't be predicted easily. Free Pascal (and therefore, Lazarus) itself is OK since they're not linked againts libc.

Every Linux distro is a separate world

Anonymous's picture

I see .deb and .rpm installers for Lazarus.
It is a shame that every Linux distro has different installer packages.
For end user, each distro is equal to a different operating system.

Writing components

Motaz's picture

I'm using Delphi for more than 10 years, and following Lazarus for many years. Thanks to Lazarus, I discovered a new area of developments, which is component development. Lazarus is needing a lot of components and packages. It is very easy in Object Pascal language to write a package.
Now I'm doing a web development components similar to Delphi's WebBroker. Now CGI part is working. I'm developing this component under Linux and it works just fine under Windows without any modification.
They only problem that I faced in Linux is Arabic language support in linux. Current stable version is still using GTK 1, and Unicode support is exist in GTK+2 which supported by not-stable daily snapshots.
Fortunately I can use Arabic with my web applications under linux, because no need for GTK1 or 2 :)

> Current stable version is

Anonymous's picture

> Current stable version is still using GTK 1, and Unicode support > is exist in GTK+2 which supported by not-stable daily snapshots.

AFAIK since version GTK2 is already set as default widgetset on linux, marking its stability has reached a suitable point for development. Either you're using an older version or you take a conslusion from the wiki (which is often not up to date).

I'm using and the

Anonymous's picture

I'm using and the default is set to GTK, GTK2 is marked as beta still.

Pascal as language has been

Javier Santo Domingo's picture

Pascal as language has been understimated too much, but it features many important things since its creation. And more: it has evolved really well, in many ways in many of its dialects. I think it has what it takes to be the next king of strongly typed languages.

Compiler speed is important

Mario Ray Mahardhika's picture

Many programmers state that compiler speed isn't that important compared to generated code quality. Imaging you're working on a big project that gets changed very often, how long do you spend time watching your compiler compiling your code?

My experience in building large projects, such as QT and GCC, shows that compiler speed is important. QT took 6 hours to build (even with make -j 256!), GCC bootstraps itself (compiler, binutils, standard libs) in more than an hour.

FPC and Lazarus are also big complex project. But do you know how long does it take to build both (for FPC, it includes compiler, helper tools, IDE, standard libs, and tons of extra libs)? 10 minutes! This is a great time saver everybody!

My First project with lazarus is done!

liangby's picture

About 2 years ago, I porting a win32 application writing in delphi to linux.The first choice is kylinx, But it is too old for most linux, very hard to use on Centos 5. But I found lazarus, It help me complete that. And the application had sucessed runing on Centos and ubuntu, is cost down about 1,000RMB for me/PC!
A great project!

We do Delphi development so

Anonymous's picture

We do Delphi development so when the time came to start developing applications for use on Linux systems, Free Pascal and Lazarus seemed like the natural choice. Free Pascal is really quite nice, but unfortunately Lazarus isn't quite as nice. After using Lazarus on a number of projects we found it still had too many problems to make it worth while to continue using it. There were always large regressions between major Lazarus releases. Every time we upgraded we had to spend time figuring out why the applications that used to work just fine in the previous release suddenly no longer worked the same in the current release.

Eventually we abandoned Lazarus and moved to just building Win32 applications and running them with Wine. This approach, of course, had issues of its own but once the teething problems were sorted out our applications ended up working better and with more features than their Lazarus-based ancestors. This is partly because there is simply more choice of components and libraries for Delphi than there is for Lazarus, partly because there was a lot of code we could reuse between the "Linux" applications and the "Windows" applications, partly because the Delphi development environment is stronger than Lazarus with more and better integrated tools, and partly because the Delphi + Wine platform has by and large turned out to be more stable.

Back to Pascal? I Can Hope!

Ro's picture

Like Paulo Pinto, I cut my teeth on Pascal in the MS-DOS days and was sad to see it disappear under the weight of C and, particularly, C++.

The object-oriented versions that limped along simply seemed to be half-baked hotch-potches.

Perhaps this will give me a way back to multi-platform Pascal. Goodness knows, it would make my life much easier!

Thanks for the review :-)

Lazarus vs Delphi

Anonymous's picture

Lazarus has still a long way to go comparing it with its commercial competitor Delphi.
Don't get me wrong: I would love using Lazarus if it would compile faster and create smaller binaries, but most important, it has to be rock stable.
I'm a using Delphi as a professional developer for about 10 years now.

Oh come on!!!

Anonymous's picture

When did Pascal become "an easy to learn language"? It shouldn't come as a surprise that it is not in widespread use since it works only with Pascal which..... need I say isn't much used these days. Extending the IDE is always nice but there aren't too many third-party plugins available for it.
Nice review of Lazarus though.

Pascal was always a nice

Paulo Pinto's picture

Pascal was always a nice language. Did you know that the first versions of MacOS were written in a mixture of Pascal and Assembly?

I remember the days were most of my MS-DOS coding was done in Turbo Pascal, and even my first coding attempts in Windows 3.x were done in Pascal.

But C got more widespread, mainly thanks to C and having a language standard defined more tailored for doing systems programming.

The Pascal standard was only suited for teaching and we had to rely on manufacturer extensions to be able to do real life applications. When the second revision of the standard came out, it was already to late.

For me, Pascal will always have a place on my heart.

I'm working with lazarus for

devel's picture

I'm working with lazarus for years and it is amazing!
It is great to compile it in linux and windows with excactly the same code!

Cross-platform is a breeze

Graeme's picture

Excellent article Mattias! Our company moved over to the Free Pascal Compiler and Lazarus IDE four and a half years ago. The support is fantastic, the products are very stable and development is continuous.

The Object Pascal language is also very nice - miles ahead of the old Pascal from the 80's. A clean and strongly typed language that you can learn very easily. It supports full object oriented programming and combined with a very fast compiler and excellent component libraries, this is a win-win situation for any developer or company. Object Pascal deserves a lot more attention in the developer circles.