Dynamic Linking for MacOS?

BlitzMax Forums/BlitzMax Programming/Dynamic Linking for MacOS?

Is there such a thing as dynamic linking for MacOS, Linux? It seems many licenses restrict static linking.

Thanks

Mike

Yes.

The following illustrates the use of the python shared library using the dynamic linking instructions dlopen and dlsym:

Extern "C"
Function dlopen:Byte Ptr(libname$z,mode)
Function dlsym:Byte Ptr(handle:Byte Ptr,symbol$z)
End Extern

Const PylibName$="libpython.dylib"

Const RTLD_DL_SYMENT=1
Const RTLD_DL_LINKMAP=2

Const RTLD_LAZY=1 'Lazy Function call binding
Const RTLD_NOW=2 'Immediate Function call binding
Const RTLD_BINDING_MASK=3 ' Mask of binding time value
Const RTLD_NOLOAD=4 ' Do Not load the object.
Const RTLD_DEEPBIND=8 'Use deep binding
Const RTLD_LOCAL=0
Const RTLD_GLOBAL=$100
Const RTLD_NODELETE=$1000

Local pylib:Byte Ptr=dlopen(PylibName,RTLD_GLOBAL|RTLD_NOW)

If Not pylib Throw PylibName+" library not found"

Print "python loaded"

Local Py_Initialize()
Local PyRun_SimpleString(script$z)
Local Py_InitModule4:Byte Ptr(name$z,methods:Byte Ptr,doc:Byte Ptr,PyObject:Byte Ptr,apiver)
Local Py_Finalize()

Py_Initialize=dlsym(pylib,"Py_Initialize")
Py_Finalize=dlsym(pylib,"Py_Finalize")
PyRun_SimpleString=dlsym(pylib,"PyRun_SimpleString")

Py_Initialize
PyRun_SimpleString "print ~qHello from Python~q"
Py_Finalize



Awesome. Thanks for the example!

If you want "proper" dynamic linking, as per the -l option that the linker uses (like in Max on Linux), see this post for a description on what to change in bmk to allow it.

:-)