Welcome to LjGww's Blog

Here we will see...

Tuesday, January 17, 2012

Happy New Year with the old programming language with a brand new compiler

Ahh, windows dll's, 32bit programming, .net, inet, sockets...

Honestly, I find idea to use runtime libraries really fine programming approach. Once upon a time I have hit for the first time in my life a multiuser multitask multiprocess environment. With understanding of that environemnt issue of programming got a really new dimension to me. I was at that time using PC (in DOS at home and at work) and VAX computers (at work). VAX had runtime libraries. Limited memory (16Mb at that time was huge offer) was asking for some optimization. Vax had fortran, pascal and c compiler and basic interpreter. Quite fine programmers offer. Yet all of these languages (except basic) had approach - you write/compile a program and you get very small executable image - wow how efficient, but later I have realized by exploring system in more depth that all of these executables relied on Run-Time-Libraries. What was really neat on VAX is that it was smart on conserving memory. One copy of run time library for each language was almost always running on the system and all instances of programs in particular language were using it. Which basically means that each program had to reserve its own data memory section and compile calls to the library and some specifics of algorithm you are writing. And you could run many programs at once and even do some interaction between them.

At home situation was completely different. I had AT PC machine with dos (and 4dos if someone remembers that animal) and of course programmers miracle of that time which can do almost everything lighting fast - Turbo Pascal!!!

I have breath Pascal programming. It had a lot to offer compared to basic: compiling (basic was slow, computers were slow), variety of functions from abstract to direct hardware programming. You could virtually do anything you want and make it as efficient as it can be on contemporary hardware.

Of course, Turbo Pascal worked in smililar fashion as Vax Pascal with a slight difference. Run-time-library was still present but since DOS was single tasking RTL was glued to executable image. Luckily TP RTL was not large for what it had to offer. You cannot easily port code from Turbo Pascal to Vax forth and back. You needed to do some interventions in code. But it was as good as it gets.

After some time someone came with C and things have gone other way (C was appealing because it had shorther syntax and we programers are lazy to type). Yet, many years later using C, DB/clipper, and many languages, Turbo Pascal remained a niche tool. Whenever I needed to make something that would augment and fulfil some special programming need, Turbo Pascal proved to be handy. I made a ton of desktop publishing projects in Turbo Pascal working with textual files or calculating complex coefficient while calculating values of bonds and at that time rampant inflation rates. TP was versatile tool of choice. Period!

Then Pascal went to history... I moved to C++, ASP, VB, PERL, HTML, PHP, JavaScript, SQL ... yada... yada.. DOS changed to Windows, from VAX i moved to UNIX etc etc...

Recently, I negotiated to make a novel approach to some established system. Mathematical in nature, internet in background and limited in options, have specialized language that is just good enough that you cannot say its bad. After some research I have got the hint that you can extend that system with custom written .DLL. So lets explore DLL writing nowadays.

All of you that programm windows on a daily base know that Microsoft after many years of so called COM approach will sell its own grandmother to influence programmers not to use DLL's i.e. Component Object Model (COM) and make DLL hell on the way :). This goes to direction as you already know, of .net approach. Virtual machines :) installation blah blah..

In essence this is again not much different than vax, honestly I do not see much of a difference between RTL and VM's (virtual machines). In essence they are not far from each other. Your code is compiled in some meta-code and then that meta-code access ready made library to be executed. Is it RTL that is glued or dynamically linked or it is VM that is executing meta-code, I find that matter of taste.

But, for this project, I needed "ugly" 32bit DLL!!! And what's worse, I needed it to be compatibile with the system I am using. That system is REALLY PICKY how that dll shall be. I assumed it was written in C/C++ so it looks for standard COM DLL (c-ish like). So I took a ride of research which tool to use. Of course I could think nothing and write in C, but yet again WHICH C?

As it turns out, Microsoft was not a good solution as their all effort is on .net. All old Visual Studios are far out of fashion and support. Microsoft - dead end. Another option was open source C (in a form of GCC for example). I am not that familiar with alternative programming systems on windows that are not web based, applications are also developed in Java... every path that I have explored have lead me to some dead end. Can't use this because of that, this is not for that, you cannot do this in that, this is not intended to be used in this platform or in this way. Oh, Gosh... give me something that does not offer iPhone programming :) I WANT TO WRITE BLOODY UGLY COM DLL in WINDOWS 32!!!! :)

And then a short example of using Lazarus with this system i needed to extend - on some russian web site. Some months ago I was lightly researching pascal on mac, not out of need, but out of curiosity. Downloaded lazarus, tried to do something and then I was drawn to some other things. Life matters :)

So I made a test .dll in lazarus (downloaded one for windows), compiled, mounted dll in a system (wrote some additional interface code) and let it work. WOOOOOW! it worked where all others failed. And it worked STABLE. I left it working for a whole week, with all computer going to sleep and waking up. NO CRASH! I was awed.

Again, I was drawn away by other things I had to do in the meantime and started designing a new system that is mixture of dlls, socket connectors, management, calculations.... still yet on paper / drawing board but once i make all decisions code will unfurl. I have said let me make some socket examples ... some client some server and see maybe I will use web server maybe sql db maybe nothing of that at all, but lets go basic - make socket example.

Number of years ago I was writing some chat and some kind of remote controller in VB on windows sockets. It was frustrating, but it was fun in the same time, and at the end it was working fine. Socket (internet) programming shall be kinda separate programming course IMHO as much as web programming is.

What initially I did not realize that beneath Lazarus there was some strange animal that was taking many forms. Actually very soon I have realized that Lazarus on its own is not Pascal compiler (unlike Delphi and Turbo Pascal). Rather Lazarus lean on multifaceted animal called FPC or Free Pascal Compiler.

So I set to quest to explore it further. Then I really got awed!!! FPC was rare kind of animal that have enriched my programming world! FPC works on Windows, Linux, Mac, can write programs for iOS too, did not find anything for android as yet but do not take for granted that it would not support that too (like iOS, android is linux/unix derivate). Can Cross-Compile (compile for one platform on another one). Can mimic Turbo Pascal, Delphi, Objective Pascal, MAC Pascal... it can make code for MacClassic as much as it can make code for MacOSX both carbon and coccoa, of course it has strong Windows 32 and 64 bit support (looks very native to windows) and also do the same trick on Linux. Is it a time to go back to PASCAL? I think its worth exploring.

Oh yes, documentation is quirky, in some parts not explained in much detail, some examples may not be working, community seems not large and populated with game developers??? as it looks like. But do not forget some things. MacOS Classic is fully written in Pascal. Skype is written in Pascal. And this compiler is for at least 10 years on the road. Does not look too buggy! Some lab tests are required.

And oh yes two. I made small example of client and server working with sockets. Compiled, debugged on windows, until happy proof of concept, then I wandered what Mac will do, moved code to Mac, compiled on mac and made socket connection. Guess what? It worked. Not only that it worked, but the SAME SOURCE that I have compiled on windows, i compiled on Mac with NOT A SINGLE CHANGE, compiled the same like it was always on mac. And worked as it was written on mac. This I find really awesome! OK you may say its not the same when you write application with windows controls etc, this is simple console application, and Yes I will agree that when you wish to make some decent application you need to interact with user with windows and mouse, show some data, fonts and graphics, but if I can write at least 60% of code to be same between platforms I am already happy as puppy!

One more significant thing is that FPC is OPEN SOURCE!!!! You may have new official Pascal bangs and whistles from Borland called embracadero http://www.embarcadero.com/products/delphi but if you do not feel forking out many $$$ and you wish to have good pascal compiler that can do many things and do it well explore FPC Free Pascal Compiler ....

And yes, in a new year they have just released latest version 2.6.0. Even versioning looks convincing. Not to low not to high....

Some links where you can start from:

http://www.freepascal.org/download.var get compiler here
http://www.freepascal.org/docs.var equip yourself with PDF books/manuals here (html online manuals sometimes get handy too)
if you get confused ask questions here http://community.freepascal.org:10000/
Want IDE? get lazarus http://lazarus.freepascal.org/

that shall cover most of your bases. Cheers! Happy New Year!!!!

1 comment:

  1. Couple of additional comments on some concepts.

    Pascal was first language in my life that offered structures, both data and programming structures. With pascal units, programmer could break large application into smaller module chunks. Pascal Units also offered pra-OO approach in hiding some non-public functionality (public to a main program) using INTERFACE and IMPLEMENTATION parts.

    Of course once you get ride with OO approach you have another dimension of programming. Pascal was a bit out of that waters but as I understood in the meantime I was away Pascal added objects, inheritance, classes (in some variants), method and data visibility. And some additional functionality such as virtual methods but did not shed off units. I was never Delphi programmer and some of you I know have extensive experience programming in Delphi so something that is new for me is not likely new to some of you.

    I am amazed with the combination of units and objects in Pascal. This is a bit novel thing for me OO is however not novel thing for me. So there is some catchup for me to do with extension to language itself.

    Would like to hear from you how you see these two things intermixed.