The question contains some misconceptions, especially here:
When I use my -L/dir2/lib
flag to look for -lfoo
, the libbar
compilation/linking fails because of the libfoo.la
file is expecting
foo to be installed in /dir1/lib
If linking with -L/dir2/lib -lfoo
fails, it is not because of anything in libfoo.la
. The .la
file is used only by libtool
. It is not involved in linking performed by other means. In fact, it can be removed altogether without impacting linking. If /dir2/lib/libfoo.so
is a shared library of the appropriate architecture, and there is otherwise no libfoo.so
or libfoo.a
in the library search path, then the -L/dir2/lib -lfoo
options will cause a standard Unix compiler or linker that is considering shared libraries at all to attempt to include /dir2/lib/libfoo.so
in the link.
No link error messages are presented in the question, and this long after the fact, I don't expect any to be added. Without them, it's unclear what the nature of the problem actually was. Among the more likely of the many possibilities are
- the library was built for the wrong architecture (presumably, x86 vs x86_64). In this case, a version of the library suitable for the second machine needs to be built from source.
- the ownership and permissions of the library, as installed on the second machine, did not permit the OP's builds there to read it. In this case, the ownership and / or permissions should be fixed.
- the library had dependencies that were not (correctly) satisfied on the second machine. In this case, the dependencies need to be installed, and probably included in the link, as well.
- the library was inherently sensitive to its installation directory. This is never the case on Linux, but it may be the case on some other kinds of machines. In this situation,
libtool
can use the .la
file to put the library in the location specified for it at build time, but it would need to be rebuilt to be usable from a different location.
- it was never a link error at all, but rather a runtime failure involving the library. This might happen because
/dir2/lib/libfoo.so
is not in the second system's runtime library search path, or because it depends on other libraries that are not. On an ELF-based system, libtool
will have inserted RPATH
entries into the library, but they are likely to not be appropriate for a different installation directory. They might even actively cause such an issue. Updating the dynamic linker's configuration appropriately might help in this situation.
How do I avoid this problem?
It's not the problem you think it is, and without more information, I can offer only general advice:
When using an Autotools build system, build for the correct installation directory. Don't assume that the result will be relocatable to a different installation directory (though it might be). A different machine, yes, if sufficiently alike, but not a different location. But if you want the best chance of being able to relocate the installation, then
Consider using a tool such as chrpath
to remove the RPATH entry from the library, or even hacking libtool
at build time to prevent it from adding one in the first place. Especially if you anticipate installing the library other than in its configure
d location.
Be sure to configure the system's dynamic linker to have the installation directory in its search path.
Remove the installed .la
file if you are changing the installation path, and maybe even if you aren't.
Ensure that all the library's dependencies are also installed and in the dynamic linker's search path, and that they are actually compatible.
All of that is consistent with RedHat's RPM packaging guidelines, which cater to exactly the case of building software on one machine for installation and use on another. It is not all absolutely required, however, and there are alternative philosophies about some of that stuff, especially RPATH. When you have more experience, feel free to ignore as much of that and any other advice on the topic as you like.
.la
files. – Gerrilee