' compiled as: fbc -dll -export fb-lib.bas
#inclib "fb"
' Use the `alias` keyword to prevent name mangling
declare function foo cdecl alias "foo"(s as integer) as integer
declare function bar cdecl alias "bar"(s as zstring) as boolean
function foo cdecl alias "foo"(s as integer) as integer export
randomize timer
return Int(Rnd * s) + 1
end function
function bar cdecl alias "bar"(s as zstring) as boolean export
print s
return true
end function
fbc -dll -export fb-lib.bas
ld: /usr/local/bin/../lib/freebasic/linux-x86_64/libfb.a(io_printbuff.o): warning: relocation against `__fb_con' in read-only section `.text'
ld: /usr/local/bin/../lib/freebasic/linux-x86_64/libfb.a(init.o): relocation R_X86_64_PC32 against symbol `__fb_ctx' can not be used when making a shared object; käännetty uudelleen -fPIC-parametrillä
ld: loppulinkitys ei onnistunut: bad value
Some translations from compiler error by me: käännetty uudelleen -fPIC-parametrillä -> recompiled with -fPIC parameters.
loppulinkitys ei onnistunut -> final linking failed:
Last edited by EkBass on Mar 07, 2024 6:47, edited 1 time in total.
// nodejs
const ffi = require('ffi-napi');
// Load the shared library
const libf = ffi.Library('./fb-lib.so', {
'foo': ['int', ['int']],
});
// Call the 'foo' function
const result1 = libf.foo(100);
console.log(result1); // This should print a random number between 1 and 100
/home/ekbass/Projects/fb-lib/node_modules/ffi-napi/lib/dynamic_library.js:75
throw new Error('Dynamic Linking Error: ' + err);
^
Error: Dynamic Linking Error: ./fb-lib.so: undefined symbol: fb_Rnd
at new DynamicLibrary (/home/ekbass/Projects/fb-lib/node_modules/ffi-napi/lib/dynamic_library.js:75:11)
at Object.Library (/home/ekbass/Projects/fb-lib/node_modules/ffi-napi/lib/library.js:47:10)
at Object.<anonymous> (/home/ekbass/Projects/fb-lib/lib-test.js:4:18)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
You just use -dll for a dynamic (.dll /so) or -lib for a static (.a)
You can set the options in the code and compile the code with no option switches.
Example for dll
#cmdline "-dll"
' Use the `alias` keyword to prevent name mangling
declare function foo cdecl alias "foo"(s as integer) as integer
declare function bar cdecl alias "bar"(s as zstring) as boolean
function foo cdecl alias "foo"(s as integer) as integer export
randomize timer
return Int(Rnd * s) + 1
end function
function bar cdecl alias "bar"(s as zstring) as boolean export
print s
return true
end function
If you save the above code as fb-lib.bas you will get fb-lib.dll and Libfb-lib.dll.a.
you can then delete Libfb-lib.dll.a if you wish.
Function GetSeed() As Long
Dim As Long Seed
Asm
rdtsc
bswap eax ' fast moving to upper bits
mov Dword Ptr [Seed], eax
End Asm
Return Seed
End Function
' Test
For i As ULong = 1 To 10
Print GetSeed
Next
Print
Dim tot As long
For i As ULong = 1 To 10^6
Randomize GetSeed
tot += Rnd
Next
Print tot/10^6
Sleep
In your code, simply use: Randomize GetSeed
On my machine, GetSeed takes about 200 nanoseconds.