views:

43

answers:

2

I would like to display the explicit path to a library which will be used in the linking stage of compilation. I want to do this so that I can add the library as a dependency to a separate object file.

In other words, I often link using:

g++ myFile.cpp -Lsomewhere -Lelse -Lhere -Lthere -lfoo

Is there a way to coerce g++, ld, ldd, or something else to resolve '-lfoo' using the -L's without actually linking anything so that I can use the explicit path as a dependency? For more explicit info, see Makefile Updated Library Dependency.

+1  A: 

Since you know the order in which the linker searches directories looking for the library in question (see the manual), you can use Make's vpath to search them in the same order:

vpath %.so somewhere else here there

otherObjectFile: foo.so
  #whatever...
Beta
+1  A: 

This isn't ideal, and I hope there is a cleaner answer, but you can get the default search paths from gcc, then search each one for the files. Here it is in GNU make:

libnams = foo bar
dirs = somewhere else here there
dirs += $(subst :, ,$(subst =,,$(word 2,$(shell gcc -print-search-dirs | grep libraries))))
exts = a so
paths = $(foreach L, $(libnams), \
           $(firstword $(foreach D, $(dirs), \
              $(foreach E, $(exts), \
                 $(wildcard $(D)/lib$(L).$(E))))))
John