Import binary file
BlitzMax Forums/BlitzMax Programming/Import binary file
| ||
Yasha suggested you can import binary files and use them as static librarys (embedded in executable) but I'm wondering how you would do that exactly. I'm currently using LoadLibraryA and dlopen to open a dynamic/shared library in the same folder as the executable. |
| ||
Ask Yasha for a working example? :-) |
| ||
Aha! It was Incbin. But it doesn't work with LoadLibraryA/dlopen as they aren't native functions.Rem IncBin embeds an external data file in a BlitzMax program that can then be read using the "incbin::" device name. End Rem ' code snippet from demos/firepaint/firepaint.bmx Incbin "stars.png" Local stars=LoadImage( "incbin::stars.png" ) |
| ||
You would have to save it to temp directory then call loadlubrary,dlopen. |
| ||
On Windows, at least, if you have a library.a file in the same folder as your source, you can just do this (from a 10-year old example!):' Import static library... Import "BlitzPTC.a" ' Declare library's functions... Extern Function OpenPTC (title$z, width, height) = "ptc_open" Function UpdatePTC (buffer:Int Ptr) = "ptc_update" Function ClosePTC () = "ptc_close" End Extern ' Call away... Should maybe be Extern "c" or Extern "win32", but still... |
| ||
Woohoo! It works. I was able to just rename libOpenB3d.so to .a and it was happy. Thanks a lot dude. |
| ||
You probably didn't need to rename it if you're on Linux... I'd guess, anyway! |
| ||
No, if I try to use .so it reports this: Unrecognized Import file type: libOpenB3D.so |
| ||
On Mac my archive wrapper won't load .a, and with .dylib I get the same error as .so on Linux. This is the error.dyld: Library not loaded: libopenb3d.dylib Referenced from: /Applications/BlitzMax/mod/angros.mod/openb3d.mod/examples/balls_collision.app/Contents/MacOS/balls_collision Reason: no suitable image found. Did find: /usr/local/lib/libopenb3d.dylib: stat() failed with errno=20 It works fine on Windows. |
| ||
I'm assuming that Mac is looking in usr/local/lib for the .a and it's not there. |
| ||
Strange, I copied the .a to /usr/local/lib and now it reports this.dyld: Library not loaded: libopenb3d.dylib Referenced from: /Applications/BlitzMax/mod/angros.mod/openb3d.mod/examples/balls_collision.app/Contents/MacOS/balls_collision Reason: image not found |
| ||
Ok fixed it. I needed to copy libopenb3d.dylib to /usr/local/lib to resolve a linking issue. Since /usr is a hidden folder I used terminal. I also had to sudo rm lib as it said lib was not a directory, then sudo mkdir lib then cp libopenb3d.dylib /usr/local/lib. |
| ||
I'm pretty sure a .so and a .a are not built in the same way. So I'm surprised renaming a .so to .a and then importing it, worked? |
| ||
Yeah, I was wondering about that... From: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways. 1) Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution. 2) Dynamically loaded/unloaded and linked during execution (i.e. browser plug-in) using the dynamic linking loader system functions. I'm thinking perhaps it's working (regardless of having been renamed -- presumably the linker doesn't care about the file extension) as it's available as what it really is, ie. a dynamic (.so) library, rather than actually being used as a static (.a) library. Does the executable work even if you remove the renamed .so? |
| ||
You're right, they're not the same. It won't work if I rename the library so it is still a dynamic library. On Mac the file needed had to be /usr/local/bin/libopenb3d.dylib and on Windows it was openb3d.dll in the same folder as the exe. On Linux it was libOpenB3D.a in the module folder so I had to add a path to the Import filename like "/usr/local/lib/libOpenB3D.a". I will have to learn how to build openb3d as a static library then. Thanks for the link. |
| ||
I figured it out. For the makefile I used ar -cvq mylib.a $(myobjects) where -cvq is different printout messages. But static libraries can't link to dynamic libraries so I added Import "-lGL -lGLU" etc. and it was happy - and so was I. Cheers! |