1

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 

2 Answers 2

4

Here is your answer,

$ gcc -static main.c -L. -lfact -o fact

-L Add directory to the list of directories to be searched for -l

Its in the link that you gave. If you put the seach direction correctly and low search range, it will not be a problem. Otherwise it is not going to compile the code. Because code did not know where is the header.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried it with the header in the header directory (/include/fact.h) but it gave me an error in the main program that says the file fact.h doesn't exists. Maybe it's not a problem with the compilation only, maybe I have to change something in the include in the main program.
Please check the directory that you can wrote it wrong. c:// or c:/ or C:// variations maybe have problem. And try to give the exact location of header other than search range. #include "fact.h" this means that its near the main program. You should use #include <fact.h> try with this.
I tried, but the problem persists: Error in the main program indicating that the file fact.h doesn't exists.
I solved the problem. The command you ṕosted, who was also on the link, worked. The problem wasn't in the compilation, was in the code. As a matter of fact, I corrected the include with #include "include/fact.h". Anyway, thanks =)
2

You can add -I to specify include path(s).

gcc -I/include fact.c gcc -I/include -static main.c -L/lib -lfact -o fact_main 

1 Comment

Same problem as above: Error in the main program indicating that the file fact.h doesn't exists.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.