cmake find_package说明
CMake中find_package功能演示
find_package可以被用来在系统中自动查找配置构建工程所需的程序库。在linux和unix类系统下这个命令尤其有用。CMake自带的模块文件里有大半是对各种常见开源库的find_package支持,支持库的种类非常多。
find_package(Qt5Widgets),find_package(Qt4 COMPONENTS QTCORE QTGUI QTOPENGL QTSVG)返回之后
就会有变量Qt5Widgets_FOUND,Qt5Widgets_INCLUDE_DIRS相应的变量生效。
然后就可以在CMakeLists.txt里面使用上述的变量了。类似如下:
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()
例子:
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} )
坚持就是胜利