Static lib example - with thread



The directory structure should look like this:



  [top]

lib     src     prog




Three directories below one.
Files in each:

prog -- compile.sh    and   p1.bas

src -- h1.bas  l1.bas  l2.bas  l3.bas

lib -- will be blank to begin


Put these files into the correct directory.
I use linux. This means - I always have to pay attention to permissions. A chore, but a good thing.
YOU must have permissions on the directories and files. If you use linux, suck it up: get used to chown, chmod. This will save you so much trouble, later. Make them all owned by you, with rwx permissions for all. You can change it later if you worry. I assume you have a good, somewhat current FreeBASIC installation.

------------------- compile.sh --------------------- prog dir -------------

{file begin}

#!/bin/bash
#
# compile.sh
# compile and put lib where it belongs
# you have to make this executable

cd ../src

fbc -mt -lib -exx l1.bas
fbc -mt -lib -exx l2.bas
fbc -mt -lib -exx l3.bas

mv ./libl1.a ../lib
mv ./libl2.a ../lib
mv ./libl3.a ../lib

cd ../prog

fbc -exx -mt p1.bas

{file end}
-------------------- p1.bas ------------------------ prog dir -------------
{file begin}

' p1.bas

' test static libs with thread

#include "/rdkl/FB/examples/static_lib/src/h1.bi"

' this is the main program. Without the included stuff - pretty small

dat0.uu1 = 17       ' created in h1.bi

print
print "before"
print
print dat0.uu1
print dat0.ss1

print

xform(dat0)       ' call the subroutine
print
print "after"
print
print dat0.uu1       ' data changes
print dat0.ss1
print

print

makethread(thrptr, @dat0.uu1)       ' thread prints until key pressed

print


{file end}
-------------------------------- h1.bas ---------------------- src dir ----------------

{file begin}

' h1.bi

' test static libs with thread

' include data - each files needs at least some of this

#libpath "/rdkl/FB/examples/static_lib/lib"

#inclib "l1"
#inclib "l2"
#inclib "l3"

type t_data

as string ss1
as ulong uu1

end type

declare sub xform(ddd as t_data)
declare sub makethread(newthread as any ptr, tdata as any ptr)
declare sub xxp
declare sub threadsub(xptr as any ptr)

dim shared as t_data dat0
dim as any ptr thrptr

thrptr = @threadsub


{file end}
--------------------------------- l1.bas -------------------- in src dir ----------------------------

{file begin}

' l1.bas

' test static libs with thread

#include "/rdkl/FB/examples/static_lib/src/h1.bi"

sub xform(ddd as t_data)
' test static lib and var declare

ddd.ss1 = str(ddd.uu1)

end sub

sub xxp
' just print something when called

print "+";

end sub

{file end}
--------------------------------- l2.bas ----------------------- in src dir -------------------------

{file begin}

' l2.bas

' test static libs with thread

#include "/rdkl/FB/examples/static_lib/src/h1.bi"

sub makethread(newthread as any ptr, tdata as any ptr)
' make a thread - pass the correct pointer types
' wait for the thread to die

dim as sub ptr nthd
nthd = cast(sub ptr, newthread)
dim as any ptr rthrd

rthrd = threadcreate(newthread, tdata)

print "make thread"

threadwait(rthrd)

end sub

{file end}
------------------------------------ l3.src ------------------ in src dir ---------------------------

{file begin}

' l3.bas

' test static libs with thread

#include "/rdkl/FB/examples/static_lib/src/h1.bi"

sub threadsub(xptr as any ptr)
' This is the thread - it will run until a key is pressed

while inkey <> "" : wend       ' clear any key in buffer

print
print
sleep 100
print "this is thread !!!"       ' announce that we are in thread sub
sleep 100
print
print

while inkey = ""
sleep 75,1               ' tiny pause to allow print
print ".";
if inkey <> "" then exit while        ' test for any key press
xxp                                   ' call another sub - because we can
wend

while inkey <> "" : wend       ' clear that buffer - never know what comes next

end sub

{file end}
---------------------------------------------------------------------------------

The compile.sh must have been made executable.
Run this script from a console.

You should have a p1 in your prog directory and a set of 'libl(x).a' files in your lib directory.

You should see some text and numbers on the screen, and some scrolling characters.

-------------------------- some things to keep in mind ------------------------

These only show what you can do.

DON'T DO IT THIS WAY.

static lib notes

threading notes

FB stuff

FB home