Auto-Compiling Shared Libraries

Post your Gambas programming questions here.
Post Reply
User avatar
Posts: 125
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State

Auto-Compiling Shared Libraries

Post by Cedron » Saturday 30th March 2019 4:35pm
(18.44 KiB) Downloaded 313 times
One of the nice features of Gambas is the ability to use external shared libraries (.so) files in Linux. Not only does this allow you to expand the functionality of Gambas by employing existing libraries, but you can also write your own. A primary motivation for doing this would be an increase in speed for computation intensive tasks such as image processing or signal processing.

For documentation, the Gambas Wiki page to start with is:
There are further links at the bottom.

For the first few libraries I wrote, I was using Pluma (a text editor) to edit the source code and a "makefile" to compile it. Pretty simple actually, I could edit the source code file, press "Save", click on a terminal window, hit up arrow to bring back "make", then press [Enter] to compile. Any errors would show up in the terminal window. If there were no errors (always the case, right?), I could then click on my task bar for my Gambas test program, press "Run", and test.

What could be easier? Well, I found out that I could use the "File/Open File..." menu option in Gambas and include my source.c file in my project and edit it directly in the Gambas IDE. This takes the text editor out of the equation, but I still needed to go to a terminal window to compile.

Wouldn't it be nice if I could get Gambas to automatically compile the library code if it needed it? So, a bunch of questions to the mailing list, research, trial and error, I came up with the attached solution.

Most of the code is located in a file called "RunSh.module". This needs to be included in the Gambas test project. The file can be copied directly into the .src directory, or you can keep the file in a common spot and create a soft link in the .src directory. The latter is how it is done in the attached example.

Then, these few lines need to be included in the very start of your test program:
'---- Compile the Shared Library if Necessary        
        Dim theResult As String
        theResult = RunSh.MaybeCompileLibrary("Soup")
        If Len(theResult) > 0 Then
           Print theResult
        '   Me.close   ' Include this when inside a Form_Open
If it is a clean compile, then the result will be empty and the test program will run. Otherwise, the error messages will be printed to the console and the test program isn't run.

It is assumed that there is a "Gambas" directory within your home directory. Otherwise, you may need to adjust some of the hard coded paths. Unzip the attached file in the Gambas directory. There will be a "libGambas" directory created.

Rather than explain all the detail here, I will leave it to you to install it and look at it to see how it works. It is very straightforward.

To create a new test program/library, do the following steps.

1) Create a Gambas Project in the ~/Gambas/libGambas directory.

2) Copy the libGambas_Soap.c and files from the Soup directory into the new project directory.

3) Rename the .c file to the new name.

4) Edit the with the name change as well.

5) Copy the RunSh.module link from the Soup/.src directory into the new source directory.

6) Include the above lines at the start of your test program.

Why the long name format of "libGambas_(name).so"? Well, when it is ready, you can copy it (as administrator) into the /usr/lib directory and then you can use it from any Gambas program without the full path specifier in the Extern statement. Or you can do as I do, and put a soft link to the file in the /usr/lib pointing at the compiled .so file. This allows you to recompile the library and have it immediately available globally without having to play administrator again.

This post is already too long, so I will end it here. Happy to entertain any questions or comments.


SOUP = Shared Object Utility Procedures
.... and carry a big stick!

Post Reply