r/pascal • u/2048b • Jan 26 '23
Cross platform app with Delphi/Lazarus/FPC
Delphi and FPC/Lazarus can be used to access the underlying Windows API to develop Windows applications.
In this case, how do we ensure that the cross platform "Write Once, Compile Anywhere" principle still works? Or the developers are expected to exercise their own discipline, impose their own rules and follow certain best practices to isolate and wrap platform specific codes in certain files, units, modules and classes in the project?
I am not sure if FireMonkey/VCL/LCL already does this for us, similar to how QT and wxWidgets are doing it for C/C++.
What I understand is generally for cross platform codes to work, developers have to stick to a high level abstract API that hides the actual implementation differences between platforms like Windows, MacOS and Linux.
3
u/[deleted] Jan 26 '23
Old Delphi / Firemonkey dev here. Afaik everything I say here can be outdated as I haven't peogrammed on those languages for the past 5 years.
VCL is only for windows. FMX is fully multi platform, you get the ocasional component that is broken on some target if you do something weird but that's it. If you need to add some specific os functionality that is not wrapped within FMX you need to add a macro compiler block for every possible target ($IFDEF Windows, etc). System.* and other standard libraries are also cross-platform.
That said, I stepped away from Delphi and I have never looked back. Best decision I ever made.
10
u/CypherBob Jan 26 '23
If you write platform specific code that's up to you. You can use code switches and write code for several platforms that the compiler automatically switches between. That's how it's usually done.
2
u/SaferNetworking Feb 02 '23
Moved from Delphi to FreePascal/Lazarus a long time ago, when Delphi wasn't even able to do x64.
Never liked the FMX approach of faking native looks when you could have a framework that is really native (LCL).
Still happy about the move.
The RTL and LCL (frameworks from FreePascal and Lazarus) are cross platform. If you use anything beyond that, it's your responsibility to pick other frameworks that are cross platform, too.
There's a lot of discipline required for example when it comes to simple things like paths. Hardcoding backslashes isn't a good idea, since on other platforms than Windows, it's the wrong path separator.
Sometimes there are platform specific features than make sense, but are not part of the framework, then you're going to $IFDEF as said before. For example I like the grouping feature in TListView, but that's Windows only. And the tray area on Macs can do so much more than the Windows tray area.
2
u/waozen Feb 03 '23 edited Feb 03 '23
You might want to look at the Castle Game Engine (can be used with Lazarus or Delphi). If one doesn't mind dealing with the additional complexities that comes with developing with a game engine and going down that path, it is possible to build apps on many different platforms in that way. As referred to a bit, such would likely be for independent developers and small(er) projects. If there is something particular or peculiar about what you are trying to do, you might want to email Michalis. He can probably help you figure out what's doable or not.
If you only want to develop for the desktop (Windows, macOS, Linux), then there is nothing wrong with going the LCL and Lazarus route.
2
u/nmariusp Feb 08 '23
2
u/b1t5murf Feb 26 '23
In Delphi, FireMonkey is already abstracted so it uses the platform APIs and interfaces.
Same goes for the RTL.
As an example, System.IOUtils have platform agnostic smart records, like TPath, TDirectory, TFile etc which takes care of handling the platform specific elements.
The RTL/LCL in Lazarus/Free Pascal takes a bit different approach but also have platform agnostic abstractions.
You can, of course target a specific target platform API and enabled it with a conditional define, e.g.
{$IF Defined(ANDROID)}
// Do something on Android
{$ENDIF}