Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 574
JeffV Avatar asked Apr 19 '12 18:04

JeffV


People also ask

Which gcc option is used to link the library?

The -l option tells gcc to link in the specified library.

How do I create a dynamic library in gcc?

To create a dynamic library in Linux, simply type the following command: gcc *. c -c -fPIC and hit return. This command essentially generates one object file .o for each source file .


1 Answers

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.

like image 71
Employed Russian Avatar answered Oct 01 '22 04:10

Employed Russian