2012-04-19 13 views
11

No quiero agregar boost.cxx si cmake find_package no encontró ningún boost instalado. Does find_package devuelve algo que puedo envolver para compilar boost.cxx o no. Aquí está mi actual archivo cmake:Cómo comprobar si find_package encontró el paquete (boost)

add_executable (complex complex.cxx lexer.cxx boost.cxx ../../src/lili.cxx ../../src/lilu.cxx) 

# Make sure the compiler can find all include files 
include_directories (../../src) 
include_directories (.) 

# Make sure the linker can find all needed libraries 
# rt: clock_gettime() 
target_link_libraries(complex rt) 

# Install example application 
install (TARGETS complex 
     RUNTIME DESTINATION bin) 

IF(UNIX) 
    find_package(Boost COMPONENTS system filesystem REQUIRED) 

    ## Compiler flags 
    if(CMAKE_COMPILER_IS_GNUCXX) 
     set(CMAKE_CXX_FLAGS "-O2") 
     set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread") 
    endif() 

    target_link_libraries(complex 
     ${Boost_FILESYSTEM_LIBRARY} 
     ${Boost_SYSTEM_LIBRARY} 
     #${PROTOBUF_LIBRARY} 
    ) 
ENDIF(UNIX) 

Respuesta

8

Se supone que los FindXXX secuencias de comandos para establecer una variable <Packagename>_FOUND-TRUE si el paquete fue encontrado. Entonces, en su caso, establecerá Boost_FOUND si se encontró impulso.

Al compilar su Boost.cxx, supongo que necesitará cabeceras de Boost, así, por lo que debe ajustar las necesidades de inclusión directorios también. *

look para Boost antes de crear el ejecutable. Además, debes establecer tus directorios de inclusión antes de agregar el ejecutable.

IF(UNIX) 
    find_package(Boost COMPONENTS system filesystem REQUIRED) 
    # IF(Boost_FOUND) # checking this variable isnt even necessary, since you added 
         # REQUIRED to your call to FIND_PACKAGE 
     SET(BOOST_SRC_FILES boost.cxx) 
     INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) # you could move this down as well 
                # as ${Boost_INCLUDE_DIRS} will be 
                # empty if Boost was not found 
    # ENDIF() 
ENDIF() 

add_executable (complex complex.cxx lexer.cxx ${BOOST_SRC_FILES} ../../src/lili.cxx ../../src/lilu.cxx) 

# Make sure the compiler can find all include files 
include_directories (../../src) 
include_directories (.) 
# INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS}) # alternative location to 
               # add include dirs, see above 

# Make sure the linker can find all needed libraries 
# rt: clock_gettime() 
target_link_libraries(complex rt) 

# Install example application 
install (TARGETS complex 
     RUNTIME DESTINATION bin) 

IF(UNIX) 

    ## Compiler flags 
    if(CMAKE_COMPILER_IS_GNUCXX) 
     set(CMAKE_CXX_FLAGS "-O2") 
     set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread") 
    endif() 

    target_link_libraries(complex 
     ${Boost_FILESYSTEM_LIBRARY} 
     ${Boost_SYSTEM_LIBRARY} 
     #${PROTOBUF_LIBRARY} 
    ) 
ENDIF(UNIX) 

Afternote: Puesto que se utiliza la bandera REQUIRED en la búsqueda de Boost (ya que sólo se necesita en la plataforma Unix) que es aún suficiente para utilizar los opcional-source-files-in-a-variable de truco .

(*) Gracias a su pregunta, acabo de saber que no importa si se llama include_directories(...) antes o después de crear el objetivo con ADD_EXECUTABLE o ADD_LIBRARY ya que los directorios se añaden a todos los objetivos en el mismo proyecto.

+1

¡Impresionante! Hiciste todo esto para mí, ¡gracias! – Cynede

+2

Tenga cuidado: la variable Libname_FOUND es sensible a mayúsculas y minúsculas para algunas bibliotecas. P.ej. GTest_FOUND no se establecerá aunque find_package (GTest) sea exitoso. En cambio, se establece GTEST_FOUND (usando cmake 3.0.2). –

5

Sí, conjuntos de variables Boost_FOUND. Ejemplo de FindBoost.cmake:

== Using actual libraries from within Boost: == 
# 
# set(Boost_USE_STATIC_LIBS  ON) 
# set(Boost_USE_MULTITHREADED  ON) 
# set(Boost_USE_STATIC_RUNTIME OFF) 
# find_package(Boost 1.36.0 COMPONENTS date_time filesystem system ...) 
# 
# if(Boost_FOUND) 
#  include_directories(${Boost_INCLUDE_DIRS}) 
#  add_executable(foo foo.cc) 
#  target_link_libraries(foo ${Boost_LIBRARIES}) 
# endif() 
4

Sí, si el find_package(Boost COMPONENTS system filesystem REQUIRED) tiene éxito, Boost_FOUND será cierto.

Además, habrá versiones específicas de cada componente, así Boost_date_time_FOUND, Boost_filesystem_FOUND, etc.

Para más información, ejecute

cmake --help-module FindBoost 
Cuestiones relacionadas