Linking files/headers in C
<p>Let's say I have the following program (<code>hello.c</code>):</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

#define NAME "ashoka"


int main(int argc, char *argv[])
{
    printf("Hello, world! My name is %s\n", NAME);
}
</code></pre>

<p>So, as I understand it the <strong>process of compiling</strong> this program is:</p>

<ol>
<li><p><strong>Preprocessing</strong>: will copy-paste the <em>stdio.h</em> and <em>math.h</em> functions declarations and replace <code>NAME</code> with <code>"ashoka"</code>.</p>

<pre><code>clang -E hello.c
</code></pre></li>
<li><p><strong>Compiling</strong>: will turn <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a> code into <em>assembly</em> code</p>

<pre><code>clang -S hello.c
</code></pre>

<p>file produced: <strong>hello.s</strong></p></li>
<li><p><strong>Assembling</strong>: transform <em>assembly</em> code to <em>object</em> code</p>

<pre><code>clang -c hello.s
</code></pre>

<p>file produced: <strong>hello.o</strong></p></li>
<li><p><strong>Linking</strong>: combine <em>object</em> files into one file that we will execute.</p>

<pre><code>clang hello.o -lm
</code></pre>

<p>OR (let's say I also want to link hello2.o)</p>

<pre><code>clang hello.o hello2.o
</code></pre></li>
</ol>

<hr>

<p>So, here come the questions:</p>

<ol>
<li><p>Is the process described the correct one?</p></li>
<li><p>In the linking stage, we link together <code>.o</code> (Object code) files. I know that <code>math.h</code> resides in <code>/usr/include</code> directory. Where is <code>math.o</code>? How does the linker find it?</p></li>
<li><p>What are <code>.a</code> (static libraries) and <code>.so</code> (dynamic libraries) in Linux? And how are they related with <code>.o</code> files and the linking stage?</p></li>
<li><p>Let's say I want to share a library I made with the world. I have a <code>mylib.c</code> file, in which I have declared and implemented my functions. How would I go about sharing this so that people would include it in their projects by doing either <code>#include &lt;mylib.h&gt;</code> or <code>#include "mylib.h"</code>?</p></li>
</ol>