Creating a library

Post

Posted
Rating:
#1 (In Topic #935)
Avatar
Trainee
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 :

Code (gambas)

  1. ' Gambas module file
  2.  
  3.  
  4.    Return a + b
  5.  
  6.    Return a - b
  7.  
  8.    Return a * b
  9.  
  10.    Return a / b
  11.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
Did you read this page…
/doc/library - Gambas Documentation

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

I made this…

Code (gambas)

  1.  
  2. ' Gambas module file
  3.  
  4.  
  5.  
  6.  
  7.   Return Split(sArg, " ").Reverse().Join(" ")
  8.  
  9.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Trainee

BruceSteers said

Did you read this page…
/doc/library - Gambas Documentation

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!
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
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 /doc/library - Gambas Documentation 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.

Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Trainee

thatbruce said

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] Connexion
** [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?
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
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…
Image

(Click to enlarge)


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
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’

thatbruce said

p.s. @Bruce S: re /doc/library - Gambas Documentation 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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’

BruceSteers said

thatbruce said

p.s. @Bruce S: re /doc/library - Gambas Documentation 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 said

 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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Trainee

BruceSteers said

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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
You could always compile and install gambas yourself using the autotools method..
you can get the commands needed on this page i made..
Gambas Upgrade Commands
Just select your system (i chose debian-stable for the following)

<HIGHLIGHT highlight="shell">
# Run the following commands in a terminal (copy & paste)…

# Download the source
git clone –depth=1 Gambas / gambas · GitLab –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

</HIGHLIGHT>

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 README.md · bruces-patched · Bruce Steers / gambas · GitLab
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
 Aah hang on. i think i understand. It's probably an issue with a QT only system.
you probably need to use gb.gui.qt not gb.gui or something.

The other bruce will know about these problems better as he also uses a QT only system.
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Trainee
 Thanks Bruce. Compiling from source means I don't need to install backports, which can be risky. I haven't done it yet but I'll let you know if I get any problems. As I said, I don't need the gtk libs but no harm in having them I guess.
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
And one other "noice" thing about compiling from source is that it is entirely "package hell" free.  I got so sick of "you cant install that because you haven't installed
* gambas.xxx.yyy.zzz.alpha_plural_z.rpthnigo/dbpk_umlaut" stuff. Package installation is OK for the unbaptised but not if you really want to learn, use, grow, get surprising errors etc

Oh and another tiny little thing is that it also let's you (ahem) play around a bit, perhaps better said as "experiment", with Gambas itself. But noooo :o , no-one would want to do that would they? (No-one specifically in mind but their first name might (or might not) accidentally rhyme with the first name of someone quite close to me.

 regards,
no-one in particular (rhymes with "obtuse", "Dr Seuss", "fruit loops" (and apparently with a bit of imagination) "soups")


p.s. Has anyone (else) wondered why the packager emits so many un-needed debian files? I'll get back to you-all later. Maybe 3-5 days, before the end of the month or Christmas or so, definitely before 2028.
(How's that for a "project manager" effort estimate!)
.

Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
thatbruce is in the usergroup ‘Regular’

Median Joe said

Thanks Bruce. Compiling from source means I don't need to install backports, which can be risky. I haven't done it yet but I'll let you know if I get any problems. As I said, I don't need the gtk libs but no harm in having them I guess.

To be serious.  :o  Go for it, if you have even the slightest of problems, take two aspirin and call back in the morning with a small sample (by which I mean what distro, what you tried and a very short last 13 lines of the terminal output).
Common mistakes include
* not reading https://gambaswiki.org/wiki/install?nh&l=en page until your eyes bleed
* not having the entire build stack for your distro installed (this has caught me out a few times)
* using a distro that is not exactly designed for developers, just for the great unwashed (Mint leaps to mind)
* not understanding that you need the library source headers, not just the libraries themselves

Good luck, don't panic, phone home….
rgrds
thatbruce

Online now: No Back to the top
1 guest and 0 members have just viewed this.