linking library issues

General FreeBASIC programming questions.
Game Coder
Posts: 14
Joined: Apr 17, 2017 2:37

linking library issues

Postby Game Coder » Sep 26, 2017 8:53

So I was trying to make this weird overload of a string to test compiling libraries, and I'm having some issue with linking it?

Code: Select all

'ostring.bas
type OString
    private:
        dim _string as string
   
    public:
       
        declare constructor()
        declare constructor(byval rawString as string = "")
       
        declare function raw() as string
        declare sub setRaw(byval setString as string)
       
        declare operator cast() as string
       
        declare destructor
end type


constructor OString(byval rawString as string = "")
    this._string = rawString
end constructor

constructor OString()
    this._string = ""
end constructor

function OString.raw() as string
    return this._string
end function

sub OString.setRaw(byval setString as string)
    this._string = setString
end sub

operator +(byref lhs as OString, byref rhs as OString) as OString
    return type(lhs.raw() + rhs.raw())
end operator

operator OString.cast() as string
    return this._string
end operator

destructor OString()
    'pass
end destructor



Code: Select all

'ostring.bi
type OString
    private:
        dim _string as string
   
    public:
       
        declare constructor()
        declare constructor(byval rawString as string = "")
       
        declare function raw() as string
        declare sub setRaw(byval setString as string)
       
        declare operator cast() as string
       
        declare destructor
end type

declare operator +(byref lhs as OString, byref rhs as OString) as OString

#inclib "ostring"


Code: Select all

'test.bas
#include once "OverhaulCORE/src/types/ostring.bi"

dim x as OString
dim y as OString
x.setRaw("plo")
y.setRaw("of")

print x+y
sleep


I should point out that the code works just fine if I just put the test at the end of ostring.bas, so it's not faulty code, I'm just doing something wrong with the linking...
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: linking library issues

Postby fxm » Sep 26, 2017 9:12

For a static library:
(see documentation at: Static Libraries)

Code: Select all

'ostring.bi

type OString
    private:
        dim _string as string
   
    public:
       
        declare constructor()
        declare constructor(byval rawString as string = "")
       
        declare function raw() as string
        declare sub setRaw(byval setString as string)
       
        declare operator cast() as string
       
        declare destructor
end type

declare operator +(byref lhs as OString, byref rhs as OString) as OString

Code: Select all

'ostring.bas

#include "ostring.bi"

constructor OString(byval rawString as string = "")
    this._string = rawString
end constructor

constructor OString()
    this._string = ""
end constructor

function OString.raw() as string
    return this._string
end function

sub OString.setRaw(byval setString as string)
    this._string = setString
end sub

operator +(byref lhs as OString, byref rhs as OString) as OString
    return type(lhs.raw() + rhs.raw())
end operator

operator OString.cast() as string
    return this._string
end operator

destructor OString()
    'pass
end destructor

Code: Select all

'test.bas

#include once "ostring.bi"  '' instead, put your own path
#inclib "ostring"           '' instead, put your own path

dim x as OString
dim y as OString
x.setRaw("plo")
y.setRaw("of")

print x+y
sleep

Remark:
- We can also put the line ' #inclib "ostring" ' in the "ostring.bi" file as above (and not in the "ostring.bas" file), but I find that a bit crooked.
- To can also work with a dynamic library (dll), we have just to add the specifier 'Export' at the end of the first line of each procedure body in the "ostring.bas" file.

Return to “General”

Who is online

Users browsing this forum: fxm, Majestic-12 [Bot] and 3 guests