<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Hello world external written in Zig]]></title><description><![CDATA[<p>I recently heard about the Zig programming language and wanted to see if I could make Pd externals with it, so I put together a &quot;hello world&quot; external, which showcases things like printing to console, working with symbols, and sending floats through an outlet.</p>
<p>Getting it to work required using a local copy of <code>m_pd.h</code> and making a small change to the <code>t_text</code> struct, since Zig's C-translator apparently doesn't like bit fields.:</p>
<pre><code class="lang-diff">-    unsigned int te_type:2;     /* from defs below */
+    unsigned char te_type;       /* from defs below */
</code></pre>
<p>Currently, the build file is catered to a Linux installation, but probably all that would need to be changed for a different platform is the system include path and the library extension.</p>
<p>To do a build, just run:</p>
<pre><code>zig build -Doptimize=ReleaseFast
</code></pre>
<p>EDIT: Changed the struct to an <code>extern struct</code> to guarantee the ordering of its fields.</p>
<p>EDIT2: Functions are now inside the struct. This seems to be more idiomatic to Zig, plus it looks nicer.</p>
<p>EDIT3: Link with libc during the build process.<br />
This keeps the external's size small, even when adding math functions like <code>log</code> or <code>exp</code> to it. Otherwise, there's an increase of about 115KB because those functions get baked right into the external.</p>
<p>EDIT4: Replace <code>m_pd.h</code> with custom definitions that allow many functions to be called as methods. Some examples:</p>
<p>adding a method to a class:</p>
<pre><code class="lang-c">pd.class_addbang(myclass, @ptrCast(&amp;bang)); // old way
myclass.addBang(@ptrCast(&amp;bang)); // new way
</code></pre>
<p>creating a new instance of an object</p>
<pre><code class="lang-c">// old way
const self: *Self = @ptrCast(pd.pd_new(myclass));
_ = pd.outlet_new(&amp;self.obj, &amp;pd.s_float);
self.sym = pd.gensym(&quot;world&quot;);

// new way
const self: *Self = @ptrCast(myclass.new());
_ = self.obj.outlet(pd.s.float);
self.sym = pd.symbol(&quot;world&quot;);
</code></pre>
<p>sending a float through the main outlet:</p>
<pre><code class="lang-c">pd.outlet_float(self.obj.te_outlet, f * 2); // old way
self.obj.out.float(f * 2); // new way
</code></pre>
<p><a href="/uploads/files/1711339016659-hello-zig.zip">hello-zig.zip</a></p>
<p>repo: <a href="https://github.com/myQwil/pd-zig-external" rel="nofollow">https://github.com/myQwil/pd-zig-external</a></p>
<p>More examples can be found on my library's Zig rewrite branch:<br />
<a href="https://github.com/myQwil/pd-quilt/tree/zig-rewrite" rel="nofollow">https://github.com/myQwil/pd-quilt/tree/zig-rewrite</a></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14639/hello-world-external-written-in-zig</link><generator>RSS for Node</generator><lastBuildDate>Mon, 11 May 2026 21:55:22 GMT</lastBuildDate><atom:link href="http://forum.pdpatchrepo.info/topic/14639.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 24 Feb 2024 23:27:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Hello world external written in Zig on Sat, 01 Jun 2024 01:41:45 GMT]]></title><description><![CDATA[<p>I recently heard about the Zig programming language and wanted to see if I could make Pd externals with it, so I put together a &quot;hello world&quot; external, which showcases things like printing to console, working with symbols, and sending floats through an outlet.</p>
<p>Getting it to work required using a local copy of <code>m_pd.h</code> and making a small change to the <code>t_text</code> struct, since Zig's C-translator apparently doesn't like bit fields.:</p>
<pre><code class="lang-diff">-    unsigned int te_type:2;     /* from defs below */
+    unsigned char te_type;       /* from defs below */
</code></pre>
<p>Currently, the build file is catered to a Linux installation, but probably all that would need to be changed for a different platform is the system include path and the library extension.</p>
<p>To do a build, just run:</p>
<pre><code>zig build -Doptimize=ReleaseFast
</code></pre>
<p>EDIT: Changed the struct to an <code>extern struct</code> to guarantee the ordering of its fields.</p>
<p>EDIT2: Functions are now inside the struct. This seems to be more idiomatic to Zig, plus it looks nicer.</p>
<p>EDIT3: Link with libc during the build process.<br />
This keeps the external's size small, even when adding math functions like <code>log</code> or <code>exp</code> to it. Otherwise, there's an increase of about 115KB because those functions get baked right into the external.</p>
<p>EDIT4: Replace <code>m_pd.h</code> with custom definitions that allow many functions to be called as methods. Some examples:</p>
<p>adding a method to a class:</p>
<pre><code class="lang-c">pd.class_addbang(myclass, @ptrCast(&amp;bang)); // old way
myclass.addBang(@ptrCast(&amp;bang)); // new way
</code></pre>
<p>creating a new instance of an object</p>
<pre><code class="lang-c">// old way
const self: *Self = @ptrCast(pd.pd_new(myclass));
_ = pd.outlet_new(&amp;self.obj, &amp;pd.s_float);
self.sym = pd.gensym(&quot;world&quot;);

// new way
const self: *Self = @ptrCast(myclass.new());
_ = self.obj.outlet(pd.s.float);
self.sym = pd.symbol(&quot;world&quot;);
</code></pre>
<p>sending a float through the main outlet:</p>
<pre><code class="lang-c">pd.outlet_float(self.obj.te_outlet, f * 2); // old way
self.obj.out.float(f * 2); // new way
</code></pre>
<p><a href="/uploads/files/1711339016659-hello-zig.zip">hello-zig.zip</a></p>
<p>repo: <a href="https://github.com/myQwil/pd-zig-external" rel="nofollow">https://github.com/myQwil/pd-zig-external</a></p>
<p>More examples can be found on my library's Zig rewrite branch:<br />
<a href="https://github.com/myQwil/pd-quilt/tree/zig-rewrite" rel="nofollow">https://github.com/myQwil/pd-quilt/tree/zig-rewrite</a></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14639/hello-world-external-written-in-zig</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14639/hello-world-external-written-in-zig</guid><dc:creator><![CDATA[myQwil]]></dc:creator><pubDate>Sat, 01 Jun 2024 01:41:45 GMT</pubDate></item></channel></rss>