1

I want to make g++ linker(ld) search for libraries starting in the directory /usr/local/lib. How do I permanently add /usr/local/lib to the search path for the linker?

The problem is that I am trying to link the library libboost_program_options.a to my source. There are two copies of the library, one in /usr/lib/x86_64-linux-gnu/ and the other in /usr/local/lib. How do I make the linker first look in /usr/local/lib and pick that instead of the other one?

The output of ldconfig -v 2>/dev/null | grep -v ^$'\t' on my system:

/usr/lib/x86_64-linux-gnu/libfakeroot: /lib/i386-linux-gnu: /usr/lib/i386-linux-gnu: /usr/local/lib: /lib/x86_64-linux-gnu: /usr/lib/x86_64-linux-gnu: /usr/lib/x86_64-linux-gnu/mesa-egl: /usr/lib/nvidia-304: /usr/lib32/nvidia-304: /lib32: /usr/lib32: /lib: /usr/lib: /usr/lib/nvidia-304/tls: (hwcap: 0x8000000000000000) /usr/lib32/nvidia-304/tls: (hwcap: 0x8000000000000000) 

The output of ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012

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/x86_64-linux-gnu/lib") SEARCH_DIR("=/usr/local/lib") SEARCH_DIR("=/lib") SEARCH_DIR("=/usr/lib") 
2
  • 1
    unix.stackexchange.com/questions/67781/… Commented Oct 5, 2016 at 12:35
  • @SimonKraemer I want the change to be permanent, and there is already a libc.conf file in /etc/ld.so.conf.d containing the required line. I want to alter the search order. ld should start looking from /usr/local/lib Commented Oct 5, 2016 at 12:37

1 Answer 1

1

The search path is specified in the linker scripts used during compilation. Run gcc -v foo.c to perform a link and see which linker script is used. In my case, it is /usr/lib/ldscripts/elf_x86_64.x. In that linker script, you will find SEARCH_DIR directives. Update it to include /usr/local/lib.

Note that ldconfig and ld.so.conf.d are only used at runtime.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.