当前位置:网站首页>CMake library search function does not search LD_LIBRARY_PATH
CMake library search function does not search LD_LIBRARY_PATH
2022-07-30 12:33:00 【51CTO】
摘要: This article is introduced by the problem that the library file cannot be found after compiling and running,首先分析了find_package(JNI)的工作流程,and then forcmake不搜索LD_LIBRARY_PATH的问题,A general solution is proposed.
本文分享自华为云社区《 CMakeThe library search function doesn't actually searchLD_LIBRARY_PATH? Thinking triggered by the experience of using the compilation tool》,作者: 蜉蝣与海 .
Product to be used recentlyJNI技术,CMake编译C++The code requires external linkslibjvm.so库.The code compiles fine,系统中也有libjvm.so, However, the following exception was reported when using it:
This error message indicates,The operating system was not foundlibjvm.so, Our operating system is from LD_LIBRARY_PATHSearch for these dynamic link libraries,Obviously currentlylibjvm.sonot in this directory.
The solution to the problem is simple,直接在LD_LIBRARY_PATH里加入libjvm.so的库即可.But this got me thinking:
Why can be found when buildinglibjvm.so, It can't be found at runtime?
这个问题的回答,Both can have a concise and concise version of the explanation,You can also dig deeper.
Let’s look at the brief explanation first:
代码的CMakeListThe following statements are used in ,Look for and link during compilationlibjvm.so,This search method is different from the search method of the operating system:
其中find_package(JNI)会搜索libjvm.so可能存在的路径,通过get_filename_component来获得libjvm.so的文件夹,And set this folder as the default search library path.而后set_target_propertiesLinking will work.
The answer can only tell us“是什么”,But as a programmer,还要了解“为什么”,Several issues are raised here:
- find_package(JNI)的工作过程是怎样的?为什么LD_LIBRARY_PATHDependency library not found,cmake可以找到
- cmakelibrary search functionfind_library会搜索LD_LIBRARY_PATH吗,如果不会,You can search by settingLD_LIBRARY_PATH吗?
问题一:find_package(JNI)的工作过程是怎样的
In order to facilitate developers to reference external packages,cmakeThere are many official pre-defined packages to look for dependenciesModule, 他们存储在cmake的/share/-cmake-<version>/Modules目录下.每个以Find<LibraryName>.cmake命名的文件都可以帮我们找到一个包[1].Execute the following instructions on your local computer,即可找到find_package(JNI)使用的脚本文件.
打开自己的cmake对应的FindJNI文件,You can see dense comments and scripts,by reading these scripts,We got to knowFindJNI是如何工作的.
分析问题前,Look at the results of the problem first,The comments at the top of the file have the following description:
这段代码表明,执行find_package(JNI)之后,A series of variables will be set,This includes representationJNIWhether the variable was foundJNI_FOUND,以及表示libjvm.so的变量JAVA_JVM_LIBRARY.After these variables are set,通过FindPackageHandleStandardArgs导出,返回调用处,FindPackageHandleStandardArgs是cmakeA macro dedicated to exporting variables[2]:
在文件中定位JAVA_JVM_LIBRARY, The following code snippet can be traced:
由此可知,JAVA_JVM_LIBRARY这个变量,by searching one by one${_JNI_${search}_JVM}Folder in and then confirmJAVA_JVM_LIBRARY的.而${_JNI_${search}_JVM}The relevant definition sentences are shown in the figure:
其中JAVA_JVM_LIBRARY_DIRECTORIESThere are a lot of possibilities involvedlibjvm.so存在的路径.
如上图所示,The variable dependency order is as follows:
JAVA_JVM_LIBRARY_DIRECTORIES => JAVA_AWT_LIBRARY_DIRECTORIES => _JNI_JAVA_AWT_LIBRARY_TRIES & _JAVA_HOME => _JNI_JAVA_DIRECTORIES_BASE
最终发现JAVA_JVM_LIBRARY_DIRECTORIES变量的值,是由JAVA_HOME变量的值和_JNI_JAVA_DIRECTORIES_BASEThe values of the variables are jointly determined.而JNI_JAVA_DIRECTORY_BASEA large number of predefined paths are preset:
通过以上分析可以看出,JAVA_JVM_LIBRARY的搜索,依赖JAVA_HOMEand lots of predefined paths.
问题二:cmakeLibrary search functionfind_library会搜索LD_LIBRARY_PATH吗
通过阅读Does CMake's find_library search LD_LIBRARY_PATH可以知道,find_libraryThe default is not to searchLD_LIBRARY_PATH, And can't find it online eithercmake搜索LD_LIBRARY_PATH的文章.
那cmake能搜索LD_LIBRARY_PATH吗?
答案是可以的,通过cmake获取LD_LIBRARY_PATH环境变量,并转为cmake可理解的list格式,Then injectfind_library即可,代码如下:
Compilation fails if the desired library cannot be found,可以将WARNING改为fatal_error, 代码如下:
小结
This article is introduced by the problem that the library file cannot be found after compiling and running,首先分析了find_package(JNI)的工作流程,and then forcmake不搜索LD_LIBRARY_PATH的问题,A general solution is proposed.
参考文献:
[1] Cmake之深入理解find_package()的用法:https://zhuanlan.zhihu.com/p/97369704?utm_source=wechat_session
[2] Cmake中find_packageThe module mode of the search mode of the command(Module mode):https://www.jianshu.com/p/f983a90bcf91
[3]Does CMake's find_library search LD_LIBRARY_PATH?:https://stackoverflow.com/questions/41566316/does-cmakes-find-library-search-ld-library-path
边栏推荐
- ADC0808/9 signal acquisition developed by single chip microcomputer
- C# 枚举类型 于xaml 中区别
- Kubernetes 入门实战03 中级篇
- Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
- Matlab基础(0)——命令行常用指令
- 为什么说Prometheus是足以取代Zabbix的监控神器?
- 维护数千规模MySQL实例,数据库灾备体系构建指南
- 【ASP.NET Core】选项类的依赖注入
- 亚洲高校首现KDD博士论文奖:清华裘捷中获Runner Up奖,WINNER奖也是位华人
- 什么是驱动程序签名,驱动程序如何获取数字签名?
猜你喜欢

nodeJs--fs模块

11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?

2022-07-29 Gu Yujia Study Notes Exception Handling

Rust 从入门到精通02-安装

Homework 7.29 correlation function directory and file attributes related functions

爱可可AI前沿推介(7.30)

微信视频号视频如何下载提取?视频号直播回放如何下载?方法很简单!

Verilog grammar basics HDL Bits training 08

Vivado安装后添加器件库

AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...
随机推荐
saltstack学习3模块
概率论的学习整理3: 概率的相关概念
Flexible distribution parameters of mechanical system modeling and control of research and development
干货分享:小技巧大用处之Bean管理类工厂多种实现方式
程序环境和预处理(详解)
Rust 从入门到精通02-安装
单片机工程师笔试题目归纳汇总
【32. 图中的层次(图的广度优先遍历)】
小心 transmittable-thread-local 的这个坑
Difference between C# enumeration type and xaml
Program environment and preprocessing (detailed)
ECCV 2022 | 新加坡国立大学提出:全新可恢复型模型遗忘框架LIRF!
24. 两两交换链表中的节点
横向对比5种常用的注册中心,无论是用于面试还是技术选型,都非常有帮助
[SCTF2019]Flag Shop
解码Redis最易被忽视的CPU和内存占用高问题
Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.
PyQt5快速开发与实战 8.4 设置窗口背景 && 8.5 不规则窗口的显示
ADC0808/9 signal acquisition developed by single chip microcomputer