I was looking here 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.
fact.h
int fact (int); fact.c
int fact (int f) { if ( f == 0 ) return 1; else return (f * fact ( f - 1 )); } main.c
#include <stdio.h> #include "fact.h" int main(int argc, char *argv[]) { printf("%d\n", fact(3)); return 0; } So I had first to generate the object file (phase 1)...
$ gcc -c fact.c -o fact.o ...then to generate the static library (phase 2)...
$ ar rcs libfact.a fact.o ...later to do the static library linking process (phase 3)...
$ gcc -static main.c -L. -lfact -o fact ...and finally run the program (phase 4 and final)
$ ./fact 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?
Edit/Problem Solved: 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.
main.c
#include <stdio.h> #include "include/fact.h" int main(int argc, char *argv[]) { printf("%d\n", fact(3)); return 0; } Second, to generate the static library in another directory (phase 2), this is what I had done:
$ ar rcs lib/libfact.a fact.o