2017-01-31 6 views
0

Теоретически это должно быть простым, если уже не встроено в cmake. В конце концов, apt или rpm знает расположение заголовков и объектных файлов, поэтому он должен быть выполнимым.Есть ли способ сгенерировать find <...> .cmake файл для библиотеки, предоставляемый системой пакетов?

Я знаю, что cmake предполагается кросс-платформенным. Но иногда он используется для проектов, которые будут запускаться только в Linux.

я могу жестко закодировать /usr/include как включать и /usr/lib/x86_64-linux-gnu в библиотечном каталоге, но есть, по крайней мере, два другие «стандартные» места на системах Ubuntu, которые могут содержать эти файлы, не говоря уже о нестандартных путях, как и в /opt ,

ответ

0

Я нашел следующий файл 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 <[email protected]> 
# 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")