CMake中find_package功能演示
find_package能夠被用來在系統中自動查找配置構建工程所需的程序庫。在linux和unix類系統下這個命令尤爲有用。CMake自帶的模塊文件裏有大半是對各類常見開源庫的find_package支持,支持庫的種類很是多。python
find_package(Qt5Widgets),find_package(Qt4 COMPONENTS QTCORE QTGUI QTOPENGL QTSVG)返回以後linux
就會有變量Qt5Widgets_FOUND,Qt5Widgets_INCLUDE_DIRS相應的變量生效。ui
而後就能夠在CMakeLists.txt裏面使用上述的變量了。相似以下:spa
1 INCLUDE_DIRECTORIES(${QT_INCLUDES}) 2 INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS}) 3 INCLUDE_DIRECTORIES(${Qt5Svg_INCLUDE_DIRS}) 4 INCLUDE_DIRECTORIES(${Qt5OpenGL_INCLUDE_DIRS}) 5 INCLUDE_DIRECTORIES(${Qt5Concurrent_INCLUDE_DIRS})
if(Qt5Widgets_FOUND) message(STATUS ${Qt5Widgets_INCLUDE_DIRS}) #qt5 else() find_package(Qt4 REQUIRED) #qt4 include(${QT_USE_FILE}) endif()
例子:debug
cmake_minimum_required(VERSION 2.6 FATAL_ERROR) project(hello) set(Boost_USE_STATIC_LIBS ON) set(Boost_USE_MULTITHREAD ON) #查找boost庫中python的bind庫 #REQUIRED表示若是沒有找到,cmake會中止處理,並報告一個錯誤. find_package( Boost 1.35 REQUIRED COMPONENTS python) #找到Boost後,變量Boost_INCLUDE_DIRS中將包括指定boost庫頭文件的查找路徑. #變量Boost_LIBRARY_DIRS中將包含指定boost庫的.a或.so文件的所在目錄的路徑. include_directories(${Boost_INCLUDE_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) find_package(PythonLibs 2.5 REQUIRED) add_library(hello SHARED hello.cpp) include_directories(${PYTHON_INCLUDE_PATH}) target_link_libraries(hello debug ${Boost_PYTHON_LIBRARY_DEBUG} optimized ${Boost_PYTHON_LIBRARY_RELEASE} ) target_link_libraries(hello debug ${PYTHON_DEBUG_LIBRARIES} optimized ${PYTHON_LIBRARIES} )