时间:2022-12-22 09:55:39 | 栏目:C代码 | 点击:次
内部用find_path()和find_library()实现的找头文件路径和动态共享库文件
首先明确,find_package()肯定需要个.cmake文件,不然它根本就不知道package的名字具体是啥(大写?小写?),去哪找???
(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)
要找<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 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