Creating a library

New to Gambas? Post your questions here. No question is too silly or too simple.
User avatar
Median Joe
Posts: 15
Joined: Tuesday 8th November 2022 7:30pm
Location: Britain

Creating a library

Post by Median Joe »

A library is just a group of subs and functions but should it have a different format than a normal project? Every project should have a startup method but for a library isn't it redundant?

I tried creating this toy library, following the instructions in the docs :
' Gambas module file

Export 

Public Function add(a As Single, b As Single) As Single
   Return a + b
End

Public Function subtract(a As Single, b As Single) As Single
   Return a - b
End

Public Function multiply(a As Single, b As Single) As Single
   Return a * b
End

Public Function divide(a As Single, b As Single) As Single
   Return a / b
End
I saved the code as a library and the executable is in ./local/share/gambas3/lib (called arithmetic.gambas), but when I try to access it (after loading it) in a new project, using say arithmetic.add(), I get an "unknown identifier" error. I'm probably doing a whole lot of things wrong here, and I suspect it's because I'm still confused about OOP. I'm still stuck in the procedural way of doing things.
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Creating a library

Post by BruceSteers »

Did you read this page...
http://gambaswiki.org/edit/doc/library?ht=library

My discovery....
Do not use main.module in the library.
Make another module in your library source or rename main.module

I made this...

' Gambas module file

Export


Public Function TestRev(sArg As String) As String
  
  Return Split(sArg, " ").Reverse().Join(" ")
  
End
i couldn't get anything until i changed the main.module name to Tester.module

now i can use..

Print Tester.TestRev("this is a test")
i found if main.module is renamed or deleted or left blank and another module made it works as expected.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
Median Joe
Posts: 15
Joined: Tuesday 8th November 2022 7:30pm
Location: Britain

Re: Creating a library

Post by Median Joe »

BruceSteers wrote: Wednesday 16th November 2022 2:04pm Did you read this page...
http://gambaswiki.org/edit/doc/library?ht=library
Yep.
My discovery....
Do not use main.module in the library.
Make another module in your library source or rename main.module
That worked. Many thanks Bruce!
User avatar
thatbruce
Posts: 161
Joined: Saturday 4th September 2021 11:29pm

Re: Creating a library

Post by thatbruce »

I frequently leave the Main module in a library, but the code therein is either to test the pubic routines or to send two thousand volts through the user's keyboard should they try to run it.
A library is just a gambas project that gets "installed" in ~/.local/share/gambas3/lib when you make its' executable, and is thus available to you when running one of your projects that refers to it (by adding it to the Libraries tab in the Project properties popup). The foremost purpose of a library is to share code across your projects. At runtime it also allows you to share data among your class objects by referring to the public library objects.
This .local constraint is a problem if you have multiple users on your machine and they all need to use it but this is surmountable.

Somewhat similarly, no matter what the help says, a Component is just another gambas project with a couple of extra pairs of underpants. A component has to be "installed" and when it is some stuff ends up in /usr/lib/gambas3 and some in /usr/share/gambas3. Thus making it usable by any of your local users. This is very handy, for example we have all our database access enclosed in a component called "phDB20" so all the users can run gambas programs accessing the database without the hassle of setting it up for each user.

Getting back to your original post though. It's not an OOP/procedural thing it's just a shared library (in the Unix shared library sense). So if arithmetic.add() isn't working are you giving it some biscuits to work with like arithmetic.add(2,2)?

Finally, just for fun try running /usr/lib/gambas3/gb.form.terminal.gambas in a console. See, even Benoit and the other gambas coders leaves their test routines hanging around!
regards
bruce

p.s. @Bruce S: re http://gambaswiki.org/edit/doc/library?ht=library MY GAWD! It is so many years since I wrote that! It may not be entirely correct to the nTH level nowadays. The best bit is the precedence order for the location searches. It took me several megadays of reading the gbx(probably gbx2 at that stage!) C code to decipher what was actually happening.
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
Median Joe
Posts: 15
Joined: Tuesday 8th November 2022 7:30pm
Location: Britain

Re: Creating a library

Post by Median Joe »

thatbruce wrote: Saturday 19th November 2022 9:33am Getting back to your original post though. It's not an OOP/procedural thing it's just a shared library (in the Unix shared library sense). So if arithmetic.add() isn't working are you giving it some biscuits to work with like arithmetic.add(2,2)?
Yes I did and I realize now it was nothing to do with OOP (although I'm getting the hang of that now. Familiarity breeds contempt, as they say :D ), since simply renaming the main.module worked, as BruceS suggested.
Finally, just for fun try running /usr/lib/gambas3/gb.form.terminal.gambas in a console. See, even Benoit and the other gambas coders leaves their test routines hanging around!
I did, but not sure if this is the result you were expecting?
**
** OOPS! INTERNAL ERROR. Program aborting, sorry! :-(
** Cannot find interface of library 'gb.gtk3.x11'
**
** ERROR: #27: Cannot load component 'gb.gtk3.x11': cannot find component
**
** Please send a bug report to the gambas bugtracker [1] or to the gambas mailing-list [2].
** [1] http://gambaswiki.org/bugtracker
** [2] https://lists.gambas-basic.org/listinfo/user
**
I get this same output in the console of the IDE when running a program after having selected a new project and then "Graphical Application".
The error refers to Gtk3 but there are explicit options for these, so what is a graphical application?
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Creating a library

Post by BruceSteers »

Does your desktop use x11 or is it wayland?

If you have all of gambas installed and it cannot load an x11 component it's probably because your desktop is wayland, then you cannot use any x11 components.

wayland sucks, i'd recommend installing an x11 version of your desktop environment.

If you click "About" in the gambas IDE it will tell you what your environment is...
Untitled.png
Untitled.png (156.9 KiB) Viewed 2187 times
if you are using x11 and getting that error then you are probably missing some gambas components that need installing.
sudo apt-get install "gambas3*"


A "Graphical application" refers to any GUI, using gb.gui , gb.gtk3 , gb.qt5 , gb.gtk3.wayland , gb.qt5.wayland , etc
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Creating a library

Post by BruceSteers »

thatbruce wrote: Saturday 19th November 2022 9:33am p.s. @Bruce S: re http://gambaswiki.org/edit/doc/library?ht=library MY GAWD! It is so many years since I wrote that! It may not be entirely correct to the nTH level nowadays. The best bit is the precedence order for the location searches. It took me several megadays of reading the gbx(probably gbx2 at that stage!) C code to decipher what was actually happening.
I tried to edit the page to include something about main.module being hidden but cloudflare just gives a 505 error.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Creating a library

Post by BruceSteers »

BruceSteers wrote: Tuesday 22nd November 2022 11:24am
thatbruce wrote: Saturday 19th November 2022 9:33am p.s. @Bruce S: re http://gambaswiki.org/edit/doc/library?ht=library MY GAWD! It is so many years since I wrote that! It may not be entirely correct to the nTH level nowadays. The best bit is the precedence order for the location searches. It took me several megadays of reading the gbx(probably gbx2 at that stage!) C code to decipher what was actually happening.
I tried to edit the page to include something about main.module being hidden but cloudflare just gives a 505 error.
Yay, I fixed that, one single line was causing Benoits server to refuse edits.
Benoit on M/L wrote: It's the company I'm working for that hosts the wiki. They decided to add a global sniffer that cancel any HTTP request whose contents match "some" patterns.
A really stupid "security" tool that breaks a lot of perfectly valid requests because they include something like "SELECT * FROM" inside (for example).

Of course, they are unable to give me the list of forbidden patterns.
You have to guess!
So i did not guess, I started to copy the page in small sections bit by bit and hitting preview till i found the offending line.
`/usr/bin/<vendor>/<name>.gambas`
I removed the backquotes and prefixed it with 4 spaces to do the same thing and it now is editable :)
Now i've added some info to the library page about how main.module cannot be exported.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
Median Joe
Posts: 15
Joined: Tuesday 8th November 2022 7:30pm
Location: Britain

Re: Creating a library

Post by Median Joe »

BruceSteers wrote: Tuesday 22nd November 2022 11:17am Does your desktop use x11 or is it wayland?
The "About" tells me I'm using x11 and Qt5. The problem seems to be that I installed gambas from bullseye-backports (because I wanted the latest version) although I'm using a distro (Q4OS Trinity) based on Debian stable. It's not a big deal because I only want to use Qt anyway.
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Creating a library

Post by BruceSteers »

You could always compile and install gambas yourself using the autotools method..
you can get the commands needed on this page i made..
http://138.68.116.47/cgi-bin/WebGambasUpgrade.gambas
Just select your system (i chose debian-stable for the following)
# Run the following commands in a terminal (copy & paste)...

# Download the source
git clone --depth=1 https://gitlab.com/gambas/gambas.git --branch=stable $HOME/gambas-stable
cd $HOME/gambas-stable

# Install the dependencies (these are for debian-stable)
sudo apt-get update && sudo apt-get install -y build-essential g++ automake autoconf libbz2-dev libzstd-dev default-libmysqlclient-dev unixodbc-dev libpq-dev libsqlite3-dev libglib2.0-dev libgtk2.0-dev libcurl4-gnutls-dev libgtkglext1-dev libpcre3-dev libsdl-sound1.2-dev libsdl-mixer1.2-dev libsdl-image1.2-dev libxml2-dev libxslt1-dev librsvg2-dev libpoppler-dev libpoppler-glib-dev libpoppler-private-dev libpoppler-cpp-dev libasound2-dev libdirectfb-dev libxtst-dev libffi-dev libglew-dev libimlib2-dev libv4l-dev libsdl-ttf2.0-dev libgdk-pixbuf2.0-dev linux-libc-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libcairo2-dev libgsl-dev libncurses5-dev libgmime-3.0-dev libalure-dev libgmp-dev libgtk-3-dev libsdl2-dev libsdl2-mixer-dev libsdl2-ttf-dev libsdl2-image-dev sane-utils libdumb1-dev libqt5opengl5-dev libqt5svg5-dev libqt5webkit5-dev libqt5x11extras5-dev qtbase5-dev qtwebengine5-dev libwebkit2gtk-4.0-dev git libssl-dev

# configure and compile the sources
./reconf-all
./configure -C --disable-keyring --disable-sqlite2 --disable-qt4 --disable-qtwebkit
make -j$(nproc)

# If no errors it is ready to install so you must first remove the existing apt version to avoid conflicts
sudo apt-get remove "gambas3*"

# now install the compiled version
sudo make install

Downloading and compiling the source gives certain bonuses.
you can keep up to date with the development version.

You can modify things.
I not only compile gambas i have modified it in some areas so my gambas has additional features :)
ExternalTools being the most useful i think.
check out the readme to see some of my extras https://gitlab.com/bsteers4/gambas/-/blob/bruces-patched/README.md
If at first you don't succeed , try doing something differently.
BruceS
Post Reply