Cross Platform Programming

One area where class libraries really come into their own is in enabling you to write your programs in such a way as to allow them to be run on multiple operating systems without having to change any of your own code for any specific platform. This can be done with class libraries and still preserve the use of the individual operating system's native routines.

How is this done? Well all of the common functionality such as reading from the keyboard and mouse, writing to the screen, etc. need to be concealed within classes in your class library. You create identically named classes for each operating system that you want to support that call that operating system's in-built functions or where the operating system doesn't have the specific function built in you write the necessary code to perform that function in a way that the operating system does support. Each of these identically named classes will need to be stored separately in locations that identify which operating system that they are for.

Once you have built identical libraries of all of the routines that you need for each of the operating systems that you want to support you can compile each of these libraries separately using the appropriate compiler for the individual operating system so as to produce a collection of class library modules that contain identical functionality for each of your operating systems.

All of your programs can now be written so as to call the classes that you have in these operating specific libraries whenever they need to perform any of the functions that the operating system may provide. Because you have coded identically named routines with the appropriate functionality for each operating system you no longer need to know which operating system that you are coding for when you write your code. You have successfully concealed all of the operating system specific processing within these class libraries.

Now all that you need to do when compiling your program for the different operating systems is to link in the appropriate library of operating system routines that corresponds to the operating system that you are compiling for. All of the rest of the modules that your program needs to link in are now operating system independent and you can link in the same source code for each operating system that you are developing for. The only thing that you need to watch for now is to make sure that your modules have all been compiled for the correct operating system when you make your program as with a given module having a single name, only one object compiled from this code will exist at any given time and this will be for whichever operating system that you last compiled it for which may not be the same operating system as you are now trying to make your program. To resolve this you should ensure that all of your program modules get recompiled when you switch the operating system that you are compiling for rather than just the ones where you have made changes.

Of course you don't really need to go to the trouble of building your own class library that will make your programs operating system independent because someone else has already done this for you. Several of the class libraries listed on my page What is a Class Library? have pre-built libraries for DOS text, DOS Graphics, Windows 16 bit, Windows 32 bit, OS/2, Mackintosh, Unix Curses, Unix Motif, and a range of other operating systems so that once you have purchased the modules for those operating systems that you want to support you can proceed straight into writing operating system independent programs. If you later decide that you want to support an additional operating system then you just purchase the additional module and recompile to produce a finished program.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow