c++ - How to specify preference of library path? -
i'm compiling c++ program using g++
, ld
. have .so
library want used during linking. however, library of same name exists in /usr/local/lib
, , ld
choosing library on 1 i'm directly specifying. how can fix this?
for examples below, library file /my/dir/libfoo.so.0
. things i've tried don't work:
- my g++ command
g++ -g -wall -o my_binary -l/my/dir -lfoo bar.cpp
- adding
/my/dir
beginning or end of$path
en` variable - adding
/my/dir/libfoo.so.0
argument g++
add path new library ld_library_path
(it has different name on mac ...)
your solution should work using -l/my/dir -lfoo
options, @ runtime use ld_library_path point location of library.
or
use rpath option via gcc linker - runtime library search path, used instead of looking in standard dir (gcc option):
-wl,-rpath,$(default_lib_install_path)
this temporary solution. linker first searches ld_library_path libraries before looking standard directories.
if don't want permanently update ld_library_path can on fly on command line:
ld_library_path=/some/custom/dir ./fooo
you can check libraries linker knows using (example):
/sbin/ldconfig -p | grep libpthread libpthread.so.0 (libc6, os abi: linux 2.6.4) => /lib/libpthread.so.0
and can check library application using:
ldd foo linux-gate.so.1 => (0xffffe000) libpthread.so.0 => /lib/libpthread.so.0 (0xb7f9e000) libxml2.so.2 => /usr/lib/libxml2.so.2 (0xb7e6e000) librt.so.1 => /lib/librt.so.1 (0xb7e65000) libm.so.6 => /lib/libm.so.6 (0xb7d5b000) libc.so.6 => /lib/libc.so.6 (0xb7c2e000) /lib/ld-linux.so.2 (0xb7fc7000) libdl.so.2 => /lib/libdl.so.2 (0xb7c2a000) libz.so.1 => /lib/libz.so.1 (0xb7c18000)
Comments
Post a Comment