2012-04-19 41 views
11

Tôi không muốn thêm boost.cxx nếu cmake find_package không tìm thấy cài đặt tăng cường nào. Có find_package trả lại một cái gì đó mà tôi có thể bọc trong điều kiện để biên dịch boost.cxx hay không. Đây là tệp cmake hiện tại của tôi:Cách kiểm tra xem find_package có tìm thấy gói (tăng)

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) 

Trả lời

8

Các FindXXX kịch bản có nghĩa vụ phải thiết lập một biến <Packagename>_FOUND-TRUE nếu gói đã được tìm thấy. Vì vậy, trong trường hợp của bạn, nó sẽ thiết lập Boost_FOUND nếu tăng được tìm thấy.

Khi biên dịch Boost.cxx của bạn, tôi cho rằng bạn sẽ cần tiêu đề Boost là tốt, vì vậy bạn nên điều chỉnh của bạn bao gồm các thư mục là tốt. *

nhìn cho Boost trước khi tạo thực thi của bạn. Ngoài ra, bạn cần phải thiết lập thư mục bao gồm của bạn trước khi thêm thực thi.

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: Kể từ khi bạn sử dụng REQUIRED cờ khi tìm kiếm Boost (vì bạn chỉ cần nó trên nền tảng Unix) nó thậm chí còn đủ để sử dụng tùy chọn-source-file-trong-một-biến lừa .

(*) Cảm ơn câu hỏi của bạn, tôi phát hiện ra rằng không quan trọng include_directories(...) được gọi trước hoặc sau khi tạo mục tiêu với ADD_EXECUTABLE hoặc ADD_LIBRARY vì các thư mục được thêm vào tất cả các mục tiêu trong cùng một dự án.

+1

Tuyệt vời! Bạn đã làm tất cả công việc này cho tôi, cảm ơn bạn! – Cynede

+2

Hãy cẩn thận - biến Libname_FOUND phân biệt chữ hoa chữ thường và tất cả các mũ cho một số thư viện. Ví dụ. GTest_FOUND sẽ không được đặt mặc dù find_package (GTest) thành công. Thay vào đó, GTEST_FOUND được thiết lập (sử dụng cmake 3.0.2). –

5

Có, nó đặt biến Boost_FOUND. Ví dụ từ 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

Vâng, nếu find_package(Boost COMPONENTS system filesystem REQUIRED) thành công, Boost_FOUND sẽ thành sự thật.

Ngoài ra, sẽ có các phiên bản thành phần cụ thể, vì vậy Boost_date_time_FOUND, Boost_filesystem_FOUND vv

Đối biết thêm thông tin, hãy chạy

cmake --help-module FindBoost 
Các vấn đề liên quan