2 qq 30069409 qq_30069409 于 2017.09.18 14:31 提问

linux下的动态库问题。。。
 [wangpeng@YunChanPinXian day06]$ ls
io  io.cpp  io.o  makefile
[wangpeng@YunChanPinXian day06]$ ldd io
    linux-vdso.so.1 =>  (0x00007fff9193f000)
    libboost_system.so.1.53.0 => not found
    libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f5fb9464000)
    libm.so.6 => /lib64/libm.so.6 (0x00000036edc00000)
    libgcc_s.so.1 => /usr/local/lib64/libgcc_s.so.1 (0x00007f5fb924f000)
    libc.so.6 => /lib64/libc.so.6 (0x00000036ecc00000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00000036ed000000)
    /lib64/ld-linux-x86-64.so.2 (0x00000036ec800000)
[wangpeng@YunChanPinXian day06]$ 

makefile中指定路径

 CC = g++

CFLAGS = -Wall -g

LIBS = -std=c++11 -lboost_system -I/home/wangpeng/mjf/lib/include -L/home/wangpeng/mjf/lib/lib
#-L/home/wangpeng/wangpengworked/work_space/lib/lib -levent

SRCS = io.cpp

OBJS = $(SRCS:.cpp=.o)

BIN = io

$(BIN):$(OBJS)

        $(CC) $(CFLAGS) $^ -o $@ $(LIBS)

%.o:%.cpp

        $(CC) $(CFLAGS) -c $< -o $@ $(LIBS)

动态库
[wangpeng@YunChanPinXian lib]$ pwd
/home/wangpeng/mjf/lib/lib
[wangpeng@YunChanPinXian lib]$ ls
libboost_atomic.a libboost_filesystem.so libboost_math_c99f.so.1.53.0 libboost_math_tr1.so.1.53.0 libboost_regex.so libboost_thread.so.1.53.0
libboost_atomic.so libboost_filesystem.so.1.53.0 libboost_math_c99l.a libboost_prg_exec_monitor.a libboost_regex.so.1.53.0 libboost_timer.a
libboost_atomic.so.1.53.0 libboost_graph.a libboost_math_c99l.so libboost_prg_exec_monitor.so libboost_serialization.a libboost_timer.so
libboost_chrono.a libboost_graph.so libboost_math_c99l.so.1.53.0 libboost_prg_exec_monitor.so.1.53.0 libboost_serialization.so libboost_timer.so.1.53.0
libboost_chrono.so libboost_graph.so.1.53.0 libboost_math_c99.so libboost_program_options.a libboost_serialization.so.1.53.0 libboost_unit_test_framework.a
libboost_chrono.so.1.53.0 libboost_iostreams.a libboost_math_c99.so.1.53.0 libboost_program_options.so libboost_signals.a libboost_unit_test_framework.so
libboost_context.a libboost_iostreams.so libboost_math_tr1.a libboost_program_options.so.1.53.0 libboost_signals.so libboost_unit_test_framework.so.1.53.0
libboost_context.so libboost_iostreams.so.1.53.0 libboost_math_tr1f.a libboost_python.a libboost_signals.so.1.53.0 libboost_wave.a
libboost_context.so.1.53.0 libboost_locale.a libboost_math_tr1f.so libboost_python.so libboost_system.a libboost_wave.so
libboost_date_time.a libboost_locale.so libboost_math_tr1f.so.1.53.0 libboost_python.so.1.53.0 libboost_system.so libboost_wave.so.1.53.0
libboost_date_time.so libboost_locale.so.1.53.0 libboost_math_tr1l.a libboost_random.a libboost_system.so.1.53.0 libboost_wserialization.a
libboost_date_time.so.1.53.0 libboost_math_c99.a libboost_math_tr1l.so libboost_random.so libboost_test_exec_monitor.a libboost_wserialization.so
libboost_exception.a libboost_math_c99f.a libboost_math_tr1l.so.1.53.0 libboost_random.so.1.53.0 libboost_thread.a libboost_wserialization.so.1.53.0
libboost_filesystem.a libboost_math_c99f.so libboost_math_tr1.so libboost_regex.a libboost_thread.so
[wangpeng@YunChanPinXian lib]$

1个回答

lsyyff23
lsyyff23   2017.11.04 02:12

首先,ldd是查看可执行文件的依赖库,编译时设置的依赖库路径,在运行时不一定能找到;
可以使用: -Wl,-rpath, 用于指定程序运行时查找动态链接库的路径,多个路径是使用冒号隔开;
eg:
xxx$ g++ -Wl,-rpath,/usr/local/lib/ -oevh libevent_http.cpp -levent

注意, -Wl,-rpath,xxx 之间无空格
编译完成后可以使用以下命令查看路径是否设置成功了
xxx $ readelf -dl evh

看到类似下面的信息则是路径设置成功了

Csdn user default icon
上传中...
上传图片
插入图片