The Linux programming language Gambas can use functions from external libraries.
This post gives a few simple examples to supplement the information in the Gambas Documentation.
Gambas is a great language for the Linux platform, and provides a relatively painless transition for VB programmers. One thing that is not as good as VB is the documentation. It is always useful to give programming examples alongside the language command reference information, something that VB6 was brilliant at!
So what follows are just get-you-started examples using Gambas version 3.12.x (there are differences with earlier versions, so my guide may only work with recent releases).
There are several potential benefits from using external libraries; they can allow you to reuse existing Gambas code, or you may want to draw on the power of a library written in the C programming language.
using Gambas libraries
As a simple example I'm writing some
Start a new Gambas project (I've called mine TrivialMaths) as Project Type: Command-line application.
In Project > Properties > General > Project Type set to Library.
Also on the Project Properties dialog, add a Vendor name ....Note: if you leave this field blank, your libraries will be stored in a folder called: (unknown)
Create a new Class source file (e.g. right-click on Sources > New > Class... > Name set to clsMaths and tick the Exported box.
Add code like this:-
Note that Export has been added, and that only Public functions are externally accessible |
Create an Executable. You should now have a library in your file system in a path equivalent to mine:-
/home/steve/.local/share/gambas3/lib/stevedee
(i.e. /home/{your name}/.local/share/gambas3/lib/{your vendor})
Test with a 2nd Project
Now create a second Gambas project to test the new library, this time a graphical one.
Go to Project > Properties > Libraries and select Add.
Adding a Gambas Library |
Select TrivialMaths from the list.
Add a ValueBox, and 2 Buttons. Add code like this:-
Note that we create a new instance of the class: clsMaths |
The test program should run as expected, using the simple functions provided by the library. When typing myMaths the editor in the Gambas IDE should provide a list of functions for myMaths if the library is correctly registered with the test program.
Note: if you make any changes to your library, remember to re-build the Executable. Also note that you may have to close your test project and re-open it for these changes to take affect (using Reload on v3.12.2 didn't seem to help).
You can also add more libraries, but you may have to register libraries with each other if there are any mutual dependencies. For example:-
If your-program uses functions in your-library-A which then uses a function in your-library-B, you will need to:-
- add library-B to library-A list of libraries
- then add library-A and library-B to your-program list of libraries
I hope to do a subsequent post covering the use of external C libraries.
Thank You! I didn't see anything in the Wiki walking the user through the process like this. :-)
ReplyDeleteReally helped my get started!
Thanks Steve.
DeleteIn a professional environment, 'code reuse' is an important & powerful principle. By reusing software that has already been written and properly tested, projects can be completed quicker and a lot of bugs can be avoided.
Its not so important if you are writing code for fun, but still interesting to replace 'straight code' with classes, put code into libraries, or even use code written in other languages to supplement you Gambas projects (see http://captainbodgit.blogspot.com/2019/01/gambas-using-external-c-libraries.html).
And yes, Gambas can be frustrating due to lack of code examples. I guess the clever people that write Gambas can't understand why we ordinary folk have a problem understanding it!
Hello I have a question, how can I do to use libraries that I have saved in the directory of my project and so avoid that the person who will use my program needs to install the library separately, something like that the program already takes the libraries included and does not need to install them separately.
ReplyDeleteI am sorry, I don't know the answer.
DeleteI imagine that when you create a Package so that users can install your program, you will need to include your library executable in Package Dependencies.
Take a look at this information: https://gambaswiki.org/wiki/doc/library
I'm sorry I don't have the time right now to experiment. I suggest you re-post your question on the Gambas forum: https://forum.gambas.one/ as I'm sure one of us will be able to help with this.