23

cmake find_package でブーストがインストールされていないことが判明した場合、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>_FOUNDTRUEしたがって、あなたの場合、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)

後注: REQUIREDBoost を探すときにフラグを使用するため (Unix プラットフォームでのみ必要なため)、optional-source-files-in-a-variableトリックを使用するだけで十分です。

(*) ご質問のおかげで、ディレクトリが同じプロジェクト内のすべてのターゲットに追加された後、またはディレクトリが追加されinclude_directories(...)た後、ターゲットを作成する前または後に呼び出されるかどうかは問題ではないことがわかりました。ADD_EXECUTABLEADD_LIBRARY

于 2012-04-19T11:49:41.687 に答える
7

はい、find_package(Boost COMPONENTS system filesystem REQUIRED)成功すればBoost_FOUNDtrue になります。

Boost_date_time_FOUNDまた、コンポーネント固有のバージョンもありますBoost_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 に答える