47

I'm compiling some code which uses libcurl on a Debian Linux system. My dev machine is running Debian 5 but I want the binary to be usable on older Debian 4 systems too.

I find that if I specify -lcurl it will link to libcurl.so.4 but Debian 4 systems only have libcurl.so.3

Is there some way I can tell GCC to link to either libcurl.so.3 (which exists in both Debian 4 and 5) or just libcurl.so so it will use whatever version is available ?

Adam Pierce
  • 32,221
  • 21
  • 68
  • 87
  • 1
    On the older Debian, isn't libcurl.so a symlink to libcurl.so.3 ? I mean, it looks strange that -lcurl does not the right thing by default. – Laurynas Biveinis May 06 '09 at 06:13
  • kastauyra: the versions are not, or at least cannot be assumed to be, binary compatible. So when you link it records the major version linked against in the binary: if you compile on the newer system it will require version 4 and not work on the old system. (Actually what it records is the soname, which is a string stored in the library file which conventionally but not necessarily is something "libcurl.so.3") – Mark Baker Jun 11 '09 at 13:30

4 Answers4

86

Instead of using "-lcurl" use "-l:libcurl.so.3" And ofcourse also use "-L _installed_path_"

engineer.udays
  • 961
  • 1
  • 6
  • 4
  • 2
    same problem when it's symlinked. -l:libX.so links to libX.so.Y instead – Gregory Oct 08 '15 at 04:22
  • 1
    This doesn't work for me even with the rpath. ldd shows it linking with one thing. The linked output shows the correct filename. But strace confirms it is loading file.2 instead of file.2.3 and it isn't a symlink. – jgmjgm Nov 09 '15 at 11:21
  • I am tempted to mark this as an answer. You truly saved my say @engineer.udays – Hardik Kamdar Nov 04 '16 at 05:15
  • it does not really work for me for some reason, even if I replace symlink with actual file and delete all the other libraries its still linking against specific version, which it probably gets from the SONAME – igor May 18 '18 at 16:01
30

You can pass the actual .so file instead of -l on the linker command line, and it ought to do what you want.

bdonlan
  • 214,833
  • 29
  • 259
  • 321
  • 3
    Ah, that almost works except that libcurl.so is just a symlink to lubcurl.so.4 on Debian and so it still links to libcurl.so.4. – Adam Pierce May 06 '09 at 04:53
  • I'll mark this as accepted answer although it is not exactly right for libcurl on Debian 5. What I ended up doing is getting a copy of libcurl.so.3 from a Debian 4 system and linking directly to that by specifying the .so filename as bdonlan suggested. – Adam Pierce May 06 '09 at 06:35
1

I think the correct way to do that will be to use the --filter and --auxiliary flags of the linker.

They are not very documented, but should allow you to load symbols from different versions of the same library according to the machine you are installed on.

Rick Smith
  • 8,765
  • 14
  • 80
  • 84
galk
  • 61
  • 7
1

How about creating a symlink local to your project that links to .3, then you can just use -L at compile time. I'm not sure if you'd get a name conflict though, but you could always call it libcurl-old.so just in case.

Dana the Sane
  • 14,292
  • 8
  • 54
  • 79