Friday, September 12, 2008

The Big Switch: Moving from Windows to Linux with Kylix 3

Original Source click here

Level: Introductory

Bob Swart (drbob@chello.nl), Author, Trainer, Consultant and Webmaster, Bob Swart Training and Consultancy

31 Oct 2002

One of the great things about using tools and databases such as Borland Delphi 7 Studio and IBM DB2 Universal Database is that moving between platforms is only a matter of a few changes and a recompile. Author Bob Swart takes you through the process.
Show developerWorks content related to my search: DelphiShow developerWorks content related to my search: Delphi


© 2002 International Business Machines Corporation. All rights reserved.

Introduction

One of the great things about using tools and databases such as Borland® Delphi 7TM Studio and IBM® DB2® Universal DatabaseTM is that moving between platforms is only a matter of a few changes and a recompile. In the first installment of this series, I created an application in Delphi 7 Studio that connects to an IBM DB2 UDB, and in my second article, I added some more functionality such as master-detail reporting to make the application more interesting and useful. Now that I've created an application that is functional, let's go ahead and take a big jump and move it from a Delphi 7 application running on Microsoft® Windows® to a Linux® (on Intel architecture) application using Borland KylixTM 3.



Back to top


Moving to Linux is easy with Kylix 3

Borland Kylix 3 is a rapid application environment for Linux development. Kylix uses Delphi 7 Object Pascal (as well as C++) as the development language, as well as a familiar RAD environment for Linux. One of the greatest benefits the Borland tools offer, is that moving an application is less of a "porting" project than a recompilation of your Windows/Delphi (or Linux/Kylix) project.

When moving the application from Windows and Delphi 7 to Linux by recompiling it with Kylix, there are a few things we must keep in mind. First of all, all versions of Kylix contain the TSQLClientDataSet component, which is also present in Delphi 6 but is deprecated and is replaced by TSimpleDataSet in Delphi 7. While this may not seem like a big issue, it is an issue once you realize that this means that the TSimpleClientDataSet will only compile with Delphi 7, and the TSQLClientDataSet only without Delphi 7.

Moving an application from Windows to Linux involves taking the source files and transporting them from Windows to a Linux file system. On Linux, filenames (and unit names) are case sensitive, so you must take care of that if you added some units of your own (Delphi 7 will make sure all units and filenames are named with case sensitivity in mind). However, you do not need to move all project files over from Delphi to Kylix. Specifically, the .cfg and .dof files contain Windows specific options. These files will be regenerated by Kylix as .conf and .kof files. In short, you only need to transport the main project file (.dpr and .res) and the unit files (.pas and .xfm) from Windows to Linux. (The code files used in this article are available at the end of this article.)



Back to top


DB2 on Linux

Apart from using Kylix on Linux, we must also connect to one of the various versions of DB2 on Linux (of course, you can always connect with DB2 databases on other platforms as well). One note: I've tried DB2 Universal Database Personal Edition v7.x and encountered an error message: SQL Error: Error mapping failed. This message has been reported by some other people using dbExpress and Kylix, and can be solved in this case by making sure to use the latest version of DB2 on Linux, namely the v8.1 beta, which can be downloaded from the IBM web site.



Back to top


Minor source code changes

After the source files are moved from Windows to Linux, open the project in Kylix (see Figure 1). When you open the project and look at the SQLConnection properties in the Object Inspector, you see two property values that are Win32 specific, and will not work on Linux: the LibraryName (set to DBEXPDB2.DLL) and the VendorLib (set to db2cli.dll). Linux doesn't have DLLs; it uses .so files instead. To change these values, you must first select another value for the ConnectionName property, and then reselect DB2Connection as ConnectionName again. This will set LibraryName to libsqldb2.so.1 and VendorLib to db2cli.dll.

The good news is that these two properties are the only things you have to change when you move the project over from Delphi on Windows to Kylix on Linux. The bad news is that it has to be done manually. Making these changes manually every time a new change is made on one platform is not something you want to do. The good news again is that we can solve this problem by writing some code in the OnCreate event handler of the form, by assigning the correct values to the LibraryName and VendorLib (as well as the Caption of the main form), as follows:

 
procedure TForm1.FormCreate(Sender: TObject);
begin
{$IFDEF MSWINDOWS}
Caption := 'Delphi 7 talking to DB2 using dbExpress';
SQLCOnnection1.LibraryName := 'DBEXPDB2.DLL';
SQLCOnnection1.VendorLib := 'db2cli.dll';
{$ENDIF}
{$IFDEF LINUX}
Caption := 'Kylix 3 talking to DB2 using dbExpress';
SQLCOnnection1.LibraryName := 'libsqldb2.so.1';
SQLCOnnection1.VendorLib := 'libdb2.so';
{$ENDIF}
cdsEMPLOYEE.Open;
cdsEMP_ACT.Open;
cdsEMP_PHOTO.Open;
cdsEMP_RESUME.Open
end;

Note that we cannot simply add {$ELSE} after the first {$IFDEF}, since Linux isn't the only alternative for MSWINDOWS anymore, now that Delphi 7 Studio ( in the preview command-line compiler for .NET) also supports CLR as a platform.

Another property that you have to check is the FileName property of the TSQLMonitor component, which should not use / or \ characters, but point to a single filename for example. Finally, I should note that the MidasLib, dbExpDB2 and CrtL units are not available on Linux, so you have to deploy your executable with the Midas.so and dbexpdb2.so libraries.


Figure 1. Kylix application at design-time
Kylix application at design-time

After you have a single source project on Windows and Linux, you can either work in both Delphi and Kylix (which can cause complex merges of the project source code), or work in one environment and only recompile in the other. Personally, I always design my applications using the Delphi IDE on Windows, and then transport and recompile the application on Linux using Kylix. That way, I can let the SQLConnection component point to the Windows DLLs for DB2 (instead of the Linux .so libraries).



Back to top


Summary

With Delphi 7 or Kylix 3 and DB2, you don't have to be locked into one platform because of tool or database availability, or sacrifice a RAD environment when building Linux applications. In my next article, I will use the Delphi WebSnap components in combination with the dbExpress data access layer to build Web server applications with a DB2 backend.




Back to top



0 Comments: