Jekyll2023-05-08T03:26:28+00:00https://zethon.github.io/feed.xmlzethon.github.ioI mostly work in C++ but also dabble in C#, Perl, Haskell and PHP.AddyWindows can and does reuse memory right away2023-05-07T00:00:00+00:002023-05-07T00:00:00+00:00https://zethon.github.io/blog/windows-memory-reuse<p>I have an object that will sometimes delete and immediately reallocate some memory. For example:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Object</span>
<span class="p">{</span>
<span class="nl">public:</span>
<span class="kt">char</span><span class="o">*</span> <span class="n">GetNewBuffer</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">delete</span><span class="p">[]</span> <span class="n">_buffer</span><span class="p">;</span>
<span class="n">_buffer</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span><span class="p">[</span><span class="mi">1024</span><span class="p">];</span>
<span class="k">return</span> <span class="n">_buffer</span><span class="p">;</span>
<span class="p">}</span>
<span class="nl">private:</span>
<span class="kt">char</span><span class="o">*</span> <span class="n">_buffer</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>
<p>A reasonable unit test might be:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">TEST</span><span class="p">(</span><span class="n">Object</span><span class="p">,</span> <span class="n">GetNewBuffer</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">Object</span> <span class="n">object</span><span class="p">;</span>
<span class="kt">char</span><span class="o">*</span> <span class="n">buffer</span> <span class="o">=</span> <span class="n">object</span><span class="p">.</span><span class="n">GetNewBuffer</span><span class="p">();</span>
<span class="n">EXPECT_NE</span><span class="p">(</span><span class="nb">nullptr</span><span class="p">,</span> <span class="n">buffer</span><span class="p">);</span>
<span class="kt">char</span><span class="o">*</span> <span class="n">buffer2</span> <span class="o">=</span> <span class="n">object</span><span class="p">.</span><span class="n">GetNewBuffer</span><span class="p">();</span>
<span class="n">EXPECT_NE</span><span class="p">(</span><span class="nb">nullptr</span><span class="p">,</span> <span class="n">buffer2</span><span class="p">);</span>
<span class="n">EXPECT_NE</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">buffer2</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>
<p>As a matter of fact, this seems to be so reasonable that CoPilot suggested the final test before I had a chance to type it. However, this test can fail, and in my recent experience, it <em>can fail often</em> on Windows!</p>
<h1 id="windows-memory-reuse">Windows Memory Reuse</h1>
<p>The class I was writing is more complicated than the example above, so when my unit tests failed I assumed it was a problem with my code. Of course this was an issue where it was only failing on the build machines and locally everything worked just fine. After adding various debug logs to by unit tests, the only explanation I could come up with is that Windows will sometimes immediately reuse memory that was just freed. Hence, the unit test was bad.</p>AddyI have an object that will sometimes delete and immediately reallocate some memory. For example: ```cpp class Object { public: char* GetNewBuffer() { delete[] _buffer; _buffer = new char[1024]; return _buffer; }Modern Day Owl2022-10-23T00:00:00+00:002022-10-23T00:00:00+00:00https://zethon.github.io/blog/modern-owl<h2 id="qts-macdeployqt--debug-binaries">Qt’s <code class="language-plaintext highlighter-rouge">macdeployqt</code> & debug binaries</h2>
<p>In some form I have been working with Qt since 2014. A couple years ago I took over as lead on a Qt project and noticed something weird. I could never get breakpoints to work on macOS. After spending a day or two on the problem with no luck, I moved on to more important things, falling back on log messages.</p>
<p>Today I was in another project and looking at <code class="language-plaintext highlighter-rouge">macdeployqt</code>’s help and I noticed this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>-no-strip Don't run 'strip' on the binaries
</code></pre></div></div>
<p>Even though the Qt project had long been given to another team, I pulled the latest code from the repo and built it. I confirmed that breakpoints still didn’t work and then added the <code class="language-plaintext highlighter-rouge">-no-strip</code> option. Voila! Breakpoints worked!</p>
<p>It seems like bad design that <strong>by default</strong> a tool would remove functionality from a binary when its job is to package that binary. I also wonder does the counterpart, <code class="language-plaintext highlighter-rouge">windeployqt</code> do something similar?</p>AddyQt’s macdeployqt & debug binariesNaivecoin Addendum2020-12-25T00:00:00+00:002020-12-25T00:00:00+00:00https://zethon.github.io/blog/NaivecoinAddendum<p>Currently I am building a cryptocurrency in C++. The project can be found <a href="https://github.com/zethon/Ash">here</a>. This is not a project where I’ve forked another code base, instead the purpose of this project is to write a brand new protocol from scratch.</p>
<p>Much of what I’m learning comes from <a href="https://lhartikk.github.io/">this Javascript tutorial</a>. However, like any tutorial, the author assumes a foundation of knowledge that may not exist, or leaves out important details. This is not a knock on the author, for the tutorial is well written, is excellent source material, and has helped me immensely. This post is just an attempt to fill in some of the gaps.</p>
<div class="notice--info">This post will be updated periodically to cover all items in the tutorial that I believe deserve futher explanation.</div>
<h2 id="chapter-3---transactions">Chapter 3 - Transactions</h2>
<h3 id="transaction-inputs">Transaction Inputs</h3>
<p>Here the reader is introduced to <code class="language-plaintext highlighter-rouge">TxIn</code> which is given the following structure:</p>
<div class="language-typescript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nx">TxIn</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nx">txOutId</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span>
<span class="k">public</span> <span class="nx">txOutIndex</span><span class="p">:</span> <span class="kr">number</span><span class="p">;</span>
<span class="k">public</span> <span class="nx">signature</span><span class="p">:</span> <span class="kr">string</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">signature</code> field is explained but the <code class="language-plaintext highlighter-rouge">txOutId</code> and <code class="language-plaintext highlighter-rouge">txOutIndex</code> fields never are! Here is what I could find out from both of them.</p>
<p>The <code class="language-plaintext highlighter-rouge">txIn</code> is the part of the transaction that says “the money being sent in this transaction comes from this <code class="language-plaintext highlighter-rouge">txtOut</code>”. That <code class="language-plaintext highlighter-rouge">txOut</code> is located by two components:</p>
<h4 id="txoutindex"><code class="language-plaintext highlighter-rouge">txOutIndex</code></h4>
<p>This is the block index that contains the transaction in which the originating <code class="language-plaintext highlighter-rouge">TxOut</code> exists.</p>
<h4 id="txoutid"><code class="language-plaintext highlighter-rouge">txOutId</code></h4>
<p>This is the transaction id in which the originating <code class="language-plaintext highlighter-rouge">TxOut</code> exists.</p>
<h4 id="example">Example</h4>
<p>Consider the following JSON:</p>
<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">{</span><span class="w">
</span><span class="nl">"index"</span><span class="p">:</span><span class="w"> </span><span class="mi">57</span><span class="p">,</span><span class="w">
</span><span class="err">/*</span><span class="w"> </span><span class="err">lots</span><span class="w"> </span><span class="err">of</span><span class="w"> </span><span class="err">other</span><span class="w"> </span><span class="err">Block</span><span class="w"> </span><span class="err">fields</span><span class="w"> </span><span class="err">*/</span><span class="w">
</span><span class="nl">"transactions"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"id'"</span><span class="p">:</span><span class="w"> </span><span class="s2">"transaction0"</span><span class="p">,</span><span class="w">
</span><span class="nl">"inputs"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"txOutId"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
</span><span class="nl">"txOutIndex"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
</span><span class="nl">"signature"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">],</span><span class="w">
</span><span class="nl">"outputs"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Stefan"</span><span class="p">,</span><span class="w">
</span><span class="nl">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mi">10</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"id'"</span><span class="p">:</span><span class="w"> </span><span class="s2">"transaction1"</span><span class="p">,</span><span class="w">
</span><span class="nl">"inputs"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"txOutId"</span><span class="p">:</span><span class="w"> </span><span class="s2">"transaction0"</span><span class="p">,</span><span class="w">
</span><span class="nl">"txOutIndex"</span><span class="p">:</span><span class="w"> </span><span class="mi">57</span><span class="p">,</span><span class="w">
</span><span class="nl">"signature"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">],</span><span class="w">
</span><span class="nl">"outputs"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Henry"</span><span class="p">,</span><span class="w">
</span><span class="nl">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mi">4</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Addy"</span><span class="p">,</span><span class="w">
</span><span class="nl">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mi">3</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Stefan"</span><span class="p">,</span><span class="w">
</span><span class="nl">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>
<p>Here we have Block #57 in our chain, which contains two transactions <code class="language-plaintext highlighter-rouge">transaction0</code> and <code class="language-plaintext highlighter-rouge">transaction1</code>. In practice the fields <code class="language-plaintext highlighter-rouge">id</code>, <code class="language-plaintext highlighter-rouge">address</code> and <code class="language-plaintext highlighter-rouge">signature</code> would have hashes, for purposes of this explanation they have been simplified.</p>
<p>You may notice that <code class="language-plaintext highlighter-rouge">transaction0</code> has no <code class="language-plaintext highlighter-rouge">txIn</code> information, which would only happen with a coinbase transaction (i.e. the reward that happens when a miner has solved a block). In this example the receiving address is <code class="language-plaintext highlighter-rouge">Stefan</code> who has balance of <code class="language-plaintext highlighter-rouge">10</code>.</p>
<p>In <code class="language-plaintext highlighter-rouge">transaction1</code> we see that Stefan has sent <code class="language-plaintext highlighter-rouge">4</code> crypto to Henry and <code class="language-plaintext highlighter-rouge">3</code> to Addy, with the remainder being send back to Stefan.</p>
<p>Also, notice how the <code class="language-plaintext highlighter-rouge">txOutIndex</code> value is <code class="language-plaintext highlighter-rouge">57</code> meaning that the source of this <code class="language-plaintext highlighter-rouge">txIn</code> is located in Block #57. Likewise, the value of <code class="language-plaintext highlighter-rouge">txOut</code> tells you exactly which transaction in the block from which this <code class="language-plaintext highlighter-rouge">txIn</code> comes.</p>AddyCurrently I am building a cryptocurrency in C++. The project can be found here. This is not a project where I’ve forked another code base, instead the purpose of this project is to write a brand new protocol from scratch.Simple CMake to Compile Resources2020-12-08T00:00:00+00:002020-12-08T00:00:00+00:00https://zethon.github.io/blog/simple-cmake-to-compile-resources<p>Recently I have been working on a project that has a <a href="https://gitlab.com/eidheim/Simple-Web-Server">Simple Web Server</a>, so I needed a way to serve HTML files.</p>
<p>I went to Google and searched <code class="language-plaintext highlighter-rouge">c++ cmake compile resources</code>. The first project I found was <a href="https://vector-of-bool.github.io/2017/01/21/cmrc.html">this</a>. However this project was much more involved than I needed, going so far as to implement a mini-filesystem, much like Qt’s resource system.</p>
<p>What I wanted was more simple. I wanted to take a resource, compile it into a header as a <code class="language-plaintext highlighter-rouge">char</code> array. This was my solution:</p>
<div class="language-cmake highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">function</span><span class="p">(</span>generate_header INPUTFILE OUTPUTFILE VARNAME<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>DONE FALSE<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>CURRENTPOS 0<span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>READ <span class="si">${</span><span class="nv">INPUTFILE</span><span class="si">}</span> FILEDATA HEX<span class="p">)</span>
<span class="nb">string</span><span class="p">(</span>LENGTH <span class="s2">"</span><span class="si">${</span><span class="nv">FILEDATA</span><span class="si">}</span><span class="s2">"</span> DATALEN<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>OUTPUT_DATA <span class="s2">""</span><span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>WRITE <span class="si">${</span><span class="nv">OUTPUTFILE</span><span class="si">}</span> <span class="s2">"#pragma once</span><span class="se">\n\n</span><span class="s2">static const char </span><span class="si">${</span><span class="nv">VARNAME</span><span class="si">}</span><span class="s2">[] = { "</span><span class="p">)</span>
<span class="nb">foreach</span><span class="p">(</span>BYTE_OFFSET RANGE 0 <span class="s2">"</span><span class="si">${</span><span class="nv">DATALEN</span><span class="si">}</span><span class="s2">"</span> 2<span class="p">)</span>
<span class="nb">string</span><span class="p">(</span>SUBSTRING <span class="s2">"</span><span class="si">${</span><span class="nv">FILEDATA</span><span class="si">}</span><span class="s2">"</span> <span class="s2">"</span><span class="si">${</span><span class="nv">BYTE_OFFSET</span><span class="si">}</span><span class="s2">"</span> 2 HEX_STRING<span class="p">)</span>
<span class="nb">string</span><span class="p">(</span>LENGTH <span class="s2">"</span><span class="si">${</span><span class="nv">HEX_STRING</span><span class="si">}</span><span class="s2">"</span> TEMPLEN<span class="p">)</span>
<span class="nb">if</span> <span class="p">(</span><span class="s2">"</span><span class="si">${</span><span class="nv">TEMPLEN</span><span class="si">}</span><span class="s2">"</span> GREATER 0<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>OUTPUT_DATA <span class="s2">"</span><span class="si">${</span><span class="nv">OUTPUT_DATA</span><span class="si">}</span><span class="s2">0x</span><span class="si">${</span><span class="nv">HEX_STRING</span><span class="si">}</span><span class="s2">, "</span><span class="p">)</span>
<span class="nb">endif</span><span class="p">()</span>
<span class="nb">endforeach</span><span class="p">()</span>
<span class="nb">file</span><span class="p">(</span>APPEND <span class="si">${</span><span class="nv">OUTPUTFILE</span><span class="si">}</span> <span class="s2">"</span><span class="si">${</span><span class="nv">OUTPUT_DATA</span><span class="si">}</span><span class="s2">0"</span><span class="p">)</span>
<span class="nb">file</span><span class="p">(</span>APPEND <span class="si">${</span><span class="nv">OUTPUTFILE</span><span class="si">}</span> <span class="s2">" };</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">endfunction</span><span class="p">()</span>
<span class="nb">function</span><span class="p">(</span>z_compile_resources RESOURCE_LIST<span class="p">)</span>
<span class="nb">foreach</span><span class="p">(</span>RESOURCE_NAME <span class="si">${</span><span class="nv">ARGN</span><span class="si">}</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>RESOURCE_FILENAME <span class="s2">"</span><span class="si">${</span><span class="nv">CMAKE_CURRENT_SOURCE_DIR</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">RESOURCE_NAME</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">get_filename_component</span><span class="p">(</span>FILENAME <span class="si">${</span><span class="nv">RESOURCE_NAME</span><span class="si">}</span> NAME_WE<span class="p">)</span>
<span class="nb">get_filename_component</span><span class="p">(</span>EXT <span class="si">${</span><span class="nv">RESOURCE_NAME</span><span class="si">}</span> EXT<span class="p">)</span>
<span class="nb">string</span><span class="p">(</span>SUBSTRING <span class="si">${</span><span class="nv">EXT</span><span class="si">}</span> 1 -1 EXT<span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>OUTPUT_FILE <span class="s2">"</span><span class="si">${</span><span class="nv">CMAKE_CURRENT_BINARY_DIR</span><span class="si">}</span><span class="s2">/</span><span class="si">${</span><span class="nv">FILENAME</span><span class="si">}</span><span class="s2">_</span><span class="si">${</span><span class="nv">EXT</span><span class="si">}</span><span class="s2">.h"</span><span class="p">)</span>
<span class="nb">set</span><span class="p">(</span>VARIABLE_NAME <span class="s2">"</span><span class="si">${</span><span class="nv">FILENAME</span><span class="si">}</span><span class="s2">_</span><span class="si">${</span><span class="nv">EXT</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nf">generate_header</span><span class="p">(</span><span class="si">${</span><span class="nv">RESOURCE_FILENAME</span><span class="si">}</span> <span class="si">${</span><span class="nv">OUTPUT_FILE</span><span class="si">}</span> <span class="si">${</span><span class="nv">VARIABLE_NAME</span><span class="si">}</span><span class="p">)</span>
<span class="nb">endforeach</span><span class="p">()</span>
<span class="nb">endfunction</span><span class="p">()</span>
</code></pre></div></div>
<p>Now I can compile resources in my CMake file like so:</p>
<div class="language-cmake highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nf">z_compile_resources</span><span class="p">(</span>RESOURCE_FILES
html/index.html
<span class="p">)</span>
</code></pre></div></div>
<p>And now CMake will generate a header file that looks like this:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#pragma once
</span>
<span class="k">static</span> <span class="k">const</span> <span class="kt">char</span> <span class="n">index_html</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="mh">0x3c</span><span class="p">,</span> <span class="mh">0x68</span><span class="p">,</span> <span class="p">...</span> <span class="p">,</span> <span class="mi">0</span> <span class="p">};</span>
</code></pre></div></div>
<p>This is awesome! And now in my source file I can use it like so:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include "index_html.h"
</span>
<span class="cm">/* lots of code here */</span>
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">index_html</span> <span class="o"><<</span> <span class="sc">'\n'</span><span class="p">;</span>
</code></pre></div></div>AddyRecently I have been working on a project that has a Simple Web Server, so I needed a way to serve HTML files.Using a Conan Recipe (the basics)2020-11-21T00:00:00+00:002020-11-21T00:00:00+00:00https://zethon.github.io/blog/using-conan-recipes<p>I’m using <a href="https://gitlab.com/eidheim/Simple-Web-Server">Simple Web Server</a> for a project and I wanted a way to pull the library down via Conan. I found <a href="https://github.com/inexorgame-obsolete/conan-simple-web-server">a recipe</a> for the library a couple months ago but was still unsure of how to use it. If I couldn’t find the library being hosted in an existing repo, how exactly do I use the recipe such that I can host the resource in my own repo? I had done this multiple times before but it was always a bastardization of the process and it was always something I had to figure out through trial and error.</p>
<p>Well here are the steps I used in order to use the existing recipe in my own <a href="https://bintray.com">bintray</a> repo. I put this here as much as a note to myself as a guide to anyone else looking.</p>
<p>Before I get started I want to mention that these steps assume you already have a repo on bintray and that you’ve already authenticated your repo on your machine so that you can push to it (like I talked about <a href="https://zethon.github.io/2019-11-06-hacking-together-a-conan-package/">here</a>).</p>
<p>The first thing I did was fork the recipe so I could update it to the latest version of the library. My fork can be found <a href="https://github.com/zethon/conan-simple-web-server">here</a>. After cloning and updating the library’s version number I was ready to start.</p>
<p>One I pulled the recipe and changed into its folder, I used the recipe to pull the library’s source: <code class="language-plaintext highlighter-rouge">conan source .</code></p>
<p>Then I “installed” the package: <code class="language-plaintext highlighter-rouge">conan install .</code></p>
<p>Next I was ready to package everything together: <code class="language-plaintext highlighter-rouge">conan package .</code></p>
<p>Now I was ready to install the package locally: <code class="language-plaintext highlighter-rouge">conan export-pkg . Simple-Web-Server/v3.1.1@owl/stable</code></p>
<p>Finally, I was ready to upload the package to my repo: <code class="language-plaintext highlighter-rouge">conan upload Simple-Web-Server/v3.1.1@owl/stable -r owl --all</code></p>AddyI’m using Simple Web Server for a project and I wanted a way to pull the library down via Conan. I found a recipe for the library a couple months ago but was still unsure of how to use it. If I couldn’t find the library being hosted in an existing repo, how exactly do I use the recipe such that I can host the resource in my own repo? I had done this multiple times before but it was always a bastardization of the process and it was always something I had to figure out through trial and error.Linking errors with boost on macOS2020-11-20T00:00:00+00:002020-11-20T00:00:00+00:00https://zethon.github.io/blog/linking-errors-with-boost-on-macos<p>I recently revived <a href="https://github.com/zethon/arcc">an old project</a> and tried to get it to build on my Mac. I was immediately met with this error:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Undefined symbols for architecture x86_64:
"boost::system::detail::system_category_instance", referenced from:
boost::system::system_category() in ConsoleApp.cpp.o
boost::system::system_category() in OAuth2Login.cpp.o
boost::system::system_category() in RedditSession.cpp.o
boost::system::system_category() in utils.cpp.o
"boost::system::detail::generic_category_instance", referenced from:
boost::system::generic_category() in arcc.cpp.o
boost::system::generic_category() in CommandHistory.cpp.o
boost::system::generic_category() in ConsoleApp.cpp.o
boost::system::generic_category() in OAuth2Login.cpp.o
boost::system::generic_category() in RedditSession.cpp.o
boost::system::generic_category() in Settings.cpp.o
boost::system::generic_category() in utils.cpp.o
...
ld: symbol(s) not found for architecture x86_64
</code></pre></div></div>
<p>This was perplexing because everything looked fine in my CMake files and I wasn’t getting this error on Windows or Ubuntu. The first thing I did was print verbose output to make sure I was in fact linking what I thought I was linking:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd /Users/zethon/src/arcc/build/arcc && /usr/local/Cellar/cmake/3.18.3/bin/cmake -E cmake_link_script CMakeFiles/arcc.dir/link.txt --verbose=1
... (ommitted) ...
/lib -lboost_program_options -lboost_timer -lboost_thread -lboost_chrono -lboost_filesystem -lboost_system -lboost_stacktrace_addr2line -lboost_stacktrace_basic -lboost_stacktrace_noop -lboost_unit_test_framework -lcurl -lfmtd -lform_g -lmenu_g -lncurses++_g -lncurses_g -lpanel_g -lz /usr/local/lib/libboost_system.a /usr/local/lib/libboost_thread-mt.a /usr/local/opt/openssl/lib/libssl.dylib /usr/local/opt/openssl/lib/libcrypto.dylib
</code></pre></div></div>
<p>In case it’s not obvious, the functions in my error are part of the Boost System library (which I found out through a Google search). The output above proved that I definitely <em>am</em> linking to <code class="language-plaintext highlighter-rouge">boost_system</code>. So.. back to Google!</p>
<p>Then I found <a href="https://stackoverflow.com/questions/13467072/c-boost-undefined-reference-to-boostsystemgeneric-category">this link</a> on StackOverflow, which had the answer:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Adding add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY) for cmake file.
</code></pre></div></div>
<p>I added this to my CMake and it worked!</p>
<p>Full admission, this is one of those answers that work, and I don’t understand why or what’s going on. All that matters is that my build is working, so I’m ready to move on.</p>AddyI recently revived an old project and tried to get it to build on my Mac. I was immediately met with this error:Xenforo 2.x Addon: `xf-addon:create` not working2020-11-05T00:00:00+00:002020-11-05T00:00:00+00:00https://zethon.github.io/blog/xenforo-2-x-addon-notes<h2 id="creating-the-addon">Creating the Addon</h2>
<p>The first step in creating a new Xenforo Addon is creating the addon within the Xenforo dev environment with the following command: <code class="language-plaintext highlighter-rouge">php cmd.php xf-addon:create</code>.</p>
<p>Last night I set up a development environment for Xenforo using <a href="https://www.apachefriends.org/index.html">XAMPP</a>. I was able to install Xenforo and create an addon without a problem on Windows.</p>
<p>However, when I tried to do this on Mac I got the following error:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>An exception occurred: [XF\Db\Exception] No such file or directory in src/XF/Db/Mysqli/Adapter.php on line 173
#0 src/XF/Db/Mysqli/Adapter.php(28): XF\Db\Mysqli\Adapter->makeConnection(Array)
#1 src/XF/Db/AbstractAdapter.php(60): XF\Db\Mysqli\Adapter->getConnection()
#2 src/XF/Db/Mysqli/Adapter.php(113): XF\Db\AbstractAdapter->connect()
#3 src/XF/Db/AbstractAdapter.php(516): XF\Db\Mysqli\Adapter->escapeString('addOnsComposer')
#4 src/XF/Db/AbstractAdapter.php(494): XF\Db\AbstractAdapter->quote('addOnsComposer')
#5 src/XF/DataRegistry.php(138): XF\Db\AbstractAdapter->quote(Array)
#6 src/XF/DataRegistry.php(83): XF\DataRegistry->readFromDb(Array, Array)
#7 src/XF/DataRegistry.php(226): XF\DataRegistry->get(Array)
#8 src/XF/App.php(1650): XF\DataRegistry->offsetGet('addOnsComposer')
#9 src/XF/Container.php(28): XF\App->XF\{closure}(Object(XF\Container))
#10 src/XF/App.php(2164): XF\Container->offsetGet('addon.composer')
#11 src/XF/App.php(1749): XF\App->setupAddOnComposerAutoload()
#12 src/XF/Cli/App.php(25): XF\App->setup()
#13 src/XF.php(364): XF\Cli\App->setup(Array)
#14 src/XF/Cli/Runner.php(50): XF::setupApp('XF\\Cli\\App')
#15 cmd.php(15): XF\Cli\Runner->run()
#16 {main}
</code></pre></div></div>
<p>Turns out that the problem was in my <code class="language-plaintext highlighter-rouge">config.php</code> file where I had <code class="language-plaintext highlighter-rouge">host</code> configured as <code class="language-plaintext highlighter-rouge">localhost</code>:</p>
<div class="language-php highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$config</span><span class="p">[</span><span class="s1">'db'</span><span class="p">][</span><span class="s1">'host'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'localhost'</span><span class="p">;</span>
</code></pre></div></div>
<p>On a hunch, I figured I would try the following:</p>
<div class="language-php highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$config</span><span class="p">[</span><span class="s1">'db'</span><span class="p">][</span><span class="s1">'host'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'127.0.0.1'</span><span class="p">;</span>
</code></pre></div></div>
<p>And it worked! Now I’m able to create new addons on my Mac without a problem.</p>AddyCreating the AddonWriting const and non-const Getters2020-07-19T00:00:00+00:002020-07-19T00:00:00+00:00https://zethon.github.io/blog/writing-const-and-non-const-getters<p>One use of <code class="language-plaintext highlighter-rouge">const_cast</code> is to avoid duplicating code when implementing getters in a class. If you want users of your class to have write-access to a member object, then in order to provide write-access and read-only access to the member you might do something like:</p>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Texture</span>
<span class="p">{</span>
<span class="c1">// lots of stuff</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">Player</span>
<span class="p">{</span>
<span class="n">Texture</span> <span class="n">_texture</span><span class="p">;</span>
<span class="nl">public:</span>
<span class="k">const</span> <span class="n">Texture</span><span class="o">&</span> <span class="n">texture</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_texture</span><span class="p">;</span> <span class="p">}</span>
<span class="n">Texture</span><span class="o">&</span> <span class="n">texture</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_texture</span><span class="p">;</span> <span class="p">}</span>
<span class="p">};</span>
</code></pre></div></div>
<p>But we would like to avoid the code duplication.</p>
<p>We have two options. We can implement the const version and do a cast in the non-const version like so:</p>
<h3 id="option-1">Option 1</h3>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">const</span> <span class="n">Texture</span><span class="o">&</span> <span class="n">texture</span><span class="p">()</span> <span class="k">const</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">_texture</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">Texture</span><span class="o">&</span> <span class="n">texture</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="k">const_cast</span><span class="o"><</span><span class="n">Texture</span><span class="o">&></span><span class="p">(</span><span class="k">static_cast</span><span class="o"><</span><span class="k">const</span> <span class="n">Player</span><span class="o">&></span><span class="p">(</span><span class="o">*</span><span class="k">this</span><span class="p">).</span><span class="n">texture</span><span class="p">());</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Or we can do the opposite, we can implement the non-const version and cast in the const version. Something like:</p>
<h3 id="option-2">Option 2</h3>
<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">const</span> <span class="n">Texture</span><span class="o">&</span> <span class="n">texture</span><span class="p">()</span> <span class="k">const</span>
<span class="p">{</span>
<span class="k">return</span> <span class="p">(</span><span class="k">const_cast</span><span class="o"><</span><span class="n">Person</span><span class="o">&></span><span class="p">(</span><span class="o">*</span><span class="k">this</span><span class="p">)).</span><span class="n">texture</span><span class="p">();</span>
<span class="p">}</span>
<span class="n">Texture</span><span class="o">&</span> <span class="n">texture</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">_texture</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>When I first saw this, my gut reaction was to go with <strong>Option 2</strong> mostly because casting away the const felt wrong. However, what I didn’t immediately realize is that <strong>Option 2</strong> is calling a non-const function within a const context, which is even worse!. For example, imagine someone came along later and changed the non-const version of <code class="language-plaintext highlighter-rouge">texture()</code> to modify some internal state of the object.</p>
<p>Because of this <strong>Option 1</strong> is preferred.</p>
<p><strong>Note:</strong> This topic is discussed in Item 3 in <em>Effective C++, Third Edition</em> by Scott Meyers</p>AddyOne use of const_cast is to avoid duplicating code when implementing getters in a class. If you want users of your class to have write-access to a member object, then in order to provide write-access and read-only access to the member you might do something like:Qt Automatic Slot Names2020-01-24T00:00:00+00:002020-01-24T00:00:00+00:00https://zethon.github.io/blog/notes<p>I’ve been using Qt for several years now in a couple different projects. Only today did I learn about <a href="https://doc.qt.io/qt-5/qmetaobject.html#connectSlotsByName">QMetaObject::connectSlotsByName</a>.</p>
<p>As the documentation states, this function will <strong>recursively</strong> search the given object for signals matching the format of:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>void on_<object name>_<signal name>(<signal parameters>);
</code></pre></div></div>
<p>If you’re using Qt Designer and MOC’ing, this fuction gets called automatically in your <code class="language-plaintext highlighter-rouge">ui_<mainwindow>.h</code> file inside of <code class="language-plaintext highlighter-rouge">setupUi()</code>. This means that you don’t have to manually connect the objects in your <code class="language-plaintext highlighter-rouge">.ui</code> files. I didn’t know this! You can simply do something like:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class MainWindow : public QMainWindow
{
...
private Q_SLOTS:
void on_button1_triggered();
}
</code></pre></div></div>
<p>And that’s it! You don’t have to define the signals/slots in Designer, you don’t have to manually connect them (as I had been doing). Nothing! <em>It just works</em>!</p>
<p>I had never seen this mentioned in any tutorial or even code examples. I only stumbled upon it when looking at another project for something completely unrelated. But this is a great time saver!</p>AddyI’ve been using Qt for several years now in a couple different projects. Only today did I learn about QMetaObject::connectSlotsByName.Conan: Adding a new build_type to an existing package2019-11-24T00:00:00+00:002019-11-24T00:00:00+00:00https://zethon.github.io/blog/more-conan-hacking<p>I am still working my way through learning Conan. I only make changes to it when I have to, which luckily isn’t often but whenever I do it’s aways a learning experience.</p>
<p>In a <a href="https://zethon.github.io/2019-11-06-hacking-together-a-conan-package/">previous post</a> I talked about how I had to create a PDCurses package for one of my projects. When I created the package I didn’t think about different configurations since I was only building “Release” and “RelWithDebInfo” on Windows in my project. However, I recently added Codecov support to my project so I needed to build “Debug” builds on Windows. Suddenly I realized that I needed to add a “Debug” configuration to my PDCurses packages.</p>
<p>After some searching I found <a href="https://github.com/alex-precosky/conan-pdcurses">this recipe</a> for PDCurses 3.6. I forked the repo, pulled it locally and started hacking at it. Here are the steps I ultimately followed to add “Debug” libraries to my existing requirements:</p>
<ul>
<li><code class="language-plaintext highlighter-rouge">conan search pdcurses/3.9@zethon/stable -r arcc</code> - First I confirmed that I did indeed only have a <code class="language-plaintext highlighter-rouge">Release</code> package</li>
</ul>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ conan search pdcurses/3.9@zethon/stable -r arcc
Existing packages for recipe pdcurses/3.9@zethon/stable:
Existing recipe in remote 'arcc':
Package_ID: 6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7
[settings]
arch: x86_64
build_type: Release
compiler: Visual Studio
compiler.runtime: MD
compiler.version: 15
os: Windows
Outdated from recipe: False
</code></pre></div></div>
<ul>
<li>Clone the recipe, cd into the recipe’s folder, mkdir a <code class="language-plaintext highlighter-rouge">bin</code> folder and cd into the <code class="language-plaintext highlighter-rouge">bin</code></li>
<li><code class="language-plaintext highlighter-rouge">conan install .. -s build_type=Debug</code> - Generate the build info</li>
<li><code class="language-plaintext highlighter-rouge">conan source ..</code> - Clone PDCurses’s code locally</li>
<li><code class="language-plaintext highlighter-rouge">conan build .. -sf .</code> - Build everything! The <code class="language-plaintext highlighter-rouge">-sf .</code> is saying “the source files are in the current folder”</li>
<li><code class="language-plaintext highlighter-rouge">conan package .. -sf .</code> - Now we prepare everything a publishable package</li>
<li><code class="language-plaintext highlighter-rouge">conan export-pkg .. pdcurses/3.9@zethon/stale -pf .</code> - I believe this just installs the package locally. It may not strictly be necesary if it you just upload the package and then download it through the consuming project.</li>
<li><code class="language-plaintext highlighter-rouge">conan upload pdcurses/3.9@zethon/stable -r arcc --all</code> - Push everything up to our repo.</li>
<li><code class="language-plaintext highlighter-rouge">conan search pdcurses/3.9@zethon/stable -r arcc</code> - And nowe we verify we have a Debug package.</li>
</ul>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ conan search pdcurses/3.9@zethon/stable -r arcc
Existing packages for recipe pdcurses/3.9@zethon/stable:
Existing recipe in remote 'arcc':
Package_ID: 6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7
[settings]
arch: x86_64
build_type: Release
compiler: Visual Studio
compiler.runtime: MD
compiler.version: 15
os: Windows
Outdated from recipe: True
Package_ID: 8cf01e2f50fcd6b63525e70584df0326550364e1
[options]
shared: False
[settings]
arch: x86_64
build_type: Debug
compiler: Visual Studio
compiler.runtime: MDd
compiler.version: 15
os: Windows
Outdated from recipe: False
</code></pre></div></div>
<p>After this I was able to do Debug builds in my project.</p>AddyI am still working my way through learning Conan. I only make changes to it when I have to, which luckily isn’t often but whenever I do it’s aways a learning experience.