欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言中find_package()的搜索路径的实现

程序员文章站 2022-03-07 13:44:18
目录find_package()module模式config模式find_package(opencv required) 如何设置查找路径/指定只用版本find_package()内部用find_p...

find_package()

内部用find_path()和find_library()实现的找头文件路径和动态共享库文件

首先明确,find_package()肯定需要个.cmake文件,不然它根本就不知道package的名字具体是啥(大写?小写?),去哪找???

module模式

(1)是否有cmake_module_path,有的话,优先在这里找find<packagename>.cmake文件,根据这个文件来找; (最容易自己操作)

set(cmake_module_path ${project_source_dir}/cmake_module)

(2)没有cmake_module_path,在cmake modules里找(/usr/share/cmake-3.10-modules)

config模式

要找<packagename>config.cmake或<lower-case-package-name>-config.cmake文件,查找顺序:
(1)<packagename>_root变量路径,cmake3.12新增
(2)<package_name>_dir变量路径,有了就不用上面那个了(可以用这个,定义这个变量)

set(opencv_root "/usr/lib/opencv_249/build") #可在子目录查找
#为了在这里查找.cmake文件
set(opencv_dir "/usr/lib/opencv_300/build")  #弱智,不能在子目录查找

find_package(opencv required
          no_module        # tells cmake to use config mode
          no_default_path) # and don't look other anywhere
 #此时用的在${opencv}查找的.cmake文件

如果没有.cmake文件而且自己不想写.cmake文件,那就不用find_package,简单粗暴:

#find_package就是这么实现的
#头文件目录(是个目录的绝对路径)
find_path(<name_include_dirs> names xx.h paths /usr/include /usr\local/include/ ...)
#库文件(是个文件的绝对路径)
find_library(<name_libraries> names lib-name paths /usr/lib /usr/local/lib ...)

cmake_include_path:find_path默认会去这里找,当然指定写到paths后面更好
cmake_library_path:find_library默认会去这里找,当然指定写到paths后面更好

set(cmake_include_path /usr/local/include/opencv
                    /usr/include)
 
find_path(cv_include_dirs names cv.h paths cmake_include_path)

find_package(opencv required) 如何设置查找路径/指定只用版本

#find opencv lib
find_package(opencv required
              no_module # should be optional, tells cmake to use config mode
              paths /usr/local # look here
              no_default_path) # and don't look anywhere else

到此这篇关于c语言中find_package()的搜索路径的实现的文章就介绍到这了,更多相关c语言 find_package()搜索路径内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!