Am i trying to create and use this library correctly here?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Post Reply
EkBass
Posts: 5
Joined: Oct 21, 2020 16:54
Contact:

Am i trying to create and use this library correctly here?

Post by EkBass »

Im decently sure i was able to create and use .dll libraries like this maybe 10 years ago. Though they were not built in a same way than back then. They were just a file full of static functions.

So now im bit lost here. How i can use library like this?

Code: Select all

' myType.bas
Type myType
    public:
    dim zxc as integer
    declare function myFunction(someText as string) as string
End Type

function myType.myFunction(someText as string) as string
    if len(someText) = 0 then
        return "You are so quiet there"
    end if
    return "I thought so too"
end function

Code: Select all

' myFile.bas
#inclib "myType"
dim myLib as myType
print myLib.myFunction("Hello")
fbc myFile.bas
myFile.bas(2) error 14: Expected identifier, found 'myType' in 'dim myLib as myType'
myFile.bas(4) error 265: Symbol not a CLASS, ENUM, TYPE or UNION type, before '.' in 'print myLib.myFunction("Hello")'
fbc -v
FreeBASIC Compiler - Version 1.09.0 (2021-12-31), built for win32 (32bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
standalone
target: win32, 486, 32bit
caseih
Posts: 2063
Joined: Feb 26, 2007 5:32

Re: Am i trying to create and use this library correctly here?

Post by caseih »

A dll is just binary code. Type definitions are not part of it. So there's no way a compiler can know what myType is from the DLL. Type definitions and function prototypes typically are in what C and C++ call "header files." In FB the convention is to put them in a file with a ".bi" extension while you then #include into your program that will link against the dll. The .bi files are only for the developer and the compiler; the resulting EXE only requires the DLLs. Hope this helps.
grindstone
Posts: 852
Joined: May 05, 2015 5:35
Location: Germany

Re: Am i trying to create and use this library correctly here?

Post by grindstone »

@caseih: I'm afraid, your response could be a little misunderstandable.

The Type definition has to be done in both the dll and the calling program. Using a .bi file is good custom (and recommendable), but it is not necessary.

Furthermore the procedure(s) of the dll have to be specified "Export".

This way it works:

Code: Select all

' myType.bas
Type myType
    Public:
    Dim zxc As Integer
    Declare Function myFunction(someText As String) As String
End Type

Function myType.myFunction(someText As String) As String Export
    If Len(someText) = 0 Then
        Return "You are so quiet there"
    End If
    Return "I thought so too"
End Function

Code: Select all

' myFile.bas
#Inclib "myType"
Type myType
    Public:
    Dim zxc As Integer
    Declare Function myFunction(someText As String) As String
End Type
Dim myLib As myType
Print myLib.myFunction("Hello")
Sleep
Last edited by grindstone on Jan 13, 2023 12:01, edited 1 time in total.
dodicat
Posts: 7698
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Am i trying to create and use this library correctly here?

Post by dodicat »

You should tag export to the functions you want to use.
Your code

Code: Select all



' myType.bas
#cmdline "-dll"
Type myType
    public:
    dim zxc as integer
    declare function myFunction(someText as string) as string
End Type

function myType.myFunction(someText as string) as string export
    if len(someText) = 0 then
        return "You are so quiet there"
    end if
    return "I thought so too"
end function 
Then to use it

Code: Select all

#inclib "mytype"

Type myType
    public:
    dim zxc as integer
    declare function myFunction(someText as string) as string
End Type

dim as mytype m

print m.myFunction("hello")
sleep
 
Creating a .bi file is optional, but for many functions in a library, (examples in the fb inc folder), then it is convenient.
dodicat
Posts: 7698
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Am i trying to create and use this library correctly here?

Post by dodicat »

OOPS, SORRY GRINDSTONE, I didn't see your post.
We must both be drinking the same brand of beer.
Post Reply