headers (.bi) for Implicit linking (Windows)

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

headers (.bi) for Implicit linking (Windows)

Post by Axle »

Hi Forum

This is a question for anyone familiar with creating FreeBASIC includes and headers.

I am currently working through the conversions of the more recent version/s of raylib and some other associated headers.
My goal is to create the headers for Implicit linking only to the raylib+.dll. Following previous raylib.bi examples, and raylib being made up a single file headers, includes both declarations as well as the initialization and functions normally found in the source.c file.

The previous raylib.bi etc include both declarations as well as the initialization and functions, which is necessary for static linking, but not for runtime linking. So I have done the base conversions with all functions included (<- complex and quite a bit of effort).

It just occurred to me that for Implicit linking to the dll I should really only require the header definitions, as all of the functions are in the dll and initialized from the dll.

So, my question is:
1. Do you think I am on the right track in the context of FreeBASIC and creating the headers with only the header declarations for Implicit linking only?
(aka remove all functions/subs from the headers.)
2. As FB relies on numerous dlls with implicit linking, could you point me to a good example from .\FB\inc\*.bi that that makes use of Implicit Linking only? Just as a guide to check myself against.

In reality I should be able to use the dlls (or .so) with explicit linking and not require a header at all, but implicit just makes it a little simpler.
(Just so its clear. The headers will have no capacity for static linking)

Regards
Axle
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: headers (.bi) for Implicit linking (Windows)

Post by dodicat »

Searching for dylibload in the inc folder (and all folders inside the inc folder), there are none.
I think you might have to do the runtime calling functions by hand from a .bi file as a starter.
Maybe you could do some find and replace in the .bi, you could automate it somewhat.
What is raylib.bi anyway?
Any constants should stand OK, how many subs and functions does it contain?
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: headers (.bi) for Implicit linking (Windows)

Post by Axle »

Hey, thanks for the reply @dodicat

"Searching for dylibload..." That's for explicit linking. I want to use implicit linking. I should be able to as if it was a standard header and C source for conversion to FB.bi. I would assume only the C header definitions would be required as the functions are already in the lib*dll file.

raylib is a C99 graphics + GUI library.
There are several hundred, so maybe 10,000 15,000 lines in the source.

I just assume that other examples included in the FB inc/lib folders are also created for static linking, but I want to eliminate any static linking and use the lib.dll only.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: headers (.bi) for Implicit linking (Windows)

Post by dodicat »

#inclib will load dynamic libraries also.
So will lib
simple.bas to simple.dll

Code: Select all

#cmdline "-dll"
#include "dir.bi" 

function list_files (ByRef filespec As String, ByVal attrib As Integer) as string
    Dim As String s,filename = Dir(filespec, attrib) 
    Do While Len(filename) > 0 
        s+=filename+chr(13,10)
        filename = Dir() 
    Loop
    return s
End function



function start() as string export
return list_files ("*", fbArchive)
end function

 
test code

Code: Select all


#inclib "simple"

declare function start() as string
'or
'declare function start lib "simple"alias "START"() as string
print start
sleep 
So long as the dll is in the same folder as the test code.
The import lib libsimple.dll.a is not needed, you can delete it.
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: headers (.bi) for Implicit linking (Windows)

Post by Axle »

Hi @dodicat thanks for the reply.

Not what I was attempting to ascertain, but it is a difficult question and maybe I haven't asked it correctly :)

My question is purely about what is required in the FB header.bi for implicit linking to to a C .dll/.so

I am working with a header only C library that includes both the function declarations as well as the function definitions. Normally the definitions (actual functions) would be in the somelib.c file. If I was wanting to use the library for static compile in FB by linking to the pre-compiled archive.a then I would also need to supply the function definitions from the somelib.c as well as the declarations in the somelib.h

But I don't wish to make any use of the static compile, so can I remove all of the function definitions and leave the declarations only?

Example C:

Code: Select all

// Declaration in somelib.h
int myfunction(int arg_1, int arg_2);
 
// Definition in somelib.c
int myfunction(int arg_1, int arg_2)
{
	return arg_1 + arg_2
}
Example FB bi:

Code: Select all

Declare Function myfunction(arg_1 As Integer, arg_2 As Integer) As Integer

' Do I really need to have the definition when implicit linking only?
' Can I just use the declaration above in the somelib.bi without the following definition.
Function myfunction(arg_1 As Integer, arg_2 As Integer) As Integer
	Return (arg_1 + arg_2)
End Function
fbfrog appears to create header.bi for both static and implicit dynamic linking by default, but I really don't think I need to do all of the function definition conversions and can use the function declarations only. This just removes about 80% of the conversion work and simplifies the resulting FB library if it can be done that way (This would be perfectly acceptable in C).
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: headers (.bi) for Implicit linking (Windows)

Post by dodicat »

If you #inclib "somelib" you just need the declarations.
1)
inclib "somelib"
Declare Function myfunction(arg_1 As Integer, arg_2 As Integer) As Integer
or maybe
extern "c"
Declare Function myfunction(arg_1 As Integer, arg_2 As Integer) As Integer
end extern

or maybe
Declare Function myfunction alias " myfunction" (arg_1 As Integer, arg_2 As Integer) As Integer
or maybe without #inclib

Declare Function myfunction lib "somelib"alias " myfunction" (arg_1 As Integer, arg_2 As Integer) As Integer

If the declarations are in the c source also (prototypes), then it doesn't make any difference to the actual library.
You still have to supply fb declarations to use the functions in the library.
If there are many, maybe a .bi file, if only a few you can do them in the fb source code file.


Example, hex and bin of a -ve integer using the cruntime dll, linking at compile time.

Code: Select all

Extern "c"
Declare Function ltoa  Alias "_i64toa"(As Longint,As zstring Ptr,As Long) As zstring Ptr
Declare Function strtol Alias "strtoull"(As zstring Ptr,As Any Pointer,As Long) As Longint
End Extern


Function intToBase(N As Longint, _base As Byte)As zstring Ptr
    Dim As zstring * 500 buffer
    ltoa(n,@buffer,_base)
    Return (@buffer)
End Function

Function intFromBase(s As zstring Ptr,_base As Long) As Longint
    Return ( strtol(s,0,_base))
End Function

Dim As zstring * 500 s
randomize
for n as long=1 to 5
dim as longint z=-rnd*9223372036854775800
s= *inttobase(z,16)
Print "hex   ";z;" = "; s

Print"check "; (intfrombase(s,16))

s=*inttobase(z,2)
Print  "bin   ";z;" = ";s
Print"check "; (intfrombase(s,2))
print
next n
Sleep
 
The only linking I use is either compile time, as above, or runtime using dylibload.
Compile time links dll's or static (.a) libraries.
Runtime links dll libraries.
Axle
Posts: 67
Joined: May 31, 2022 6:49
Location: Australia

Re: headers (.bi) for Implicit linking (Windows)

Post by Axle »

Thanks for the reply @dodicat.

Sorry for the delay, busy with tertiary assessments.

We have 3 main ways of linking the libray in C:

1. Static using the comped archive .a
To the best of my knowledge only the "declarations" are used from the header as the "definitions" (Actual Functions) are already compiled into the .a archive.

2. Implicit linking to a shared object (.dll/.so) also sometimes called "Compile time dynamic linking"
As above because the definitions are already compiled into the shared object only the header declarations are required. This sets up all of the calls to the .dll and the .dll/.so is stored in the memory space of the application for entire period that the application is open.

3. Explicit linking to a shared object (.dll/.so) also sometimes called "Runtime dynamic linking"
Uses dllload (dylibload in FB) to load the dll to memory and only remains open until dllfree (DyLibFree in FB) is called.

I think I just worked it out from WIIDT Raylib headers... Only need the declarations :)

Code: Select all

declare sub InitWindow(byval width as long, byval height as long, byval title as const zstring ptr)
Previous versions had the static definitions as well.

The raylib.dll was self explanatory, but sorting the raygui header for use with raylib+raygui.dll was a bit more confusing. Looks like WIIDT has dropped all static linking (.a) from the headers the same as I was attempting.
It looks like WIIDT has beat me to doing the raylib V4.2 and raygui.3.2 headers .bi lol
So I will have a go at it next week and then compare with WIIDTs work :)
If I fail I will just use WIIDT includes and see how I go.

Thank you all just the same :)

P.S. I don't know WIIDTs FB forum handle, so If you read this let me know so I can thank you for your work.
Post Reply