Linux API calls from PB

Linux specific forum
ColeopterusMaximus
User
User
Posts: 37
Joined: Fri Oct 29, 2010 11:29 am

Linux API calls from PB

Post by ColeopterusMaximus »

Hi All,

As PB in Linux seems to have improved lots I'm trying to write a small program in Linux.

I have been trying to find information about how to use the Linux API from PB and the only information that I could find is this:

"- The Linux API functions are fully supported as if they were BASIC keywords"

However if I try to type: getaddrinfo in the IDE, it doesn't recognize the keyword, and trying to call that function returns "getaddrinfo" is not a function.

Can anyone point me to examples of how to call the Linux API? (And some info on using Linux .so files too if possible)

Most of what I have found in the forum is related to Windows API.

Maybe this is a very stupid question, if so, please have mercy with me, I'm a newbie using PB in Linux.
User avatar
idle
Always Here
Always Here
Posts: 5092
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Linux API calls from PB

Post by idle »

It's not included but it's resident so you need to import it
and you'll need to define the structures and constants if they're not present

Code: Select all

Structure addrinfo 
    ai_flags.l;
    ai_family.l;
    ai_socktype.l;
    ai_protocol.l;
    ai_addrlen.i;
    *ai_addr  ;.sockaddr;
    *ai_canonname.Character;
    *ai_next.addrinfo;
EndStructure;

ImportC ""
   getaddrinfo(node.s,service.s,*hints.addrinfo,*res.addrinfo) 
EndImport 

Global res.addrinfo 

getaddrinfo("127.0.0.1","",0,@res) ;returns zero on success 

Debug res\ai_flags  
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Linux API calls from PB

Post by heartbone »

ColeopterusMaximus wrote:Can anyone point me to examples of how to call the Linux API? (And some info on using Linux .so files too if possible)

Most of what I have found in the forum is related to Windows API.

Maybe this is a very stupid question, if so, please have mercy with me, I'm a newbie using PB in Linux.
I think that your questions are very good.
I'm also a Linux newbie also trying to make it in a Windows centric environment.
Hopefully because of the repeated blunders over the past decade or so by the wizards of Redmond, the focus will change to Linux, but I ain't holding my breath.

Follows is what I've been able to learn so far about how to use the .so files,
although at this point I have no idea why or when I'll be using them. :?:
Perhaps at some time I'll be part of a project that uses them for development.

(Optional) create the source file sodll.pb from the code below,
or simply copy the code into the IDE.

Code: Select all

ProcedureDLL.l addition(a.l, b.l)
  ProcedureReturn a + b
EndProcedure
ProcedureDLL.l subtraction(a.l, b.l)
  ProcedureReturn a - b
EndProcedure
After it is loaded into the IDE compile it into the 'Shared .so' format
using the name sodll.so
Compiler > Compiler Options... > Executable Format > 'Shared .so'
Compiler > 'Create Executable'

From the IDE select 'New',
paste the following code into the new source window.
(Optional) save it into the same folder as the sodll.so.

Code: Select all

If OpenLibrary(1,"./sodll.so")
   result = CallFunctionFast(GetFunction(1,"addition"),5,3)
   Debug result
   result = CallFunctionFast(GetFunction(1,"subtraction"),5,3)
   Debug result
EndIf
Next, select the Compile/Run button to see how the Linux dlls work.
Keep it BASIC.
User avatar
idle
Always Here
Always Here
Posts: 5092
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Linux API calls from PB

Post by idle »

in addition to what heartbone said.

most of the time you can use ImportC to work with linux libraries rather than using openlibrary
to link to libraries that are installed on your system, you can generally link to them with
the "-l" option which will tell the linker to search the library paths for the given name
to see more linking options see the man for LD

so if for instance you wanted to work with libvlc which you'd find as usr/lib/libvlc.a or libvlc.so
you could ImportC the lib with either of these

ImportC "-lvlc" ;grab the current system version shared over static
ImportC "-lvlc.a" ;grab the static version
ImportC "-lvlc.so" ;grab shared version
ImportC "-lvlc.so.5" grab a specific shared version 5
Windows 11, Manjaro, Raspberry Pi OS
Image
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Re: Linux API calls from PB

Post by auser »

idle wrote: most of the time you can use ImportC to work with linux libraries rather than using openlibrary
But doesn't that mean that you have to share your source and even source that could not be shared from you because it's closed (PB-lib)?


For example the binary of this code if compiled on wheezy64 even works on an another system (e.g. much older lenny64) where there is no "/lib/x86_64-linux-gnu/" directory at all.

Code: Select all

Structure timespec
  tv_sec.i
  tv_nsec.i
EndStructure

ImportC "/lib/x86_64-linux-gnu/librt.so.1"
   clock_gettime(id.i,*t.timespec)
EndImport

Define time.timespec
clock_gettime(4,@time)
OpenConsole()
PrintN(Str(time\tv_sec * 1000000000 + time\tv_nsec))
As far as I understand the LGPL right you have to give the user the possibility to replace the library with an modified version of the lib. So I guess you have to do dynamic linking or maybe if statically linked share .obj stuff? But how should a user be able to use another lib if the pathname from my source does not matter at all? Did I miss something here? :?
User avatar
idle
Always Here
Always Here
Posts: 5092
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Linux API calls from PB

Post by idle »

Using ImportC on shared objects vs openlibrary / getfunction more or less achieves the same thing
the difference being ImportC tells the linker to add the references to the libs and functions to the dynamic section
and elf loader resolves them when you start the program.

If you happen to link to a static lib then yes you will be subject to the restrictions of the licence
since it will copy the object code into your program.

As I said the -l option tells the linker to search in the standard library paths for the specified name
in your case ImportC "-lrt" will find librt.so.1 or what ever the current version is
Shared objects take precedence over static but if you ever needed to check use objdump or nm

Objdump of the example
> objdump -x programname

So in your example
Dynamic Section:
NEEDED libc.so.6
NEEDED librt.so.1
...
Version References:
required from librt.so.1:
0x09691a75 0x00 03 GLIBC_2.2.5
required from libc.so.6:
0x09691a75 0x00 02 GLIBC_2.2.5
Windows 11, Manjaro, Raspberry Pi OS
Image
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Re: Linux API calls from PB

Post by auser »

objdump -x programname looks pretty usefull. Thanks.
ColeopterusMaximus
User
User
Posts: 37
Joined: Fri Oct 29, 2010 11:29 am

Re: Linux API calls from PB

Post by ColeopterusMaximus »

Wow, thank you all for your replies, that is exactly what I needed to kickstart.

I also have found that many API functions are already defined as built-in, what happens is that auto-complete for API functions and stuctures is disabled by default (This is a new PB install.)

On Windows I installed PB so long ago that I completely forgot about this when I installed this new Linux version.
User avatar
idle
Always Here
Always Here
Posts: 5092
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Linux API calls from PB

Post by idle »

you can take a look at apifunctions.txt in the compilers folder, it lists all the api's that are available.
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply