【问题标题】:Is there a way to generate find<...>.cmake file for library provided by the package system?有没有办法为包系统提供的库生成 find<...>.cmake 文件?
【发布时间】:2017-01-31 11:13:44
【问题描述】:

理论上,如果还没有内置到 cmake 中,它应该很简单。毕竟aptrpm知道头文件和目标文件的位置,所以应该是可行的。

我知道 cmake 应该是跨平台的。但有时它用于仅在 Linux 上运行的项目。

我可以将 /usr/include 硬编码为 include 并将 /usr/lib/x86_64-linux-gnu 硬编码为库目录,但 Ubuntu 系统上至少还有两个其他“标准”位置可以包含这些文件,更不用说非标准路径,例如/opt中的那些。

【问题讨论】:

    标签: ubuntu cmake


    【解决方案1】:

    我找到了以下 cmake 文件 amirgholami's Github。到目前为止它对我有用。

    #######################################################################
    #
    #  This is a basic, generic find_package function intended to find
    #  any package with the following conditions:
    #
    #  (1) The package has a CMake variable associated with it
    #      that is a path directory pointing to the installation directory
    #      of the package.  It is assumed that the libraries and include
    #      files are either contiained within this directory or the
    #      sub-directories lib/ or include/, respectively.
    #
    #  (2) The name(s) of the required library file(s) will be given to
    #      the macro as a string list argument, and each will be tested in 
    #      sequence.
    #
    #  (3) The name(s) of the required include file(s) will be given to 
    #      the macro as a string list argument, and each will be tested in 
    #      sequence.
    #
    #  (4) For a package with associated environment variable VAR, this
    #      macro will define the variables:
    #
    #        VAR_FOUND - Boolean indicating if all files/libraries were found
    #        VAR_INCLUDE_DIRS - Directory path(s) to include files
    #        VAR_LIBRARIES - Full path(s) to each libraries
    #
    # AUTHOR:  Kevin Paul <kpaul@ucar.edu>
    # DATE:    11 Feb 2014
    #   
    #######################################################################
    
    function(BASIC_FIND PCKG REQ_INCS REQ_LIBS)
    
    #If environment variable ${PCKG}_DIR is specified, 
    # it has same effect as local ${PCKG}_DIR
    if( (NOT ${PCKG}_DIR) AND DEFINED ENV{${PCKG}_DIR} )
      set( ${PCKG}_DIR "$ENV{${PCKG}_DIR}" )
      message(STATUS " ${PCKG}_DIR is set from environment: ${${PCKG}_DIR}")
    endif()
    
    if (NOT ${PCKG}_DIR)
      if(!${PCKG}_FIND_QUIETLY)
        message (WARNING " Option ${PCKG}_DIR not set.")
      endif ()
    else (NOT ${PCKG}_DIR)
      message (STATUS " ${PCKG}_DIR set to ${${PCKG}_DIR}")
    endif (NOT ${PCKG}_DIR)
    
    message (STATUS " Searching for package ${PCKG}...")
    set (${PCKG}_FOUND FALSE PARENT_SCOPE)
    
    # Look for each required include file
    foreach(INC_FILE ${REQ_INCS})
      message (STATUS " Searching for include file: ${INC_FILE}")
      set (INC_DIR ${INC_FILE}-NOTFOUND)
      find_path(INC_DIR ${INC_FILE}
        HINTS ${${PCKG}_DIR} ${${PCKG}_DIR}/include)
      if (EXISTS ${INC_DIR}/${INC_FILE})
        message (STATUS " Found include file ${INC_FILE} in ${INC_DIR} required by ${PCKG}")
        set (${PCKG}_INCLUDE_DIRS ${${PCKG}_INCLUDE_DIRS} ${INC_DIR} PARENT_SCOPE)
      elseif(!${PCKG}_FIND_QUIETLY)
        message (WARNING " Failed to find include file ${INC_FILE} required by ${PCKG}")
      endif ()
    endforeach()
    # Look for each required library
    foreach(LIB_NAME ${REQ_LIBS})
      message (STATUS " Searching for library: ${LIB_NAME}")
      set (LIB ${LIB_NAME}-NOTFOUND)
      find_library(LIB NAMES "lib${LIB_NAME}.a" "${LIB_NAME}"
        HINTS ${${PCKG}_DIR} ${${PCKG}_DIR}/lib)
      if (EXISTS ${LIB})
        message (STATUS " Found library at ${LIB} required by ${PCKG}")
        set (${PCKG}_LIBRARIES ${${PCKG}_LIBRARIES} ${LIB} PARENT_SCOPE)
        set (${PCKG}_FOUND TRUE PARENT_SCOPE)
        set (${PCKG}_FOUND TRUE )
      else ()
        set (${PCKG}_FOUND FALSE PARENT_SCOPE)
        set (${PCKG}_FOUND FALSE )
      endif ()
    endforeach()
    
    # If we made it this far, then we call the package "FOUND"
    if(${PCKG}_FOUND)
      message (STATUS "All required include files and libraries found.")
    else()
      if(!${PCKG}_FIND_QUIETLY)
        message ("WARNING! ${PCKG} not found.")
      else()
        message ("${PCKG} not found (Optional, Not Required).")
      endif()
    endif()
    
    endfunction(BASIC_FIND)
    

    您在CMakeLists.txt 中这样使用它(例如libpnetcdf-dev):

    include(basicFind)
    BASIC_FIND (PNETCDF "pnetcdf.h" "pnetcdf")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2020-04-30
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      相关资源
      最近更新 更多