首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cmake:FindLIBCONFIG.cmake查找libconfig的脚本find_package脚本

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

作者头像
10km
发布2019-05-25 20:55:14
7820
发布2019-05-25 20:55:14
举报
文章被收录于专栏:10km的专栏10km的专栏10km的专栏

版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433477

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 )
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年10月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 调用示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档