注册公司费用跟后期费用,宁波seo推广外包公司,学做网站论坛,产品ui设计是什么cmake find_package路径详解
转自#xff1a;https://zhuanlan.zhihu.com/p/50829542
经常在Linux下面写C程序#xff0c;尤其是需要集成各种第三方库的工程#xff0c;肯定对find_package指令不陌生。
这是条很强大的指令。可以直接帮我们解决整个工程的依赖问题#x…cmake find_package路径详解
转自https://zhuanlan.zhihu.com/p/50829542
经常在Linux下面写C程序尤其是需要集成各种第三方库的工程肯定对find_package指令不陌生。
这是条很强大的指令。可以直接帮我们解决整个工程的依赖问题自动把头文件和动态链接文件配置好。比如说在Linux下面工程依赖了OpenCV只需要下面几行就可以完全配置好
add_executable(my_bin src/my_bin.cpp)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(my_bin, ${OpenCV_LIBS})工作流程如下
find_package在一些目录中查找OpenCV的配置文件。找到后find_package会将头文件目录设置到${OpenCV_INCLUDE_DIRS}中将链接库设置到${OpenCV_LIBS}中。设置可执行文件的链接库和头文件目录编译文件。
到现在为止出现了第一个问题。那就是 find_package会在哪些目录下面寻找OpenCV的配置文件
find_package目录
为什么我们要知道这个问题呢因为很多库我们都是自己编译安装的。比如说电脑中同时编译了OpenCV2和OpenCV3我该如何让cmake知道到底找哪个呢
其实这个问题在CMake官方文档中有非常详细的解答。
首先是查找路径的根目录。我把几个重要的默认查找目录总结如下
package_DIR
CMAKE_PREFIX_PATH
CMAKE_FRAMEWORK_PATH
CMAKE_APPBUNDLE_PATH
PATH其中PATH中的路径如果以bin或sbin结尾则自动回退到上一级目录。 找到根目录后cmake会检查这些目录下的
prefix/(lib/arch|lib|share)/cmake/name*/ (U)
prefix/(lib/arch|lib|share)/name*/ (U)
prefix/(lib/arch|lib|share)/name*/(cmake|CMake)/ (U)cmake找到这些目录后会开始依次找packageConfig.cmake或Findpackage.cmake文件。找到后即可执行该文件并生成相关链接信息。
现在回过头来看查找路径的根目录。我认为最重要的一个是PATH。由于/usr/bin/在PATH中cmake会自动去/usr/(lib/arch|lib|share)/cmake/name*/寻找模块这使得绝大部分我们直接通过apt-get安装的库可以被找到。
另外一个比较重要的是package_DIR。我们可以在调用cmake时将这个目录传给cmake。由于其优先级最高因此cmake会优先从该目录中寻找这样我们就可以随心所欲的配置cmake使其找到我们希望它要找到的包。而且除上述指定路径外cmake还会直接进入package_DIR下寻找。如我在3rd_parties目录下编译了一个OpenCV那么执行cmake时可以使用
OpenCV_DIR../../3rd-party/opencv-3.3.4/build/ cmake .. 这样做以后cmake会优先从该目录寻找OpenCV。
配置好编译好了以后我感兴趣的是另一个问题 我现在编译出了可执行文件并且这个可执行文件依赖于opencv里的动态库。这个动态库是在cmake时显式给出的。那么
该执行文件在运行时是如何找到这个动态库的如果我把可执行文件移动了如何让这个可执行文件依然能找到动态库如果我把该动态库位置移动了如何让这个可执行文件依然能找到动态库如果我把可执行文件复制到别的电脑上使用我该把其链接的动态库放到新电脑的什么位置
可执行文件如何寻找动态库
在ld的官方文档中对这个问题有详尽的描述。 The linker uses the following search paths to locate required shared libraries: Any directories specified by -rpath-link options. Any directories specified by -rpath options. The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers which have been configured with the --with-sysroot option. On an ELF system, for native linkers, if the -rpath and -rpath-link options were not used, search the contents of the environment variable “LD_RUN_PATH”. On SunOS, if the -rpath option was not used, search any directories specified using -L options. For a native linker, the search the contents of the environment variable “LD_LIBRARY_PATH”. For a native ELF linker, the directories in “DT_RUNPATH” or “DT_RPATH” of a shared library are searched for shared libraries needed by it. The “DT_RPATH” entries are ignored if “DT_RUNPATH” entries exist. The default directories, normally /lib and /usr/lib. For a native linker on an ELF system, if the file /etc/ld.so.conf exists, the list of directories found in that file. If the required shared library is not found, the linker will issue a warning and continue with the link. 最重要的是第一条即rpath。这个rpath会在编译时将动态库绝对路径或者相对路径取决于该动态库的cmake写到可执行文件中。chrpath工具可以查看这些路径。 chrpath extract_gpu
extract_gpu: RPATH/usr/local/cuda/lib64:/home/dechao_meng/data/github/temporal-segment-networks/3rd-party/opencv-3.4.4/build/lib可以看到OpenCV的动态库的绝对路径被写到了可执行文件中。因此即使可执行文件的位置发生移动依然可以准确找到编译时的rpath。
接下来的问题如果我把可执行文件复制到了别人的电脑上或者我的动态库文件的目录发生了改变怎样让可执行文件继续找到这个动态库呢其实是在第五条LD_LIBRARY_PATH。只要将存储动态库的目录加入到LD_LIBRARY_PATH中可执行文件就能正确找到该目录。
这种做法十分常见比如我们在安装CUDA时最后一步是在.bashrc中配置
export LD_LIBRARY_PATH/usr/local/cuda/lib64:$LD_LIBRARY_PATH这样做之后依赖cuda的可执行文件就能够正常运行了。
总结
写这篇文章是因为从我第一次使用cmake以来经常因为动态链接的问题而耽误很长时间。清楚理解find_package的运行机制在Linux的C开发中是非常重要的而相关的资料网上又比较稀少。其实官网上解释的非常清楚不过之前一直没有认真查。做事情还是应该一步一个脚印将原理搞清楚再放心使用。
Reference
https://cmake.org/cmake/help/v3.0/command/find_package.htmlhttps://unix.stackexchange.com/questions/22926/where-do-executables-look-for-shared-objects-at-runtimehttps://codeyarns.com/2017/11/02/how-to-change-rpath-or-runpath-of-executable/