@executable_path in constant

Good afternoon

Have a simple test project with a universal libusb-1.0.dylib I copy in the build settings over to the framework folder.

To access that library I added a constant with

@executable_path/../Frameworks/libusb-1.0.dylib

But using then that constant with FolderItem(libusbpath) doesn’t work…
So what would be the correct way so that @executable_path is converted?

thanks in advance
richard

I don’t know about the constant, but you may use:
App ExecutableFile FolderItem

Okay…this works:

dim ff as New FolderItem(App.ExecutableFile.Parent.NativePath + "/../Frameworks/libusb-1.0.dylib")

Might be that @executable_path isn’t working correctly anymore…but it was when I look at an older thread here from 2022.

What’s wrong with:

Var ff = new FolderItem ("", FolderItem.PathModes.Native)

Hmm…and what does that do?

Gets the current path?

Gets you the path to the executable - the item you double-click to run the app. The .exe on wondows, the .app on macOS, and the executable file on Linux.

Only gets me a syntax error…

Anyway…doing it as I described above…

Yes sorry it should be:

Var f as new FolderItem ("", FolderItem.PathModes.Native)

Easy enoiugh to fix.

Still odd why @executable_path isn’t resolved…was just this year as this was suggested:

I don’t think that reference is for Xojo code but for the plugin being created.

Well I saw in an older thread that someone uses this as a constant in Xojo…

But then again…have two solutions now (o;

Would be nice if Xojo already offers in the CopyFile “Framework” as destination, that there would be a SpecialFolder.Framework equivalent (o;

@executable_path does work. It’s used in my WeatherKit and macOS toolbar products, but keep in mind that its use differs between macOS and iOS. On macOS, the app bundle contains a MacOS folder which contains the app binary and a Frameworks folder which contains the frameworks, like this:

Contents
  MacOS
    AppName
  Frameworks
    Lib.dylib

And the path for accessing a dylib should be

@executable_path/../Frameworks/Lib.dylib

On iOS, the app is at the top level like this:

Contents
  AppName
  Frameworks
    Lib.dylib

And so the path needs to be written as:

@executable_path/Frameworks/Lib.dylib

It’s also important to remember that in some cases these folder names are case-sensitive, so it’s always important to match what’s in the bundle.

1 Like

I’m also going to point out that if you’re accessing a custom framework, you can’t just point to it in a declare and expect it to be loaded. For that, you also need to use a call to dlopen:

Public Function LoadLibrary(frameworkName as string) As Boolean
  // Loads the multipeer framework for use by your app
  #If TargetMacOS Or TargetIOS
    // Make sure you add a copy files step to copy your library to the Frameworks folder
    Declare Function dlopen Lib "/usr/lib/libSystem.dylib" (name As CString, flags As Int32) As Ptr
    Dim p As ptr
    #If TargetIOS
      p = dlopen("@executable_path/Frameworks/" + frameworkName + ".framework/" + frameworkName + "", 13)
    #ElseIf TargetMacOS
      p = dlopen("@executable_path/../frameworks/" + frameworkName + ".framework/" + frameworkName + "", 13)
    #EndIf
    If p=Nil Then
      Declare Function dlerror Lib "/usr/Lib/libSystem.dylib" () As CString
      Dim reason As String = dlerror()
      Raise New RuntimeException(reason)
    End If
    Return p<>Nil
  #EndIf
  
  Return False
End Function
1 Like