How do I link a library file in GCC that does not start with lib?
Asked Answered
P

2

15

When I link a library such as libm in with ld, I need to drop the lib prefix. What if the file does not follow this naming convention? Is there a way to link it other than renaming the file?

Popup answered 19/4, 2012 at 18:13 Comment(0)
B
16

You can link against any library, e.g. foo.a, by specifying full path to it on the link line:

gcc main.o /path/to/foo.a

What you lose with non-standard library name is the ability for the linker to search for it, e.g. this will not work:

gcc main.o -L/path/to foo.a

You can avoid that lack of search by using -l:foo.a syntax:

gcc main.o -L/path/one -L/path/two -l:foo.a

When I link a library such as libm in with ld

Note that in general you should not link anything with ld. Use the compiler driver instead -- it adds objects and libraries to the link line that are required for correct result.

Brood answered 21/4, 2012 at 14:33 Comment(0)
M
25

You can have the linker search for a library named without the lib prefix:

gcc main.o -L/path/to/foo -l:foo.a

This is especially useful in environments where a list of libraries is specified and the -l flag is prepended later (eg some makefiles or eclipse CDT)

Mouseear answered 18/9, 2014 at 13:0 Comment(1)
Aah I see now, I didn't catch the "-l" in there.Jalopy
B
16

You can link against any library, e.g. foo.a, by specifying full path to it on the link line:

gcc main.o /path/to/foo.a

What you lose with non-standard library name is the ability for the linker to search for it, e.g. this will not work:

gcc main.o -L/path/to foo.a

You can avoid that lack of search by using -l:foo.a syntax:

gcc main.o -L/path/one -L/path/two -l:foo.a

When I link a library such as libm in with ld

Note that in general you should not link anything with ld. Use the compiler driver instead -- it adds objects and libraries to the link line that are required for correct result.

Brood answered 21/4, 2012 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.