2012-04-19 10 views
11

Ich möchte boost.cxx nicht hinzufügen, wenn cmake find_package keine Boost installiert gefunden hat. Gibt find_package etwas zurück, das ich in Bedingung umwandeln kann, um boost.cxx zu kompilieren oder nicht. Hier ist meine aktuelle cmake-Datei:Wie überprüft man, ob find_package das Paket gefunden hat (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) 

Antwort

8

Die FindXXX Skripte eine Variable <Packagename>_FOUND-TRUE gesetzt werden soll, wenn Das Paket wurde gefunden. In Ihrem Fall wird Boost_FOUND gesetzt, wenn Boost gefunden wurde.

Wenn Ihr Boost.cxx Kompilieren, nehme ich an, dass Sie auch Boost-Header-Dateien benötigt wird, so dass Sie Ihre Einfügeverzeichnisse auch anpassen sollte. *

Look-Boost, bevor die ausführbare Datei zu erstellen. Außerdem müssen Sie Ihre Include-Verzeichnisse festlegen, bevor Sie die ausführbare Datei hinzufügen.

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: Da Sie verwenden, um die REQUIRED Flags, wenn für Boost-Suche (da Sie brauchen es nur auf Unix-Plattform) ist es auch ausreichend, um die optionalen-Source-files-in-a-Variable Trick zu verwenden, .

(*) Dank Ihrer Frage, ich habe gerade herausgefunden, dass es keine Rolle spielt, ob include_directories(...) vor oder nach dem Erstellen des Ziels mit ADD_EXECUTABLE oder ADD_LIBRARY aufgerufen wird, da die Verzeichnisse zu allen Zielen im selben Projekt hinzugefügt werden.

+1

Super! Du hast all diese Arbeit für mich gemacht, danke! – Cynede

+2

Seien Sie vorsichtig - die Libname_FOUND-Variable unterscheidet zwischen Groß- und Kleinschreibung und Großbuchstaben für einige Bibliotheken. Z.B. GTest_FOUND wird nicht gesetzt, obwohl find_package (GTest) erfolgreich ist. Stattdessen wird GTEST_FOUND festgelegt (mit cmake 3.0.2). –

5

Ja, es setzt die Variable Boost_FOUND. Beispiel aus 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

Ja, wenn die find_package(Boost COMPONENTS system filesystem REQUIRED) gelingt, wird Boost_FOUND wahr sein.

Auch wird es bauteilspezifische Versionen sein, so Boost_date_time_FOUND, Boost_filesystem_FOUND usw.

Für weitere Informationen, laufen

cmake --help-module FindBoost 
Verwandte Themen