Problem to compile -dll (solved)

Linux specific questions.
Post Reply
EkBass
Posts: 10
Joined: Oct 21, 2020 16:54
Contact:

Problem to compile -dll (solved)

Post by EkBass »

Hi.

I got this working with Win 11 yesterday, but now im on Ubuntu Mate. Am i missing something here?

Code: Select all

' 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.
EkBass
Posts: 10
Joined: Oct 21, 2020 16:54
Contact:

Re: Problem to compile -dll

Post by EkBass »

Ok.

Code: Select all

fbc -c fb-lib.bas -o fb-lib.o
gives me fb-lib.o

Not sure how to proceed more lol
EkBass
Posts: 10
Joined: Oct 21, 2020 16:54
Contact:

Re: Problem to compile -dll

Post by EkBass »

Lol, ok. Bit more further.

gcc -shared -o fb-lib.so fb-lib.o

Im still struglin how to use it though.

Code: Select all

// 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

Code: Select all

/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
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Problem to compile -dll

Post by dodicat »

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

Code: Select all

#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.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Problem to compile -dll

Post by deltarho[1859] »

Using 'randomize timer' every time we enter foo is a waste of CPU time. There is also a danger of sequence overlapping.

Use 'randomize' outside the function. timer is not needed either — it is implicit.

So just use 'randomize' before foo is called.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Problem to compile -dll

Post by deltarho[1859] »

While I am at it, I should tell you that using 'randomize' or 'randomize timer' is not the best way to seed, especially if you boot every day.

This is what I use.

Code: Select all

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.
EkBass
Posts: 10
Joined: Oct 21, 2020 16:54
Contact:

Re: Problem to compile -dll

Post by EkBass »

Morning.

Thanks dodicat, seems like i was trying something way too fancy here lol.

@deltarho[1859]
You are right, but i just fuzzed up some test code to see how i can do lib like this that works for python or nodejs.
Post Reply