<div dir="ltr">It worked! Took me a while to iron out details, but it is working now. Huge thanks sir, I will name my firstborn after you.<div>Thanks for the <span style="font-size:12.8px">@_cdecl(&quot;initializePlugin&quot;) tip as well, I didn&#39;t know about it and it will be very useful.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I am having slightly related problem now (it was there before, but I ignored it for the time being), not sure if I should start a new thread?</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The PluginInterface module has one external dependency on module A, PluginConsumer has the dependency on module B which has dependency on same module A that the PluginInterface uses. When I load the plugin library, I get bunch of errors like:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Class <a href="http://A.XYZ">A.XYZ</a> is implemented in both libPluginInterface.dylib and libMyPlugin.dylib</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I know why it is there, but I don&#39;t know how to get rid of it. I can&#39;t just remove dependency from PluginConsumer and use the one from PluginInterface (if that would even work?) because PluginConsumer does not depend on it directly, but it is going through module B first</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Cheers,</span></div><div><span style="font-size:12.8px">Lope</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 4 October 2017 at 22:17, Daniel Dunbar <span dir="ltr">&lt;<a href="mailto:daniel_dunbar@apple.com" target="_blank">daniel_dunbar@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">The way that I have done this in the past is pass a protocol as an unsafe pointer to an exposed entry point:<div>```swift</div><div>            let entryPoint = dlsym(handle, “initializePlugin”)</div><div>            guard entryPoint != nil else {</div><div>                fatalError(&quot;missing plugin entry point: \(pluginPath)&quot;)</div><div>            }</div><div>            typealias PluginInitializationFunc = @convention(c) (UnsafeRawPointer) -&gt; ()</div><div>            let f = unsafeBitCast(entryPoint, to: PluginInitializationFunc.self)</div><div>            f(Unmanaged.passUnretained(<wbr>self).toOpaque())</div><div>```</div><div><br></div><div>and then in the plugin convert back to the appropriate type:</div><div><br></div><div>```</div><div><div>@_cdecl(&quot;initializePlugin&quot;)</div><div>public func initializePlugin(_ ptr: UnsafeRawPointer) {</div><div>    let manager = Unmanaged&lt;PluginManager&gt;.<wbr>fromOpaque(ptr).<wbr>takeUnretainedValue()</div><div>```</div><div><br></div><div>HTH,</div><div> - Daniel</div><div><br><blockquote type="cite"><div><div class="h5"><div>On Oct 4, 2017, at 11:02 AM, Ján Kosa via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_1431573529807325761Apple-interchange-newline"></div></div><div><div><div class="h5"><div dir="ltr">Hello folks,<div><br></div><div>I have been toying with dynamic libraries, trying to implement plugin functionality. I was able to get to the point where I can call simple function in loaded library, but I am having troubles starting more sophisticated communication channel.</div><div><br></div><div>There are 3 projects<br></div><div>- PluginConsumer is an app that loads plugin libraries </div><div>- MyPlugin is a plugin implementation, output is dynamic library that PluginConsumer loads</div><div>- PluginInterface is common interface that both MyPlugin and PluginConsumer use, so that they know how to communicate</div><div><br></div><div>My first idea was to have PluginInterface be a simple SPM project with single file where the bare-bones PluginInterface class would be:</div><div><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-s1"><br></span></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-s1">open</span> <span class="m_1431573529807325761gmail-s1">class</span> PluginInterface {</p><p class="m_1431573529807325761gmail-p2">    open func sayHi()</p><p class="m_1431573529807325761gmail-p1">}</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p1">Package.swift file:</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p2">// swift-tools-version:4.0</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">import</span> PackageDescription</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">let</span> package = Package(</p><p class="m_1431573529807325761gmail-p4"><span class="m_1431573529807325761gmail-s2"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    name: </span>&quot;PluginInterface&quot;<span class="m_1431573529807325761gmail-s2">,</span></p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    products: [ .library(name: <span class="m_1431573529807325761gmail-s3">&quot;PluginInterface&quot;</span>, type: .dynamic, targets: [<span class="m_1431573529807325761gmail-s3">&quot;PluginInterface&quot;</span>]) ],</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    targets: [ <span class="m_1431573529807325761gmail-s2">.target(name: </span>&quot;PluginInterface&quot;<span class="m_1431573529807325761gmail-s2">) </span>]</p><div>




















<br class="m_1431573529807325761webkit-block-placeholder"></div><p class="m_1431573529807325761gmail-p3">)</p><p class="m_1431573529807325761gmail-p3"><br></p><p class="m_1431573529807325761gmail-p3"><br></p><p class="m_1431573529807325761gmail-p1">UserPlugin is also very simple project containing only one file:</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-s1">public</span> <span class="m_1431573529807325761gmail-s1">func</span> getPlugin() -&gt; <span class="m_1431573529807325761gmail-s2">AnyObject</span> {</p><p class="m_1431573529807325761gmail-p2"><span class="m_1431573529807325761gmail-s3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span></span><span class="m_1431573529807325761gmail-s1">    return</span><span class="m_1431573529807325761gmail-s3"> </span>MyPlugin<span class="m_1431573529807325761gmail-s3">()</span></p><div>









<br class="m_1431573529807325761webkit-block-placeholder"></div><p class="m_1431573529807325761gmail-p1">}</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p1">class MyPlugin: PluginInterface {</p><p class="m_1431573529807325761gmail-p1">    override func sayHi() {</p><p class="m_1431573529807325761gmail-p1">        print(&quot;Hi from my plugin&quot;)</p><p class="m_1431573529807325761gmail-p1">    }</p><p class="m_1431573529807325761gmail-p1">}</p><p class="m_1431573529807325761gmail-p1">Package.swift:</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p2">// swift-tools-version:4.0</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">import</span> PackageDescription</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">let</span> package = <span class="m_1431573529807325761gmail-s2">Package</span>(</p><p class="m_1431573529807325761gmail-p4"><span class="m_1431573529807325761gmail-s3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    name: </span>&quot;MyPlugin&quot;<span class="m_1431573529807325761gmail-s3">,</span></p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    products: [ .<span class="m_1431573529807325761gmail-s4">library</span>(name: <span class="m_1431573529807325761gmail-s5">&quot;MyPlugin&quot;</span>, type: .<span class="m_1431573529807325761gmail-s4">dynamic</span>, targets: [<span class="m_1431573529807325761gmail-s5">&quot;MyPlugin&quot;</span>]) ],</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    dependencies: [ .<span class="m_1431573529807325761gmail-s4">package</span>(url: <span class="m_1431573529807325761gmail-s5">&quot;url_to_PluginInterface&quot;</span>, from: <span class="m_1431573529807325761gmail-s5">&quot;0.0.0&quot;</span>), ],</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    targets: [</p><p class="m_1431573529807325761gmail-p4"><span class="m_1431573529807325761gmail-s3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-Apple-tab-span"></span>        .</span><span class="m_1431573529807325761gmail-s4">target</span><span class="m_1431573529807325761gmail-s3">(name: </span>&quot;PluginInterface&quot;<span class="m_1431573529807325761gmail-s3">, dependencies: [</span>&quot;PluginInterface&quot;<span class="m_1431573529807325761gmail-s3">]),</span></p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-Apple-tab-span"></span>        .<span class="m_1431573529807325761gmail-s4">target</span>(name: <span class="m_1431573529807325761gmail-s5">&quot;MyPlugin&quot;</span>, dependencies: [<span class="m_1431573529807325761gmail-s5">&quot;PluginInterface&quot;</span>]),</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    ]</p><div>




















<br class="m_1431573529807325761webkit-block-placeholder"></div><p class="m_1431573529807325761gmail-p3">)</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p1">The PluginConsumer is bit more complicated, but here is relevant part (lib loading and function calling):</p><p class="m_1431573529807325761gmail-p1"><br></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-s1">typealias</span> <span class="m_1431573529807325761gmail-s2">I</span>nitFunction = <span class="m_1431573529807325761gmail-s1">@convention</span>(c) () -&gt; <span class="m_1431573529807325761gmail-s3">AnyObject</span></p><p class="m_1431573529807325761gmail-p2"><br></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-s1">let</span> openRes = <span class="m_1431573529807325761gmail-s4">dlopen</span>(<span class="m_1431573529807325761gmail-s2">p</span>athToLib, <span class="m_1431573529807325761gmail-s3">RTLD_NOW</span>|<span class="m_1431573529807325761gmail-s3">RTLD_LOCAL</span>)</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s5">i</span><span class="m_1431573529807325761gmail-s1">f</span><span class="m_1431573529807325761gmail-s6"> </span>openRes<span class="m_1431573529807325761gmail-s6"> != </span><span class="m_1431573529807325761gmail-s1">nil</span><span class="m_1431573529807325761gmail-s6"> {</span></p><p class="m_1431573529807325761gmail-p4"><span class="m_1431573529807325761gmail-s6"><span class="m_1431573529807325761gmail-Apple-tab-span"></span></span>    defer<span class="m_1431573529807325761gmail-s6"> {</span></p><p class="m_1431573529807325761gmail-p5"><span class="m_1431573529807325761gmail-s6"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-Apple-tab-span"></span></span>        dlclose<span class="m_1431573529807325761gmail-s6">(</span><span class="m_1431573529807325761gmail-s7">openRes</span><span class="m_1431573529807325761gmail-s6">)</span></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    }</p><p class="m_1431573529807325761gmail-p6"><span class="m_1431573529807325761gmail-s1">    let</span><span class="m_1431573529807325761gmail-s6"> symbolName = </span>&quot;mangled_symbol_name&quot;<br></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-s1">    let</span> sym = <span class="m_1431573529807325761gmail-s4">dlsym</span>(<span class="m_1431573529807325761gmail-s7">openRes</span>, symbolName)</p><p class="m_1431573529807325761gmail-p2"><br></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-s1">    if</span> sym != <span class="m_1431573529807325761gmail-s1">nil</span> {</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s6"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-Apple-tab-span"></span></span><span class="m_1431573529807325761gmail-s1">        let</span><span class="m_1431573529807325761gmail-s6"> f: </span>InitFunction<span class="m_1431573529807325761gmail-s6"> = </span><span class="m_1431573529807325761gmail-s4">unsafeBitCast</span><span class="m_1431573529807325761gmail-s6">(sym, to: </span>InitFunction<span class="m_1431573529807325761gmail-s6">.</span><span class="m_1431573529807325761gmail-s1">self</span><span class="m_1431573529807325761gmail-s6">)</span></p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-Apple-tab-span"></span><span class="m_1431573529807325761gmail-s1">        let</span> plugin = f() as? PluginInterface</p><p class="m_1431573529807325761gmail-p1"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    }</p><div>






















<br class="m_1431573529807325761webkit-block-placeholder"></div><p class="m_1431573529807325761gmail-p1">}</p><p class="m_1431573529807325761gmail-p1">Package.swift file:</p><p class="m_1431573529807325761gmail-p1">// swift-tools-version:4.0</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">import</span> PackageDescription</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">let</span> package = <span class="m_1431573529807325761gmail-s2">Package</span>(</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    name: <span class="m_1431573529807325761gmail-s3">&quot;PluginConsumer&quot;</span>,</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    dependencies: [ <span class="m_1431573529807325761gmail-s4">.</span><span class="m_1431573529807325761gmail-s5">package</span><span class="m_1431573529807325761gmail-s4">(url: </span>&quot;path_to_plugin_interface&quot;<span class="m_1431573529807325761gmail-s4">, from: </span>&quot;0.0.0&quot;<span class="m_1431573529807325761gmail-s4">) </span>],</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-Apple-tab-span"></span>    targets: [ <span class="m_1431573529807325761gmail-s4">.</span><span class="m_1431573529807325761gmail-s5">target</span><span class="m_1431573529807325761gmail-s4">(name: </span>&quot;PluginConsumer&quot;<span class="m_1431573529807325761gmail-s4">, dependencies: [</span>&quot;PluginConsumer&quot;<span class="m_1431573529807325761gmail-s4">]) </span>]</p><div>


























<br class="m_1431573529807325761webkit-block-placeholder"></div><p class="m_1431573529807325761gmail-p3">)</p><p class="m_1431573529807325761gmail-p3"><br></p><p class="m_1431573529807325761gmail-p3">This all compiles nicely, MyPlugin project creates dylib file that executable created by PluginConsumer can load, but the problem is with following line:</p><p class="m_1431573529807325761gmail-p3"><span class="m_1431573529807325761gmail-s1">let</span> plugin = f() as? PluginInterface<br></p><p class="m_1431573529807325761gmail-p3">Type of the plugin is MyPlugin, but from the consumer&#39;s view, it doesn&#39;t inherit from PluginInterface so I can&#39;t call sayHi() method. I assume this is because there is no relation between PluginInterface class that compiler uses for MyPlugin project one that it uses for PluginConsumer project. After library is loaded, they are two completely different classes that happen to share same name. Is my assumption correct and how do I go about fixing it?</p><p class="m_1431573529807325761gmail-p3">I had an idea I could make PluginInterface emit dynamic library that would be dynamically linked by both MyPlugin and PluginConsumer, thus making them share same PluginInterface class, but I can&#39;t figure out how to do that (or if it&#39;s right way of doing this).<br></p><p class="m_1431573529807325761gmail-p3"><br></p><p class="m_1431573529807325761gmail-p3">Any help appreciated :)</p><p class="m_1431573529807325761gmail-p3">Lope</p></div></div></div></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></blockquote></div><br></div>