Setting path of the Native Library for DllImport on Mono for Mac

我的未来我决定 提交于 2019-11-30 20:27:46

Let's split this into two questions: how to set environment variables and how to bundle native frameworks in a MonoMac application.

Setting Environment Variables

You can set environment variables in the LSEnvironment section of your application's Info.plist, like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
            <dict>
            <key>CFBundleIdentifier</key>
            <string>com.yourcompany.TableViewTest</string>
            <key>CFBundleName</key>
            <string>TableViewTest2</string>
            <key>CFBundleVersion</key>
            <string>1</string>
            <key>LSMinimumSystemVersion</key>
            <string>10.6</string>
            <key>NSMainNibFile</key>
            <string>MainMenu</string>
            <key>NSPrincipalClass</key>
            <string>NSApplication</string>
            <key>LSEnvironment</key>
            <dict>
                    <key>Foo</key>
                    <string>Bar</string>
            </dict>
    </dict>
    </plist>

It seems like to have to manually edit that file once and add at least one environment variable.

The file is automatically created by MonoDevelop, so all you have to do is to add the LSEnvironment section.

After that, you can edit them in MonoDevelop: go to project options, "Mac OS X Application", "Advanced".

Bundling Native Frameworks in a MonoMac Application

You don't need to set any environment variables to bundle a native framework in a MonoMac Application, there's a much easier and cleaner way to do it, which is also similar to how things work in Objective C.

I created a small test applications, which bundles a framework both in a native Objective C application and in a MonoMac one.

The first thing you need to do is bundle your framework with the app. There currently is no way of doing this automatically in MonoDevelop, so you need to manually copy the files or use some post-build script (see copy-framework.sh in my example).

I would recommend to put the framework into YourApp.app/Contents/Frameworks/YourFramework.framework as that's how XCode handles it; see also Apple's Documentation.

To reference a library inside your application bundle, you can use `@executable_path' (see the dyld man page).

I would recommend to create an app.config file using <dllmap>, so you don't need to put any pathnames into your code, thus making it easier to change framework versions. For instance:

    <configuration>
       <dllmap dll="TestFramework" target="@executable_path/../Frameworks/TestFramework.framework/TestFramework" />
     </configuration>

If the actual library inside your framework starts with lib or ends with .so / .dylib, then you must specify that name (the above dllmap won't file TestFramework.framework/libTestFramework.dylib, for instance). This is a bug in Mono, which I just fixed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!