Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: controlling where `ld` searches for .o object files?

Ok, this is the situation: I'm trying to use some older software: works fine on Ubuntu Lucid, fails on Natty.

So, I straced around a bit, and it turns out that this software calls ld, and ld eventually fails with:

.../ld: crt1.o: No such file: No such file or directory

... yes, the old crti.o file missing error :) However, I'd like to ask the question in more general terms...

The thing is, this is 'standalone' (older) ld here, and when I run .../ld -verbose | less, I get:

...
SEARCH_DIR("/usr/local/lib"); 
SEARCH_DIR("/lib"); 
SEARCH_DIR("/usr/lib");
...

Now, the thing is that:

  • On Lucid, crt1.o is in /usr/lib/crt1.o
  • On Natty, crt1.o is in /usr/lib/i386-linux-gnu/crt1.o

... so no surprise why crt1.o cannot be found, I guess. It seems, all I have to do, is tell ld to look for crt1.o in /usr/lib/i386-linux-gnu, but how do I do that?

I thought I could use the -L option, but man ld says:

to link a file "hello.o":

    ld -o <output> /lib/crt0.o hello.o -lc

  This tells ld to produce a file called output as the result of linking
  the file "/lib/crt0.o" with "hello.o" and the library "libc.a", which
  will come from the standard search directories. 
...

-L searchdir
--library-path=searchdir
  Add path searchdir to the list of paths that ld will search for
  archive libraries and ld control scripts.

... meaning, '-L' would influence where we look for "libc.a" (in man example) - but not for the object files.

I would actually prefer an environment variable for this, but I tried both LD_PRELOAD_PATH and LD_LIBRARY_PATH to no avail (I guess, those are related to "shared objects", and these .o files aren't one of those).

Does anyone know if there is an environment variable (preferably - or if not, command line option to ld) that would control where ld searches for .o object files?

As a note, I guess I could just symlink /usr/lib/i386-linux-gnu/crt1.o in /usr/lib/, but I'd rather use an environment variable, if it exists... If not, are there any other possible solutions to this?

Thank in advance for any answers,
Cheers!

EDIT: possibly relevant: Daniel Kegel - --with-sysroot newbie troubles: "ld: cannot open crt1.o"

like image 511
sdaau Avatar asked Mar 30 '11 15:03

sdaau


1 Answers

You can use also --sysroot:

   --sysroot=dir
       Use dir as the logical root directory for headers and libraries.  For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches
       dir/usr/include and dir/usr/lib.

       If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.

       The GNU linker (beginning with version 2.16) has the necessary support for this option.  If your linker does not support this option, the header file aspect of --sysroot still works, but the
       library aspect does not.

!!! => The GNU linker (beginning with version 2.16) has the necessary support for this option.

like image 126
Vladyslav Savchenko Avatar answered Oct 21 '22 22:10

Vladyslav Savchenko