How can I recompile a program so that it does not require setting LD_LIBRARY_PATH in order to run?

1

On a solaris server where I don't have admin, I am compiling various software and installing in $HOME/usr, and it's mostly working. However, some of the programs that I compile in this way depend on libiconv, which I have also installed in $HOME/usr/lib. These programs will not run unless I export LD_LIBRARY_PATH=$HOME/usr/lib. If I do not do this, I get an error such as the following:

ld.so.1: rsync: fatal: libiconv.so.2: open failed: No such file or directory

This is expecially bad for rsync, because it does not always operate in a shell environment, so I don't even have the option of using LD_LIBRARY_PATH to make it work. Is it possible to compile my programs such that I do not have to set LD_LIBRARY_PATH for them to run?

Ryan C. Thompson

Posted 2010-11-04T20:53:49.720

Reputation: 10 085

Answers

2

You want to look up rpath. With gcc, you pass -Wl,-rpath,/directory/with/library to the compile/link. I think with the Solaris compiler the flag was -R/directory/with/library, but I have no Solaris machine to check with.

Rich Homolka

Posted 2010-11-04T20:53:49.720

Reputation: 27 121

I'm using GCC on solaris, and I can use GNU binutils as well. So should I use the -Wl form? – Ryan C. Thompson – 2010-11-04T22:10:00.770

Yeah, -Wl to pass to the linker, and -rpath is what binutils understands, or -Wl,-R,/directory/with/library should work as well (with solaris linker) – Rich Homolka – 2010-11-05T00:17:58.713

Can I put -rpath /directory/with/library in my LDFLAGS, or is it better to put -Wl,-rpath,/directory/with/library in my CFLAGS? – Ryan C. Thompson – 2010-11-05T00:58:51.670

Probably -Wl,-rpath,/path/to/dir in LDFLAGS. Makefiles rarely use ld directly but use ${CC} to drive the ld line, so -Wl is appropriate. – Rich Homolka – 2010-11-05T01:59:24.127

Wait, don't you mean in CFLAGS? – Ryan C. Thompson – 2010-11-05T02:31:51.607

To be honest I'd check in either way. You can test your executable possibly. On Linux I use readelf -d to see the section for RPATH, this may be doable with gnu binutils on Solaris. BUT I meant LDFLAGS. Think of gcc not as a compiler, but as a build tool driver. One of the things it does is drive the link stage. The link will (probably) be gcc -o executable $OBJS $LDFLAGS $LIBS. So, gcc wil pass (because of -Wl) to ld. – Rich Homolka – 2010-11-05T03:05:49.347

When I add this to my CFLAGS, I get ld: fatal: option -dn and -P are incompatible during ./configure, after which it says the the C compiler can't create executables. GNU ld doesn't even have a -P option, so I'm pretty sure GCC is using the Solaris linker, even though I have GNU ld installed in a higher-priority path. – Ryan C. Thompson – 2010-11-05T05:34:52.057

I tried with -R instead of -rpath, and it seems to work. I would still like to know how to use my own ld, but I guess that's another question. – Ryan C. Thompson – 2010-11-05T06:03:05.190