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

cnd*_*cnd 13 cmake

我想不添加boost.cxx如果cmake find_package发现没有安装boost.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)
Run Code Online (Sandbox Code Playgroud)

Joh*_* S. 10

FindXXX脚本应该给一个变量设置<Packagename>_FOUNDTRUE如果包裹被发现.因此,在您的情况下,它将设置Boost_FOUND是否找到了提升.

在编译时Boost.cxx,我假设你也需要Boost头文件,所以你也应该调整你的include目录.*

在创建可执行文件之前查找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)
Run Code Online (Sandbox Code Playgroud)

Afternote:因为你REQUIRED在寻找Boost时使用了标志(因为你只需要在Unix平台上使用它),所以甚至可以使用可选的源文件变量技巧.

(*)由于你的问题,我发现无论include_directories(...)是在创建目标之前还是之后调用,ADD_EXECUTABLE或者ADD_LIBRARY因为目录被添加到同一项目中的所有目标,都无关紧要.

  • 注意 - Libname_FOUND变量区分大小写,并且对某些库有所有上限.例如.即使find_package(GTest)成功,也不会设置GTest_FOUND.而是设置GTEST_FOUND(使用cmake 3.0.2). (2认同)

KCH*_*KCH 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()
Run Code Online (Sandbox Code Playgroud)


Fra*_*ser 5

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

此外,将有部分特定的版本,因此Boost_date_time_FOUNDBoost_filesystem_FOUND

有关更多信息,请运行

cmake --help-module FindBoost
Run Code Online (Sandbox Code Playgroud)