I’ve started to learn about writing custom dylib plugins by using the StructAndEnum example from the Plugins SDK, but I’ve run into an issue with the plugin path not being found when placing the dylib within the app bundle.
Using Xcode 11.3, I was able to compile a 64-bit plugin with an extra test function returning a Single:
static float HelloWorldTest() { return 42.0; }
Hooking it into REALmethodDefinition and compiling it, I placed the plugin directly into Xojo 2020r2’s Plugins folder (per the SDK doc’s suggestion for testing) and launched the IDE: works fine when call
Var answer As Single = HelloWorldTest()
and my hard-coded value in the dylib returns as expected. Also, the provided example Xojo code also works just fine.
However, I’d like to be able to put the dylib into the built app with a Copy Files build step. Doing this breaks my test and the example code.
The docs don’t seem to mention how to handle the dylib outside, and my own attempts to Soft Declare to it have fallen flat, ie:
Soft Declare Function HelloWorldTest Lib “@executable/…/Frameworks/StructAndEnum.dylib” () As Single
And I’m not even sure how to go about accessing the example’s custom structs and enums.
I’m assuming it’s a path issue, but multiple variations on the Lib path still gets me a framework not found error.
I’ve done a deep dive into the forums but haven’t found a solution that I’ve been able to get working. I’m hoping I’m missing something obvious as I’ve been hammering away at it for the past few days.
@Christian Schmitz - Because I’ve used another 3rd party dylib before in a similar manner, so I thought it would run similarly. This is my first foray into writing my own dylibs, so I’m learning lots.
@Sam Rowlands - I’ve amended my original post where I mistyped the number of periods. Even with two, it fails for me.
I’ve modified the path multiple times, dotting the path up and down to see if I could hone in on it. I’ve tried a full native path to no avail. I even double-checked with a FolderItem and it says it exists, even copying the file to Terminal for a path to cut and paste. I’m stumped.
I’m with Christian, why not treat this as a Plugin and move it into your Plugins folder so you can reference it in your code? Also, your HelloWorldTest function does not appear to be exported (considering it’s a static function) so even if you got the right declare it would not find that function anyway.
In case this is really important to you, we do look next to the actual executable (i.e. not the bundle directory but the actual executable found in the Contents/MacOS directory). So you could move your dylib in there and reference it using a simple
Declare Sub MyFunctunion Lib “StructAndEnum.dylib” ()
@William Yu - I must be doing something wrong: I can’t even get your suggestion to work.
What is working in my case is to keep the dylib in Xojo’s Plugin folder.
Upon compile, I see that Xojo automatically places the dylib in Contents/Frameworks and my HelloWorldTest function works. I was afraid that I would still need to bundle the dylib separately after compile, so I’m relieved that this isn’t the case.
Even so, I still can’t figure out why I can’t Declare to it given that the Copy Files build step placed the dylib in the exact same location.
I doubt you’ll get anything to actually “work” in your Declare example since the only exported function in your plugin is likely the one that we explicitly export, and that is our main entry point. When you’re working with our Plugins SDK the functions that you reference are not strictly exported (you might want to read up on C/C++ docs to understand this).
Declare Sub REALPluginMain Lib “StructAndEnum.dylib” ()
Right, we automatically copy all the necessary plugins into your app.
We handle things a little differently with Declares as opposed to when loading up Plugins that were compiled into your app.