23

如果 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)
4

3 回答 3

19

如果找到包,FindXXX脚本应该设置一个变量。因此,在您的情况下,如果找到提升,它将设置。<Packagename>_FOUNDTRUEBoost_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)

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

include_directories(...)(*)感谢您的问题,我刚刚发现无论是在创建目标之前还是之后调用ADD_EXECUTABLE都无关紧要,ADD_LIBRARY因为目录已添加到同一项目中的所有目标中。

于 2012-04-19T11:49:41.687 回答
7

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

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

有关更多信息,请运行

cmake --help-module FindBoost
于 2012-04-19T11:41:48.267 回答
6

是的,它设置了 variable 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()
于 2012-04-19T11:38:08.050 回答