如何检查find_package是否找到了包(boost)

时间:2012-04-19 11:25:37

标签: cmake

如果cmake find_package没有安装boost,我想不添加boost.cxx。 find_package是否会返回一些我可以包装的条件来编译boost.cxx。这是我当前的cmake文件:

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)

3 个答案:

答案 0 :(得分:10)

如果找到包,FindXXX脚本应该将变量<Packagename>_FOUND设置为TRUE。因此,在您的情况下,如果找到提升,它将设置Boost_FOUND

在编译Boost.cxx时,我认为您还需要Boost标头,因此您也应该调整包含目录。*

在创建可执行文件之前查找Boost。此外,您需要在添加可执行文件之前设置包含目录。

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:由于您在查找Boost时使用REQUIRED标志(因为您只需要在Unix平台上使用它),因此使用 optional-source-files-in-a-variable就足够了技巧。

(*)感谢您提出的问题,我发现在使用include_directories(...)ADD_EXECUTABLE创建目标之前或之后调用ADD_LIBRARY并不重要被添加到同一项目中的所有目标。

答案 1 :(得分:5)

是的,它设置变量Boost_FOUND。 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()

答案 2 :(得分:5)

是的,如果find_package(Boost COMPONENTS system filesystem REQUIRED)成功,Boost_FOUND将成立。

此外,还会有特定于组件的版本,因此Boost_date_time_FOUNDBoost_filesystem_FOUND等等。

有关详细信息,请运行

cmake --help-module FindBoost