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

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

我们知道简单的使用命令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

tao@tao:~/SLAM/ORB_SLAM3/build$ cmake .. -DCMAKE_BUILD_TYPE=Release -DOPENCV_VERSION=3.4 2>&1 | tee cmake.log CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 3.10 will be removed from a future version of CMake. Update the VERSION argument <min> value. Or, use the <min>...<max> syntax to tell CMake that the project requires at least <min> but has been updated to work with policies introduced by <max> or earlier. -- The C compiler identification is GNU 7.5.0 -- The CXX compiler identification is GNU 9.4.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done Build type: Release -- Performing Test COMPILER_SUPPORTS_CXX11 -- Performing Test COMPILER_SUPPORTS_CXX11 - Success -- Performing Test COMPILER_SUPPORTS_CXX0X -- Performing Test COMPILER_SUPPORTS_CXX0X - Success -- Using flag -std=c++11. -- Found OpenCV: /usr/local (found suitable version "4.2.0", minimum required is "4.2") OPENCV VERSION: 4.2.0 CMake Deprecation Warning at /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:40 (cmake_policy): Compatibility with CMake < 3.10 will be removed from a future version of CMake. Update the VERSION argument <min> value. Or, use the <min>...<max> syntax to tell CMake that the project requires at least <min> but has been updated to work with policies introduced by <max> or earlier. Call Stack (most recent call first): CMakeLists.txt:41 (find_package) CMake Warning (dev) at /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:152 (find_package): Policy CMP0144 is not set: find_package uses upper-case <PACKAGENAME>_ROOT variables. Run "cmake --help-policy CMP0144" for policy details. Use the cmake_policy command to set the policy and suppress this warning. CMake variable EIGEN_ROOT is set to: /usr/include/eigen3 For compatibility, find_package is ignoring the variable, but code in a .cmake module might still use it. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:316 (find_eigen) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:551 (find_external_library) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Checking for module 'eigen3' -- Found eigen3, version 3.3.7 -- Found Eigen: /usr/include/eigen3 (Required is at least version "3.1") -- Eigen found (include: /usr/include/eigen3, version: 3.3.7) CMake Warning (dev) at /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:134 (find_package): Policy CMP0167 is not set: The FindBoost module is removed. Run "cmake --help-policy CMP0167" for policy details. Use the cmake_policy command to set the policy and suppress this warning. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:314 (find_boost) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:551 (find_external_library) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found Boost: /usr/include (found suitable version "1.71.0", minimum required is "1.55.0") found components: system filesystem date_time iostreams regex -- Checking for module 'libusb-1.0' -- Found libusb-1.0, version 1.0.23 CMake Warning (dev) at /usr/local/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message): The package name passed to `find_package_handle_standard_args` (USB_10) does not match the name of the calling package (OpenNI). This can lead to problems in calling code that expects `find_package` result variables (e.g., `_FOUND`) to follow a certain pattern. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/Modules/FindOpenNI.cmake:28 (find_package_handle_standard_args) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:179 (find_package) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:322 (find_openni) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:548 (find_external_library) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found USB_10: /usr/lib/x86_64-linux-gnu/libusb-1.0.so -- Found OpenNI: /usr/lib/libOpenNI.so -- OpenNI found (include: /usr/include/ni, lib: /usr/lib/libOpenNI.so) CMake Warning (dev) at /usr/local/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message): The package name passed to `find_package_handle_standard_args` (USB_10) does not match the name of the calling package (OpenNI2). This can lead to problems in calling code that expects `find_package` result variables (e.g., `_FOUND`) to follow a certain pattern. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/Modules/FindOpenNI2.cmake:28 (find_package_handle_standard_args) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:193 (find_package) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:324 (find_openni2) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:548 (find_external_library) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found OpenNI2: /usr/lib/libOpenNI2.so -- OpenNI2 found (include: /usr/include/openni2, lib: /usr/lib/libOpenNI2.so) ** WARNING ** io features related to pcap will be disabled ** WARNING ** io features related to png will be disabled -- The imported target "vtkParseOGLExt" references the file "/usr/bin/vtkParseOGLExt-7.1" but this file does not exist. Possible reasons include: * The file was deleted, renamed, or moved to another location. * An install or uninstall procedure did not complete successfully. * The installation package was faulty and contained "/usr/lib/cmake/vtk-7.1/VTKTargets.cmake" but not all the files it references. -- The imported target "vtkRenderingPythonTkWidgets" references the file "/usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so" but this file does not exist. Possible reasons include: * The file was deleted, renamed, or moved to another location. * An install or uninstall procedure did not complete successfully. * The installation package was faulty and contained "/usr/lib/cmake/vtk-7.1/VTKTargets.cmake" but not all the files it references. -- The imported target "vtk" references the file "/usr/bin/vtk" but this file does not exist. Possible reasons include: * The file was deleted, renamed, or moved to another location. * An install or uninstall procedure did not complete successfully. * The installation package was faulty and contained "/usr/lib/cmake/vtk-7.1/VTKTargets.cmake" but not all the files it references. -- The imported target "pvtk" references the file "/usr/bin/pvtk" but this file does not exist. Possible reasons include: * The file was deleted, renamed, or moved to another location. * An install or uninstall procedure did not complete successfully. * The installation package was faulty and contained "/usr/lib/cmake/vtk-7.1/VTKTargets.cmake" but not all the files it references. CMake Warning (dev) at /usr/local/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message): The package name passed to `find_package_handle_standard_args` (libusb-1.0) does not match the name of the calling package (PCL). This can lead to problems in calling code that expects `find_package` result variables (e.g., `_FOUND`) to follow a certain pattern. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:291 (find_package_handle_standard_args) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:338 (find_libusb) /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:548 (find_external_library) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found libusb-1.0: /usr/include ** WARNING ** io features related to libusb-1.0 will be disabled -- looking for PCL_COMMON CMake Warning (dev) at /usr/local/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message): The package name passed to `find_package_handle_standard_args` (PCL_COMMON) does not match the name of the calling package (PCL). This can lead to problems in calling code that expects `find_package` result variables (e.g., `_FOUND`) to follow a certain pattern. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:616 (find_package_handle_standard_args) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found PCL_COMMON: /usr/lib/x86_64-linux-gnu/libpcl_common.so -- looking for PCL_OCTREE CMake Warning (dev) at /usr/local/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message): The package name passed to `find_package_handle_standard_args` (PCL_OCTREE) does not match the name of the calling package (PCL). This can lead to problems in calling code that expects `find_package` result variables (e.g., `_FOUND`) to follow a certain pattern. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:616 (find_package_handle_standard_args) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found PCL_OCTREE: /usr/lib/x86_64-linux-gnu/libpcl_octree.so -- looking for PCL_IO CMake Warning (dev) at /usr/local/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:441 (message): The package name passed to `find_package_handle_standard_args` (PCL_IO) does not match the name of the calling package (PCL). This can lead to problems in calling code that expects `find_package` result variables (e.g., `_FOUND`) to follow a certain pattern. Call Stack (most recent call first): /usr/lib/x86_64-linux-gnu/cmake/pcl/PCLConfig.cmake:616 (find_package_handle_standard_args) CMakeLists.txt:41 (find_package) This warning is for project developers. Use -Wno-dev to suppress it. -- Found PCL_IO: /usr/lib/x86_64-linux-gnu/libpcl_io.so -- Found PCL: pcl_common;pcl_octree;pcl_io;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_filesystem.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_iostreams.so;/usr/local/lib/libboost_regex.so;/usr/lib/libOpenNI.so;/usr/lib/libOpenNI2.so;vtkChartsCore;vtkCommonColor;vtkCommonCore;vtksys;vtkCommonDataModel;vtkCommonMath;vtkCommonMisc;vtkCommonSystem;vtkCommonTransforms;vtkCommonExecutionModel;vtkFiltersGeneral;vtkCommonComputationalGeometry;vtkFiltersCore;vtkInfovisCore;vtkFiltersExtraction;vtkFiltersStatistics;vtkImagingFourier;vtkImagingCore;vtkalglib;vtkRenderingContext2D;vtkRenderingCore;vtkFiltersGeometry;vtkFiltersSources;vtkRenderingFreeType;/usr/lib/x86_64-linux-gnu/libfreetype.so;/usr/lib/x86_64-linux-gnu/libz.so;vtkFiltersModeling;vtkImagingSources;vtkInteractionStyle;vtkInteractionWidgets;vtkFiltersHybrid;vtkImagingColor;vtkImagingGeneral;vtkImagingHybrid;vtkIOImage;vtkDICOMParser;vtkmetaio;/usr/lib/x86_64-linux-gnu/libjpeg.so;/usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libtiff.so;vtkRenderingAnnotation;vtkRenderingVolume;vtkIOXML;vtkIOCore;vtkIOXMLParser;/usr/lib/x86_64-linux-gnu/libexpat.so;vtkIOGeometry;vtkIOLegacy;vtkIOPLY;vtkRenderingLOD;vtkViewsContext2D;vtkViewsCore;vtkRenderingContextOpenGL2;vtkRenderingOpenGL2 (Required is at least version "1.10") CMake Warning at CMakeLists.txt:44 (find_package): By not providing "Findrealsense2.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "realsense2", but CMake did not find one. Could not find a package configuration file provided by "realsense2" with any of the following names: realsense2Config.cmake realsense2-config.cmake Add the installation prefix of "realsense2" to CMAKE_PREFIX_PATH or set "realsense2_DIR" to a directory containing one of the above files. If "realsense2" provides a separate development package or SDK, be sure it has been installed. CMake Deprecation Warning at Thirdparty/g2o/CMakeLists.txt:1 (CMAKE_MINIMUM_REQUIRED): Compatibility with CMake < 3.10 will be removed from a future version of CMake. Update the VERSION argument <min> value. Or, use the <min>...<max> syntax to tell CMake that the project requires at least <min> but has been updated to work with policies introduced by <max> or earlier. -- BUILD TYPE:Release -- Compiling on Unix -- Found OpenMP_C: -fopenmp (found version "4.5") -- Found OpenMP_CXX: -fopenmp (found version "4.5") -- Found OpenMP: TRUE (found version "4.5") -- PCL_INCLUDE_DIRS = /usr/include/pcl-1.10;/usr/include/eigen3;/usr/include;/usr/include/ni;/usr/include/openni2;/usr/include/vtk-7.1;/usr/include/freetype2;/usr/include/x86_64-linux-gnu -- PCL_LIBRARIES = pcl_common;pcl_octree;pcl_io;/usr/local/lib/libboost_system.so;/usr/local/lib/libboost_filesystem.so;/usr/local/lib/libboost_date_time.so;/usr/local/lib/libboost_iostreams.so;/usr/local/lib/libboost_regex.so;/usr/lib/libOpenNI.so;/usr/lib/libOpenNI2.so;vtkChartsCore;vtkCommonColor;vtkCommonCore;vtksys;vtkCommonDataModel;vtkCommonMath;vtkCommonMisc;vtkCommonSystem;vtkCommonTransforms;vtkCommonExecutionModel;vtkFiltersGeneral;vtkCommonComputationalGeometry;vtkFiltersCore;vtkInfovisCore;vtkFiltersExtraction;vtkFiltersStatistics;vtkImagingFourier;vtkImagingCore;vtkalglib;vtkRenderingContext2D;vtkRenderingCore;vtkFiltersGeometry;vtkFiltersSources;vtkRenderingFreeType;/usr/lib/x86_64-linux-gnu/libfreetype.so;/usr/lib/x86_64-linux-gnu/libz.so;vtkFiltersModeling;vtkImagingSources;vtkInteractionStyle;vtkInteractionWidgets;vtkFiltersHybrid;vtkImagingColor;vtkImagingGeneral;vtkImagingHybrid;vtkIOImage;vtkDICOMParser;vtkmetaio;/usr/lib/x86_64-linux-gnu/libjpeg.so;/usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libtiff.so;vtkRenderingAnnotation;vtkRenderingVolume;vtkIOXML;vtkIOCore;vtkIOXMLParser;/usr/lib/x86_64-linux-gnu/libexpat.so;vtkIOGeometry;vtkIOLegacy;vtkIOPLY;vtkRenderingLOD;vtkViewsContext2D;vtkViewsCore;vtkRenderingContextOpenGL2;vtkRenderingOpenGL2 -- Configuring done (2.3s) CMake Error at CMakeLists.txt:136 (add_executable): Cannot find source file: main.cpp Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc CMake Error at CMakeLists.txt:136 (add_executable): No SOURCES given to target: main CMake Generate step failed. Build files cannot be regenerated correctly. tao@tao:~/SLAM/ORB_SLAM3/build$ grep -i "error" cmake.log CMake Error at CMakeLists.txt:136 (add_executable): CMake Error at CMakeLists.txt:136 (add_executable): 第二步编译出来的结果
最新发布
10-27
-- 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
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

10km

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

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

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

打赏作者

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

抵扣说明:

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

余额充值