G++ Linker: Force Static Linking If Static Library Exists

g++ linker: force static linking if static library exists?

g++ -Wl,-Bstatic -lz -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed

Will link zlib and libfoo as static, and libbar as dynamic . --as-needed will drop any unused dynamic library.

Proper way to link a static library using GCC

Thanks for the replies! Turns out the problem was due to link order. Apparently, if you use a library which in turn has other library dependencies, those other dependencies must be listed after the library, not before as I had been doing. Learned something new!

static linking only some libraries

gcc -lsome_dynamic_lib code.c some_static_lib.a

Telling gcc directly to link a library statically

Use -l: instead of -l. For example -l:libXYZ.a to link with libXYZ.a. Notice the lib and .a are written out, as opposed to -lXYZ which would auto-expand to libXYZ.so/libXYZ.a.

It is an option of the GNU ld linker:

-l namespec ... If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a. ... on ELF ... systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. ... Note that this behavior does not apply to :filename, which always specifies a file called filename."

(Since binutils 2.18)

Note that this only works with the GNU linker. If your ld isn't the GNU one you're out of luck.

Static linking failed although the name exists

There are two different issues there, the first of which is the simplest, you have used the wrong compiler options. The -L option tells the linker to also look in the directory when looking for a library. The -l tells it to link the specific library. To link you would then use:

g++ -o test test.o -L/path/to -lcovis

or

g++ -o test test.o -l/path/to/libcovis.a

To force static linking if the same library is present as a dynamic library in the same directory.

The second potential issue is that the order of static libraries in the linker command line does matter, so that might also be an issue if there is a dependency on different static libs.

g++ -o test tests.o -ldependent -lprovider

The linker will process the libraries in order as they are in the command line, and from each static lib it will only pull those symbols that are required (with as much information as the linker has at that time). In the command line above, the linker will extract from dependent the symbols it needs for test.o, and that might in turn add new undefined symbols to the program (the dependencies of dependent). When it processes provider it will fill in those symbols. If the order was reversed in the command line, the symbols that are required by dependent but not by test.o would not be added to the executable, as the linker does not know that those symbols will be needed when processing provider.

while compiling a code using gcc on mac OS linker failed in linking custom made static library

-static is in error here. To link to your static lib just plain link to it without any -static flag, which is for kernel compilation.



Related Topics



Leave a reply



Submit