当前位置:网站首页>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
边栏推荐
- Apifox 生成接口文档 教程与操作步骤
- 最基础01/完全背包
- Matlab基础(4)——矩阵
- Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
- 横向对比5种常用的注册中心,无论是用于面试还是技术选型,都非常有帮助
- 【CVA估值训练营】如何快速读懂上市公司年报——第五讲
- Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
- The method of judging the same variable without the if branch
- Summary of text alignment, line height, space, etc.
- 崩了,该来的终究躲不掉
猜你喜欢

EXCEL解决问题:如何查找目标区域,是否包含指定字符串?

Reverse linked list - recursive inversion method

历时两月,终拿字节跳动offer,算法面试题分享「带答案」

Vivado安装后添加器件库

Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing

维护数千规模MySQL实例,数据库灾备体系构建指南

unity初学6——简易的UI制作(血条制作)和音频加入以及NPC的对话气泡(2d)

【32. 图中的层次(图的广度优先遍历)】

nodeJs--fs模块

Rust 从入门到精通02-安装
随机推荐
Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
数据湖(十八):Flink与Iceberg整合SQL API操作
2022-07-29 顾宇佳 学习笔记 异常处理
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
MySQL查询性能优化
Microsoft SQL server hacked, bandwidth stolen
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
Verilog grammar basics HDL Bits training 08
Digital input and output module DAM-5088
unity对象池(学习)
如何用Golang来手撸一个Blog - Milu.blog 开发总结
What happened when the computer crashed?
数字量输入输出模块DAM-5088
PanGu-Coder: Function-level code generation model
PyQt5快速开发与实战 8.2 绘图 && 8.3 QSS的UI美化
Apifox 生成接口文档 教程与操作步骤
看了这些6G原型样机,我想一觉睡到2030年
IO/multiplexing (select/poll/epoll)
Win11打不开exe应用程序怎么办?Win11无法打开exe程序解决方法
AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...