2

I'm linking to a library on my filesystem using ld.

When I run the command ld -verbose -lmylib, I get the following back:

attempt to open /usr/x86_64-linux-gnu/lib64/libmylib.so failed
attempt to open /usr/x86_64-linux-gnu/lib64/libmylib.a failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libmylib.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libmylib.a failed
attempt to open //usr/local/lib64/libmylib.so failed
attempt to open //usr/local/lib64/libmylib.a failed
attempt to open //lib/x86_64-linux-gnu/libmylib.so failed
attempt to open //lib/x86_64-linux-gnu/libmylib.a failed
attempt to open //lib64/libmylib.so failed
attempt to open //lib64/libmylib.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libmylib.so failed
attempt to open //usr/lib/x86_64-linux-gnu/libmylib.a failed
attempt to open //usr/lib64/libmylib.so failed
attempt to open //usr/lib64/libmylib.a failed
attempt to open //usr/local/lib/libmylib.so failed
attempt to open //usr/local/lib/libmylib.a failed
attempt to open //lib/libmylib.so failed
attempt to open //lib/libmylib.a failed
attempt to open //usr/lib/libmylib.so failed
attempt to open //usr/lib/libmylib.a failed
ld: cannot find -lmylib

I'm confused as to why it's trying to open files prefixed with //. This is not the case for another computer of mine. I've tried changing LIBRARY_PATH, LD_LIBRARY_PATH, my PATH, etc. but nothing seems to work. I've looked online, but can't find anything. Where are these paths set?

Thanks.

Citronen
  • 4,050
  • 4
  • 16
  • 20
  • Although it's somewhat tangential, you claim you passed `-libmylib` to `ld`, but the messages, especially the last, suggest you actually passed `-lmylib`. Consistency counts in programming. – Jonathan Leffler Apr 27 '16 at 02:24
  • I dunno if its any consolation, but I get substantially the same output when I run the command on Ubuntu 14.04. I don't know whether the `=` signs earlier in the output (`SEARCH_DIR("/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");`) account for the extra `/`, with the `=` being translated to `/` — see the entry without the `=`. – Jonathan Leffler Apr 27 '16 at 02:31

1 Answers1

3

The problem here is you are trying to link against the library mylib, but this library is not in the system's library search path, or it does not exists at all. You please make it locate at the right place. If the library is in another directory that is not in the library search path, you can add it with -L option to ld, like ld -verbose -L<the directory> -lmylib.

As for the double slash you see, it's not a problem, as more than more slashes are interpreted as one slash on Linux, that is to say, //foo/bar is the same as /foo/bar`, so don't need to worry about it.

As for the difference of the search path, on Fedora, the default search path is:

SEARCH_DIR("/usr/x86_64-redhat-linux/lib64"); SEARCH_DIR("/usr/lib64"); SEARCH_DIR("/usr/local/lib64"); SEARCH_DIR("/lib64"); SEARCH_DIR("/usr/x86_64-redhat-linux/lib"); SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib"); SEARCH_DIR("/usr/lib");

While on Ubuntu, it is:

SEARCH_DIR("/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");

So Ubuntu start each path with prefix '=', now let's see what ld does for this:

If searchdir begins with "=", then the "=" will be replaced by the sysroot prefix, controlled by the --sysroot option, or specified when the linker is configured.

That means = will be replaced by system root, which most like is / for a Linux system. That's why you see the double slashes on Ubuntu not on Fedora.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • Thanks. There was a typo in my original post; I edited the output to make it clear that I wasn't looking for 'libibmylib', which is completely incoherent. I forgot to edit the command as well. – Citronen Apr 27 '16 at 02:58
  • I forgot to mention the OS I'm using. The double slash exists in the paths in Ubuntu 14.04, but not in Fedora 21. Why might this be the case? – Citronen Apr 27 '16 at 02:59