June 02, 2019

Writing libraries with C++


On the first look, the C++ language seems a bit outdated. The sourcecode contains pointers, the language syntax is changing constantly and for programming a simple GUI application C++ isn't very userfriendly. Any other modern language like C# or Python looks more pleasant than C++. So what is the reason for C++?
The answer is not located in the language itself, but with the ability to write a library. A programming library is together with an operating system a powerful instrument which simplifies programming. What a C++ programmer is doing is not to write the sourcecode in C++, but he is using existing sourcecode written before in his application.
Before a library can be utilized the library has to be available in compiled machine language. And exactly this is the reason why C++ is used so much in modern software development. It is the number one language for writing libraries.
Let me give an example. The C++ system provides two libraries, one for painting pixels to the screen and the other for calculating the sinus value for an input number. Both libraries can be combined into a quick&dirty sinus-plotting program. All what the user has to write by it's own is a for loop in which the numbers are converted into sinus values and plot to the screen. This allows the programmer to write an advanced piece of software without having it programmed by himself.
On the other hand, expert programmers who are familiar with the sinus-function can implement the routines in highly optimized C++ code. They can test their code until it's perfect. This allows many other programmers to build on top of the routine their applications.
A list of some Open Source C++ libraries is available at https://en.cppreference.com/w/cpp/links/libs The consumer can utilize the APIs for writing his own software. The ability of C++ to create powerful precompiled libraries is the main reason for the success of the language. Other programming languages like Javascript, PHP, C# or Pascal doesn't provide such a high amount of existing fast-running system libraries.
In the http://linasm.sourceforge.net/ project the Assembly language was used to create a library. The speedup over the normal Glibc library which is written in plain C is around 1.5 up to 3 times faster. The main speed up is located somewhere else. Because the enduser can include the library easily and has access to all the sinus and logarithm function which are already there. He builds on top of the existing code new programs.
The concept of using a programming library is the reason why modern computing is so powerful. The question is not in which programming language software was written in, but the question is, which existing libraries and operating systems were used. This explains, why Python is perceived as the most powerful programming language available. Not because the Python syntax is so great, but Python has access to the entire C/C++ library universe. Programming something in python has mostly to do with import a library and use it in a for-loop. This allows to create complex gui applications in under 100 lines of code which are doing something useful.
Other programming languages like C# or PHP have gain their strength also from the underlying library. If somebody likes to publish a new programming language, he has at foremost create a large amount of existing libraries. This makes it easy for the user to write his own programs. For example, the PHP language comes with powerful libraries for creating server applications.
How complicated is the task to use an existing library written in assembly language in the own program? The task is surprisingly easy. The programmer needs only the name of the routine for example for determine the sinus of a value, and then he calls the routine.
Here https://github.com/oded8bit/Assembly-Lib is another example which is called a MS-DOS graphics assembly library. The idea is, that all the major functions are available and the programmer can use the functions in his own program. Even the programming language was Assembly which is not very newbie friendly, it is a pleasure to use such library for practical purpose. The library provides also functions for mouse input and sound playback so it can be compared with a pygame like game framework. The most impressive example is the demo-program in which a line is plotted in purely assembly:
grm_DrawLine 10, 10, 200, 200
Pretty easy? The secret is, that it's only a function call to an existing program code which is defined in the library. And the enduser doesn't care what the line-drawing function is doing in detail. The interesting point is, that the understanding how important a library is wasn't there in the 1980s. In many magazines for the Commodore 64 and the MS-DOS computer the enduser was explained everything about programming and the underlying hardware, but the number of articles about using existing libraries was limited.
Suppose, somebody likes to program the Commodore 64 in pure assembly language. The first thing to do is to search for a library, then he is using the library and this allows him to create his game as easy as writing a python program. That means, with an existing library, Assembly will look like Python.