3

I want to use gcc to produce a shared library, but i want to link some other libraries it depends on statically. Now to produce the "standard" dynamically linked output file i use

gcc -dynamiclib *.o -lfoo -lbar -o outfile

which would be

gcc -shared *.o -lfoo -lbar -o outfile

on for a binutils ld on a linux system. Now if i want libfoo and libbar to be linked statically, I can name the static libraries directly

gcc -dynamiclib *.o /usr/lib/libfoo.a /usr/lib/libbar.a -o outfile

however, that way i have to look for the library files myself. GNU binutils ld supports this:

gcc -shared *.o -l:libfoo.a -l:libbar.a -o outfile

but apple's ld doesnt.

  • Is there a way to make apple's ld look for the static libraries himself?
  • If not, is there another way that would avoid naming the exact location of the archives, e.g. producing an intermediate output file out of the object files requiring libfoo and libbar with the -static switch and linking that file together with the remaining objectfiles to create the dynamic object?
barbaz
  • 1,642
  • 2
  • 17
  • 27

2 Answers2

9

Quoting QA1393,

Normally, the linker goes through each path in the search paths one at a time to find a dynamic version of the library. If none is found, it goes through each of those paths looking for a static version of the same library. There is no way to choose a static library over a corresponding dylib if both libraries are in the same directory without using the -l linker option and absolute paths to each library.

As recommended by QA1393, you can place your static libraries in a different directory, use -L/path/to/static/libraries before other occurrences of -L that could point to dynamic libraries, and -search_paths_first so that the linker tries both .dylib (which won’t be there) and .a in the first search path before searching the next search path and so forth.

pyrmont
  • 225
  • 5
  • 12
  • i'm afraid that doesn't solve the problem at all... what i'm asking for is the comfort of -l resolution - if i wanted to put the libraries somewhere or add their paths i can as well name the archive files directly. – barbaz Jan 02 '11 at 15:10
  • If there is no .dylib in the search paths, the linker will use .a. However, if there is a dynamic version available, you cannot prevent the linker from using it. If that’s the case, I’m afraid there’s no solution for your problem. –  Jan 02 '11 at 15:14
3

I ran into the same problem. As it turned out, there seems to be no way to link libraries statically without specifying the full path to the .a-file.

However, there seems to be neat trick in Makefile's allowing for smooth usage.

vpath %.a /opt/local/lib
.LIBPATTERNS lib%.a lib%.dylib lib%.so

STATICLIBS = -lssh2

libmy.dylib: my1.o my2.o $(STATICLIBS)
  g++ -dynamiclib -o libmy.dylib $^

Note how the $(STATICLIBS) variable is put in the dependencies. Make will not treat dependecies with an '-l' prefix as files - but rather as libraries. Using the above vpath magic make looks up the libraries and put the full path on the commandline to g++.

Mike T
  • 41,085
  • 18
  • 152
  • 203
vidstige
  • 12,492
  • 9
  • 66
  • 110