32

When linking against libraries using the -l option (say -lfoo), gcc will prefer a shared object to a static library if both are found (will prefer libfoo.so to libfoo.a). Is there a way to make gcc prefer the static library, if both are found?

The issue I'm trying to solve is the following: I'm creating a plugin for an application (the flight simulator called X-Plane), with the following constraints:

  • the plugin is to be in the form of a 32 bit shared object, even when running on a 64 bit system
  • the running environment does not provide a convenient way to load shared objects which are not in the 'normal' locations, say /usr/lib or /usr/lib32:
    • one cannot expect the user to set LD_PRELOAD or LD_LIBRARY_PATH to find shared objects shipped with my plugin
    • the X-Plane running environment would not add my plugins directory to ``LD_LIBRARY_PATH, before dynamically loading the plugin shared object, which would allow me to ship all my required shared objects alongside my plugin shared object
  • one cannot expect 64 bit users to install 32 bit shared objects that are non-trivial (say, are not included in the ia32-libs package on ubuntu)

to solve the above constraints, a possible solution is to link the generated shared object against static, 32 bit versions of all non-trivial libraries used. but, when installing such libraries, usually both static and dynamic versions are installed, and thus gcc will always link against the shared object instead of the static library.

of course, moving / removing / deleting the shared objects in question, and just leaving the static libraries in say /usr/lib32, is a work-around, but it is not a nice one

note:

  • yes, I did read up on how to link shared objects & libraries, and I'm not trying to creatae a 'totally statically linked shared object'
  • yes, I tried -Wl,-static -lfoo -Wl,-Bdynamic, but didn't bring the expected results
  • yes, I tried -l:libfoo.a as well, but this didn't bring the expected results either
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
Ákos Maróy
  • 919
  • 2
  • 10
  • 19

3 Answers3

11

You can specify the full path to the static libs without the -l flag to link with those.

gcc ... source.c ... /usr/lib32/libmysuperlib.a ...
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    Sure, but... now I have to actually _know_ where the static lib is, whereas with `-l`, I just need to pass the name of the lib I need – Romário May 26 '22 at 15:25
6

Just add the .a file to the link line without -l as if it were a .o file.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Angelom
  • 2,413
  • 1
  • 15
  • 8
1

It's dated, but may work: http://www.network-theory.co.uk/docs/gccintro/gccintro_25.html

(almost end of the page)

"As noted earlier, it is also possible to link directly with individual library files by specifying the full path to the library on the command line."

h.z.
  • 19
  • 1