cmake:使用find_package查找libjpeg的静态库

本文介绍如何在CMake中优先查找并使用libjpeg的静态库,通过设置JPEG_NAMES变量,实现对FindJPEG.cmake脚本的定制,确保项目能够正确链接所需的静态库资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道简单的使用命令find_package可以查找指定的package.

对于经常会用到的 jpeg库turbojpeg,可以用find_package(JPEG REQUIRED)来查找。

find_package(JPEG REQUIRED)
if(JPEG_FOUND)
	message(STATUS "JPEG_INCLUDE_DIR=${JPEG_INCLUDE_DIR}")
	message(STATUS "JPEG_LIBRARIES=${JPEG_LIBRARIES}")	
endif()

但我们知道cmake默认查找到的是动态库,所以如果想查找libjpeg的静态库,这样是不行的。
其实FindJPEG.cmake脚本在设计时已经考虑到了这个问题,如下是FindJPEG.cmake脚本的代码片段,可以看到调用者可以通过JPEG_NAMES这个变量指定优先查找到的库名字。

# FindJPEG.cmake
find_path(JPEG_INCLUDE_DIR jpeglib.h)

set(JPEG_NAMES ${JPEG_NAMES} jpeg libjpeg)
find_library(JPEG_LIBRARY NAMES ${JPEG_NAMES} )
.....

所以如果想要指定优先使用静态库,前面的cmake脚本可以稍改一下,如下,就是在调用find_package之前设置JPEG_NAMES为想要的静态库名:

# 查找libjpeg库,优先使用静态库
if(MSVC)
	set( JPEG_NAMES jpeg-static )
elseif(CMAKE_COMPILER_IS_GNUCXX)
	set( JPEG_NAMES libjpeg.a)
endif()
find_package(JPEG REQUIRED)
if(JPEG_FOUND)
	message(STATUS "JPEG_INCLUDE_DIR=${JPEG_INCLUDE_DIR}")
	message(STATUS "JPEG_LIBRARIES=${JPEG_LIBRARIES}")	
endif()

参考资料

https://cmake.org/cmake/help/v3.11/module/FindJPEG.html
https://cmake.org/cmake/help/v3.11/command/find_library.html

-- Try 1 failed CMake Warning at cmake/OpenCVDownload.cmake:248 (message): IPPICV: Download failed: 28;"Timeout was reached" For details please refer to the download log file: /home/charon/Opencv/opencv-4.8.0/build/CMakeDownloadLog.txt Call Stack (most recent call first): 3rdparty/ippicv/ippicv.cmake:37 (ocv_download) cmake/OpenCVFindIPP.cmake:259 (download_ippicv) cmake/OpenCVFindLibsPerf.cmake:12 (include) CMakeLists.txt:756 (include) -- Could NOT find CUDNN: Found unsuitable version "..", but required is at least "7.5" (found /usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.0) -- CUDA detected: 12.5 -- CUDA: Using CUDA_ARCH_BIN=8.9 -- CUDA NVCC target flags: -gencode;arch=compute_89,code=sm_89;-D_FORCE_INLINES -- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off -- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off -- Could NOT find Atlas (missing: Atlas_CLAPACK_INCLUDE_DIR) -- Could NOT find Java (missing: Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE Java_JAVADOC_EXECUTABLE) (found version "11.0.26") -- Could NOT find JNI (missing: JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) -- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file CMake Error at cmake/OpenCVModule.cmake:274 (message): No modules has been found: /home/ub/opencv-4.8.0/opencv_contrib_4.8.0/modules Call Stack (most recent call first): cmake/OpenCVModule.cmake:356 (_glob_locations) cmake/OpenCVModule.cmake:408 (ocv_glob_modules) CMakeLists.txt:967 (ocv_register_modules) -- Checking for module 'gtk+-2.0' -- No package 'gtk+-2.0' found -- Checking for module 'libavresample' -- No package 'libavresample' found CMake Error at modules/core/CMakeLists.txt:54 (message): CUDA: OpenCV requires enabled 'cudev' module from 'opencv_contrib' repository: https://github.com/opencv/opencv_contrib -- Configuring incomplete, errors occurred! See also "/home/charon/Opencv/opencv-4.8
04-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值