<div dir="ltr">For one, there's no reason to subclass <span style="color:rgb(80,0,80);font-family:monospace">ExternalLibrary</span>.<div><br></div><div>But otherwise, it works for me:</div><div><br></div><img src="cid:ii_m1pd544i2" alt="image.png" width="562" height="130"><br><div><br></div><div><img src="cid:ii_m1pd2ds00" alt="image.png" width="562" height="121" style="margin-right: 0px;"><br></div><div><br></div><div>That said, if you actually want to use C functions in your application, the better way is to build a VM plugin. It's a little bit more overhead to learn but fits the Smalltalk philosophy much better, where the VM provides the environment for the image to live in, rather than the image talking to the outside world directly. The job of the VM is to provide a safe world so Smalltalk developers don't have to worry about the harsh reality. FFI breaks that illusion.</div><div><br></div><div>Vanessa</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Sep 30, 2024 at 8:10 AM Mark Volkmann via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st">cuis-dev@lists.cuis.st</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Vanessa's getenv example works for me. Now I want to learn how to invoke my own C functions.<div>I created the file "mylib.c" containing this:</div><div><br></div><div><font face="monospace">char* greet() {<br>  return "Hello, World!";<br>}</font><br></div><div><br></div><div>Then I created a shared library for macOS with this:</div><div><br></div><div><font face="monospace">clang -dynamiclib -o libmylib.dylib mylib.c<br></font></div><div><br></div><div>Then I created this Smalltalk class:</div><div><br></div><div><font face="monospace">ExternalLibrary subclass: #FFIDemo<br>    instanceVariableNames: ''<br>    classVariableNames: ''<br>    poolDictionaries: ''<br>    category: 'Volkmann'</font><br></div><div><br></div><div>Then I added this class method:</div><div><br></div><div><font face="monospace">greet<br>    <cdecl: char* 'greet' () module: '/Users/volkmannm/Documents/dev/lang/smalltalk/Cuis-Smalltalk-Volkmann/libmylib.dylib'><br>    ^self externalCallFailed</font><br></div><div><br></div><div>Side note: I need to learn where this looks by default for shared libraries so I don't have to specify an absolute path.</div><div><br></div><div>Then I entered this in a Workspace and did a "Print it".</div><div><br></div><div><font face="monospace">FFIDemo greet</font></div><div><br></div><div>That opens a Debug window with the message "Error: Unable to find function address".</div><div><br></div><div>Can anyone spot where I'm going wrong?</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Sep 29, 2024 at 10:10 PM Andres Valloud via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st" target="_blank">cuis-dev@lists.cuis.st</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Except of course getenv() is ruled by POSIX so doing that is at the very <br>
least not portable, if not outright wrong.<br>
<br>
On 9/29/24 6:29 PM, Vanessa Freudenberg via Cuis-dev wrote:<br>
> And, defining a method myself works fine:<br>
> <br>
> getenv: var<br>
>          <cdecl: char* 'getenv' (char*) module: 'libSystem.dylib'><br>
>          ^self externalCallFailed<br>
> <br>
> And then<br>
> <br>
>     MyClass getenv: 'HOME'<br>
> <br>
> <br>
> returns<br>
> <br>
>       '/Users/vanessa' .<br>
> <br>
> <br>
> There really isn't much more to it.<br>
> <br>
> Vanessa<br>
> <br>
> On Sun, Sep 29, 2024 at 6:26 PM Vanessa Freudenberg <br>
> <<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a> <mailto:<a href="mailto:vanessa@codefrau.net" target="_blank">vanessa@codefrau.net</a>>> wrote:<br>
> <br>
>     I just loaded the "Tests-FFI" package and it works.<br>
> <br>
>     And you can e.g. run<br>
> <br>
>           FFITestLibrary new ffiTestFloats: 3 with: 4<br>
> <br>
>     which returns 7.0 because the plugin happens to implement<br>
>     "ffiTestFloats()" as returning the sum of its two arguments.<br>
> <br>
>     Vanessa<br>
> <br>
> <br>
<br>
-- <br>
Cuis-dev mailing list<br>
<a href="mailto:Cuis-dev@lists.cuis.st" target="_blank">Cuis-dev@lists.cuis.st</a><br>
<a href="https://lists.cuis.st/mailman/listinfo/cuis-dev" rel="noreferrer" target="_blank">https://lists.cuis.st/mailman/listinfo/cuis-dev</a><br>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div>
-- <br>
Cuis-dev mailing list<br>
<a href="mailto:Cuis-dev@lists.cuis.st" target="_blank">Cuis-dev@lists.cuis.st</a><br>
<a href="https://lists.cuis.st/mailman/listinfo/cuis-dev" rel="noreferrer" target="_blank">https://lists.cuis.st/mailman/listinfo/cuis-dev</a><br>
</blockquote></div>