微知-动态链接库导出的三种方式?(LD_LIBRARY_PATH, /etc/ld.so.conf, -Wl,-rpath)

背景

经常需要导出动态库,最场景的方式是指定LD_LIBRARY_PATH。本文介绍3中

LD_LIBRARY_PATH

这种方式临时生效

export LD_LIBRARY_PATH=/path/to/mylibdir:$LD_LIBRARY_PATH

使用ldconfig和/etc/ld.so.conf

在配置文件 /etc/ld.so.conf 中指定动态库搜索路径。每次编辑完该文件后,都必须运行命令 ldconfig 使修改后的配置生效 。
以管理员权限打开/etc/ld.so.conf文件在其中添加,或者:
在/etc/ld.so.conf.d/目录下创建一个新的.conf文件,文件内容是动态库的路径
然后使用sudo ldconfig,使其生效
然后使用sudo ldconfig -v 查看是否包含需要的库
比如查看json库:
在这里插入图片描述

比如doca的配置:
在这里插入图片描述

通过gcc参数指定运行时动态库搜索路径

gcc -o test test.c -L. -ltest -Wl,-rpath=.:..:libdir

gcc:GCC编译器的命令。
-o test:指定输出的可执行文件名为test。
main.c:要编译的源代码文件。
-L.:告诉编译器在当前目录(.)中查找库文件。
-ltest:告诉编译器链接名为libtest.so(或libtest.a,取决于是动态库还是静态库)的库。
-Wl,-rpath=.:…:lib:这是一个传递给链接器(ld)的参数,-Wl告诉GCC这个参数是给链接器的。-rpath指定了运行时动态库搜索路径,.表示当前目录,…表示上一级目录,libdir表示名为libdir的目录。这些路径将被用于在程序运行时搜索动态链接库。使用:来指定多个搜索路径

/usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `av_free@LIBAVUTIL_55' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `av_packet_alloc@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_send_packet@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_close@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `av_frame_free@LIBAVUTIL_55' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `av_frame_alloc@LIBAVUTIL_55' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_open2@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_register_all@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_receive_frame@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_find_decoder@LIBAVCODEC_57' /usr/bin/ld: /usr/local/lib/dji/bag/libdecoder_hdr_soft.so: undefined reference to `avcodec_alloc_context3@LIBAVCODEC_57' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/parser_H265.dir/build.make:169: parser_H265] Error 1 make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/parser_H265.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... [100%] Linking CXX executable obtain_dbag_data [100%] Built target obtain_dbag_data make: *** [Makefile:91: all] Error 2 解决一下这个问题
最新发布
09-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值