Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic libraries and headers in another directories with GCC
    primarykey
    data
    text
    <p>I was looking <a href="http://segfault.in/2008/04/how-to-create-static-libraries-with-gcc/" rel="nofollow">here</a> how to do static libraries using GCC, and the explanation is pretty clear (despise the fact I had to rewrite the factorial function): I have a function (fact.c), the header of the function (fact.h), and the main function (main.c), all of them in my home directory. </p> <p><strong>fact.h</strong></p> <pre><code>int fact (int); </code></pre> <p><strong>fact.c</strong></p> <pre><code>int fact (int f) { if ( f == 0 ) return 1; else return (f * fact ( f - 1 )); } </code></pre> <p><strong>main.c</strong></p> <pre><code>#include &lt;stdio.h&gt; #include "fact.h" int main(int argc, char *argv[]) { printf("%d\n", fact(3)); return 0; } </code></pre> <p>So I had first to generate the object file (phase 1)...</p> <pre><code>$ gcc -c fact.c -o fact.o </code></pre> <p>...then to generate the static library (phase 2)...</p> <pre><code>$ ar rcs libfact.a fact.o </code></pre> <p>...later to do the static library linking process (phase 3)...</p> <pre><code>$ gcc -static main.c -L. -lfact -o fact </code></pre> <p>...and finally run the program (phase 4 and final)</p> <pre><code>$ ./fact </code></pre> <p>My question is the following. Let's suppose my program will be so big that I had no alternative than put the headers in a header directory (/include/fact.h) and the static libraries will also be in another directory (/lib/libfact.a). In that case, how the compilation and/or the code of this program will change? </p> <p><strong>Edit/Problem Solved</strong>: First, the main.c was corrected in order to include a header in another directory called include. Remember that, in this case, both of the .c files are in the main directory.</p> <p><strong>main.c</strong></p> <pre><code>#include &lt;stdio.h&gt; #include "include/fact.h" int main(int argc, char *argv[]) { printf("%d\n", fact(3)); return 0; } </code></pre> <p>Second, to generate the static library in another directory (phase 2), this is what I had done:</p> <pre><code>$ ar rcs lib/libfact.a fact.o </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload