Ice
Ice

Reputation: 1971

How to compile gcc with static library?

I have static library lib.a and in all tutorials using:

gcc -o main main.o -L. -lib

But I cant, I have errors:

    /usr/bin/ld: cannot find -lib
collect2: error: ld returned 1 exit status

I need to use:

gcc -o main main.o -L. -lib.a

Why? What should I do to repair it ?

Upvotes: 1

Views: 4940

Answers (2)

romain-aga
romain-aga

Reputation: 1561

Do you have the error with this line ?

gcc -o main main.o -L. -llib

As MicroVirus found in the documentation, you will have to rename your library in liblib.a to use my previous line or just pass your library to gcc like a simple file.

Upvotes: 0

MicroVirus
MicroVirus

Reputation: 5477

From the documentation of gcc -l:

-llibrary:

The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.

...

The only difference between using an -l option and specifying a file name is that -l surrounds library with ‘lib’ and ‘.a’ and searches several directories.

So you cannot use -l with a library named 'lib.a'. Use 'lib.a' without the -l to include it. Of course, you cannot use -L then to set the directories to be searched for this particular library.

Upvotes: 2

Related Questions