cmake:FindLIBCONFIG.cmake查找libconfig的脚本find_package脚本

libconfig 是一个用于读取,操作和编写结构化配置文件的库。cmake并没有为libconfig提供find_package脚本。所以自己撸一个,实现跨平台的查找libconfig库

FindLIBCONFIG.cmake

# - Find libconfig
# - This module determines the libconfig library of the system
# The following variables are set if the library found:
# LIBCONFIG_FOUND - If false do nnt try to use libconfig.
# LIBCONFIG_INCLUDE_DIRS - where to find the headfile of library.
# LIBCONFIG_LIBRARY_DIRS - where to find the libconfig library.
# LIBCONFIG_LIBRARIES, the library file name needed to use libconfig.
# LIBCONFIG_LIBRARY - the library needed to use libconfig.
# imported target
#   libconfig::libconfig

if(LIBCONFIG_FOUND)
	return()
endif()
if (WIN32)
	# windows下使用CONFIG模式调用find_package查找
	find_package(LIBCONFIG CONFIG)
else ()
	# linux下调用pkg_check_modules 查找
    include(FindPkgConfig)
    unset(_verexp)
    if(LIBCONFIG_FIND_VERSION)
    	if(LIBCONFIG_FIND_VERSION_EXACT)    	
    	    set(_verexp "=${LIBCONFIG_FIND_VERSION}")
    	else()
    	    set(_verexp ">=${LIBCONFIG_FIND_VERSION}")
    	endif()
    endif()
    pkg_check_modules (LIBCONFIG libconfig${_verexp})
endif()

# handle the QUIETLY and REQUIRED arguments and set LIBCONFIG_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LIBCONFIG DEFAULT_MSG	LIBCONFIG_LIBRARIES LIBCONFIG_INCLUDE_DIRS)

if(LIBCONFIG_FOUND)
    if(NOT LIBCONFIG_FIND_QUIETLY)
        message(STATUS "  -I: ${LIBCONFIG_INCLUDE_DIRS}")
        message(STATUS "  -L: ${LIBCONFIG_LIBRARY_DIRS}")
        message(STATUS "  -l: ${LIBCONFIG_LIBRARIES}")
    endif()    
    find_library (LIBCONFIG_LIBRARY NAMES ${LIBCONFIG_LIBRARIES} PATHS ${LIBCONFIG_LIBRARY_DIRS})

    # 创建 imported target
    if (NOT TARGET libconfig::libconfig)
        add_library(libconfig::libconfig INTERFACE IMPORTED)
        set_target_properties(libconfig::libconfig PROPERTIES
        	INTERFACE_COMPILE_OPTIONS "${LIBCONFIG_CFLAGS}"
            INTERFACE_INCLUDE_DIRECTORIES "${LIBCONFIG_INCLUDE_DIRS}"
    		INTERFACE_LINK_LIBRARIES   "-L${LIBCONFIG_LIBRARY_DIRS} -l${LIBCONFIG_LIBRARIES }"
	    	)
	    if(NOT LIBCONFIG_FIND_QUIETLY)
    	    message(STATUS "IMPORTED TARGET: libconfig::libconfig,link libraies ${_link_libs}")
    	endif()
    endif ()
endif(LIBCONFIG_FOUND)

调用示例

# 添加FindLIBCONFIG.cmake所在的路径到CMAKE_MODULE_PATH 
list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules")
find_package (LIBCONFIG 1.7.2 MODULE QUIET REQUIRED )
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

10km

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

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

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

打赏作者

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

抵扣说明:

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

余额充值