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.
Linux API calls from PB
Re: Linux API calls from PB
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
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
Re: Linux API calls from PB
I think that your questions are very good.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'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
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
Keep it BASIC.
Re: Linux API calls from PB
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
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
Re: Linux API calls from PB
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)?idle wrote: most of the time you can use ImportC to work with linux libraries rather than using openlibrary
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))
Re: Linux API calls from PB
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
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
Re: Linux API calls from PB
objdump -x programname looks pretty usefull. Thanks.
-
- User
- Posts: 37
- Joined: Fri Oct 29, 2010 11:29 am
Re: Linux API calls from PB
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.
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.
Re: Linux API calls from PB
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