<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>gcc on Kshitij Patil</title><link>https://ksh.fyi/tags/gcc/</link><description>Recent content in gcc on Kshitij Patil</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sat, 03 Oct 2026 20:30:00 +0530</lastBuildDate><atom:link href="https://ksh.fyi/tags/gcc/index.xml" rel="self" type="application/rss+xml"/><item><title>Reading GCC error messages</title><link>https://ksh.fyi/notes/reading-gcc-errors/</link><pubDate>Sat, 03 Oct 2026 20:30:00 +0530</pubDate><guid>https://ksh.fyi/notes/reading-gcc-errors/</guid><description>&lt;p&gt;Every GCC diagnostic has the same shape:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;scratch.c:3:13: error: initialization discards &amp;#39;const&amp;#39; qualifier from pointer target type [-Werror=discarded-qualifiers]
3 | char *s = &amp;#34;scratching surface&amp;#34;;
| ^~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id="line-1-filelinecol-severity-message-flag"&gt;Line 1: &lt;code&gt;file:line:col: severity: message [flag]&lt;/code&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;scratch.c:3:13&lt;/code&gt;&lt;/strong&gt;: where. Columns start at 1. Most terminals/editors
make this clickable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;severity&lt;/strong&gt;: &lt;code&gt;error&lt;/code&gt; stops the build; &lt;code&gt;warning&lt;/code&gt; doesn&amp;rsquo;t; &lt;code&gt;note:&lt;/code&gt; adds
context to the diagnostic above it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;[-Werror=discarded-qualifiers]&lt;/code&gt;&lt;/strong&gt;: the flag that controls this check.
&lt;code&gt;-Werror=&lt;/code&gt; means it&amp;rsquo;s really the warning &lt;code&gt;-Wdiscarded-qualifiers&lt;/code&gt;, promoted
to an error by &lt;code&gt;-Werror&lt;/code&gt;. Search &lt;code&gt;man gcc&lt;/code&gt; for &lt;code&gt;/Wdiscarded-qualifiers&lt;/code&gt;.
&lt;code&gt;-Wno-&amp;lt;name&amp;gt;&lt;/code&gt; disables it, which is rarely the right fix.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="the-message-has-a-fixed-vocabulary"&gt;The message has a fixed vocabulary&lt;/h2&gt;
&lt;p&gt;The first words say &lt;em&gt;where in the code&lt;/em&gt; the problem is:&lt;/p&gt;</description><content:encoded><![CDATA[<p>Every GCC diagnostic has the same shape:</p>
<pre tabindex="0"><code>scratch.c:3:13: error: initialization discards &#39;const&#39; qualifier from pointer target type [-Werror=discarded-qualifiers]
    3 |   char *s = &#34;scratching surface&#34;;
      |             ^~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
</code></pre><h2 id="line-1-filelinecol-severity-message-flag">Line 1: <code>file:line:col: severity: message [flag]</code></h2>
<ul>
<li><strong><code>scratch.c:3:13</code></strong>: where. Columns start at 1. Most terminals/editors
make this clickable.</li>
<li><strong>severity</strong>: <code>error</code> stops the build; <code>warning</code> doesn&rsquo;t; <code>note:</code> adds
context to the diagnostic above it.</li>
<li><strong><code>[-Werror=discarded-qualifiers]</code></strong>: the flag that controls this check.
<code>-Werror=</code> means it&rsquo;s really the warning <code>-Wdiscarded-qualifiers</code>, promoted
to an error by <code>-Werror</code>. Search <code>man gcc</code> for <code>/Wdiscarded-qualifiers</code>.
<code>-Wno-&lt;name&gt;</code> disables it, which is rarely the right fix.</li>
</ul>
<h2 id="the-message-has-a-fixed-vocabulary">The message has a fixed vocabulary</h2>
<p>The first words say <em>where in the code</em> the problem is:</p>
<table>
	<thead>
			<tr>
					<th>Phrase</th>
					<th>Means</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>initialization</code></td>
					<td>the <code>=</code> in a declaration (<code>T x = ...</code>)</td>
			</tr>
			<tr>
					<td><code>assignment</code></td>
					<td>a plain <code>x = ...</code></td>
			</tr>
			<tr>
					<td><code>passing argument N of 'f'</code></td>
					<td>a function call</td>
			</tr>
			<tr>
					<td><code>return</code></td>
					<td>a <code>return</code> statement</td>
			</tr>
	</tbody>
</table>
<p>The rest says <em>what&rsquo;s wrong</em>. Here: &ldquo;discards &lsquo;const&rsquo; qualifier&rdquo; = a <code>const</code>
would be lost (<code>const</code>/<code>volatile</code> are <em>qualifiers</em>); &ldquo;from pointer target
type&rdquo; = the <code>const</code> is on the thing <strong>pointed to</strong>, not on the pointer.</p>
<h2 id="lines-23-the-source-excerpt">Lines 2–3: the source excerpt</h2>
<p><code>^</code> marks the column, <code>~~~</code> underlines the whole expression it&rsquo;s blaming:
here the literal, not <code>s</code>.</p>
<h2 id="note-lines-compare-the-types"><code>note:</code> lines: compare the types</h2>
<p>Type errors usually come with a note giving both types:</p>
<pre tabindex="0"><code>error: passing argument 2 of &#39;execv&#39; from incompatible pointer type
note: expected &#39;char * const*&#39; but argument is of type &#39;const char * const*&#39;
</code></pre><p>Put them side by side and <a href="https://ksh.fyi/notes/reading-c-declarations/">read each right to left</a>.
The difference is the bug.</p>
<h2 id="the-last-line-whos-talking">The last line: who&rsquo;s talking</h2>
<p><code>gcc</code> is a <em>driver</em> that runs a pipeline: preprocessor → <strong><code>cc1</code></strong> (the
compiler) → <code>as</code> (assembler) → <code>ld</code> (linker, errors show up via
<code>collect2</code>). The prefix tells you which stage complained. <code>undefined reference to 'foo'</code> with no line number = linker: compilation succeeded,
the final link couldn&rsquo;t find <code>foo</code>.</p>
<h2 id="habits">Habits</h2>
<ul>
<li>Fix the <strong>first</strong> error first. One missing <code>;</code> or <code>}</code> can cause a cascade.</li>
<li>Read the <code>note:</code>s.</li>
<li>Look the flag up in <code>man gcc</code> before silencing it.</li>
</ul>
]]></content:encoded></item><item><title>C string literals are read-only, but not const</title><link>https://ksh.fyi/notes/c-string-literals/</link><pubDate>Sat, 03 Oct 2026 20:10:00 +0530</pubDate><guid>https://ksh.fyi/notes/c-string-literals/</guid><description>&lt;p&gt;A string literal like &lt;code&gt;&amp;quot;/bin/sh&amp;quot;&lt;/code&gt; is stored in &lt;code&gt;.rodata&lt;/code&gt;, which is mapped
read-only (see &lt;a href="https://ksh.fyi/notes/c-memory-sections/"&gt;where things live in a C program&lt;/a&gt;).
But its C type is plain &lt;code&gt;char[N]&lt;/code&gt;, &lt;strong&gt;not&lt;/strong&gt; &lt;code&gt;const char[N]&lt;/code&gt; (C++ fixed this, C
kept it for historical reasons). So the compiler happily lets you do this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;/bin/sh&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// compiles without a warning by default
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;X&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// undefined behavior; in practice SIGSEGV
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre tabindex="0"&gt;&lt;code&gt;$ ./ub
Segmentation fault (exit status 139 = 128 + SIGSEGV)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id="-wwrite-strings-make-the-compiler-tell-you"&gt;&lt;code&gt;-Wwrite-strings&lt;/code&gt;: make the compiler tell you&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;-Wwrite-strings&lt;/code&gt; gives literals the type &lt;code&gt;const char[N]&lt;/code&gt;, so pointing a plain
&lt;code&gt;char *&lt;/code&gt; at one becomes a warning (an error with &lt;code&gt;-Werror&lt;/code&gt;):&lt;/p&gt;</description><content:encoded><![CDATA[<p>A string literal like <code>&quot;/bin/sh&quot;</code> is stored in <code>.rodata</code>, which is mapped
read-only (see <a href="https://ksh.fyi/notes/c-memory-sections/">where things live in a C program</a>).
But its C type is plain <code>char[N]</code>, <strong>not</strong> <code>const char[N]</code> (C++ fixed this, C
kept it for historical reasons). So the compiler happily lets you do this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="cl"><span class="kt">char</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="s">&#34;/bin/sh&#34;</span><span class="p">;</span>   <span class="c1">// compiles without a warning by default
</span></span></span><span class="line"><span class="cl"><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="sc">&#39;X&#39;</span><span class="p">;</span>            <span class="c1">// undefined behavior; in practice SIGSEGV
</span></span></span></code></pre></div><pre tabindex="0"><code>$ ./ub
Segmentation fault        (exit status 139 = 128 + SIGSEGV)
</code></pre><h2 id="-wwrite-strings-make-the-compiler-tell-you"><code>-Wwrite-strings</code>: make the compiler tell you</h2>
<p><code>-Wwrite-strings</code> gives literals the type <code>const char[N]</code>, so pointing a plain
<code>char *</code> at one becomes a warning (an error with <code>-Werror</code>):</p>
<pre tabindex="0"><code>scratch.c:3:13: error: initialization discards &#39;const&#39; qualifier from pointer target type [-Werror=discarded-qualifiers]
    3 |   char *s = &#34;scratching surface&#34;;
      |             ^~~~~~~~~~~~~~~~~~~~
</code></pre><p>The fix is to say what you mean: <code>const char *s = &quot;...&quot;;</code>. It&rsquo;s not part of
<code>-Wall</code> or <code>-Wextra</code>; you have to add it yourself.</p>
<h2 id="pointer-to-a-literal-vs-array-initialized-from-one">Pointer to a literal vs. array initialized from one</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="cl"><span class="kt">char</span> <span class="o">*</span><span class="n">s</span> <span class="o">=</span> <span class="s">&#34;/bin/sh&#34;</span><span class="p">;</span>   <span class="c1">// A
</span></span></span><span class="line"><span class="cl"><span class="kt">char</span>  <span class="n">s</span><span class="p">[]</span> <span class="o">=</span> <span class="s">&#34;/bin/sh&#34;</span><span class="p">;</span> <span class="c1">// B
</span></span></span></code></pre></div><p>They look almost identical but are very different:</p>
<ul>
<li><strong>A</strong> is a pointer. It points <em>at the literal itself</em>, in <code>.rodata</code>.
With <code>-Wwrite-strings</code> this is the error above.</li>
<li><strong>B</strong> is an array of 8 <code>char</code>s (7 + <code>'\0'</code>). The literal is only its
<em>initializer</em>: the bytes are <strong>copied</strong> into the array. No pointer, no
<code>const</code> dropped, and the array is yours to modify.</li>
</ul>
<table>
	<thead>
			<tr>
					<th></th>
					<th>The variable <code>s</code></th>
					<th>The bytes <code>&quot;/bin/sh&quot;</code></th>
					<th><code>s[0] = 'X'</code></th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>A (local)</td>
					<td>8-byte pointer on the stack</td>
					<td>the literal in <code>.rodata</code></td>
					<td>UB / SIGSEGV</td>
			</tr>
			<tr>
					<td>B (local)</td>
					<td>the array itself, on the stack</td>
					<td>a copy, inside the array</td>
					<td>fine</td>
			</tr>
			<tr>
					<td>B (<code>static</code> or global)</td>
					<td>the array itself, in <code>.data</code></td>
					<td>a copy, inside the array</td>
					<td>fine</td>
			</tr>
	</tbody>
</table>
<p>More differences:</p>
<ul>
<li>A can be repointed (<code>s = &quot;other&quot;;</code>). B can&rsquo;t: arrays aren&rsquo;t assignable,
only their contents are.</li>
<li><code>sizeof s</code> is the pointer size for A and the array size for B. For
<code>&quot;/bin/sh&quot;</code> both happen to be 8 on a 64-bit machine; use a longer string to
see them differ.</li>
</ul>
<p>The real question isn&rsquo;t &ldquo;binary or stack&rdquo;, it&rsquo;s <strong>&ldquo;am I pointing at the
literal, or do I own a copy?&rdquo;</strong></p>
<p>References: the C23 standard, sections &ldquo;String literals&rdquo; and &ldquo;Initialization&rdquo;;
<code>man gcc</code> (<code>-Wwrite-strings</code>).</p>
]]></content:encoded></item></channel></rss>