CMake是一个常用的跨平台编译系统生成器。以下通过常见的几个CMakeLists文件例子,来熟悉常用的CMake命令。
1.最简单的例子
##对CMake版本的最低要求
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
##设置工程名
PROJECT(HELLO)
##对变量HELLO_SRC进行赋值
SET(HELLO_SRC hello.cpp)
##根据源文件生成可执行文件hello
ADD_EXECUTABLE (hello ${HELLO_SRC})
##if语句中可直接用变量,否则都需要使用${Variable}
2.加入了依赖库和链接
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
PROJECT(test)
##寻找opencv视觉库模块
FIND_PACKAGE(OpenCV REQUIRED)
##条件判断,找不到头文件时中断
IF(NOT OpenCV_FOUND)
##发出致命错误并中断
MESSAGE(FATAL_ERROR ”opencv library not found”)
ENDIF(NOT OpenCV_FOUND)
##添加依赖的头文件
INCLUDE_DIRECTORIES (${OpenCV_INCLUDES})
##将当前文件中所以得cpp文件加入变量SRC_LIST中
FILE(GLOB_RECURSE SRC_LIST "./*.cpp")
##根据源文件生成可执行文件hello
ADD_EXECUTABLE (hello ${SRC_LIST})
##链接可执行文件生成时依赖的库
TARGET_LINK_LIBRARIES (hello ${OpenCV_LIBRAIES})
##也可以使用AUX_SOURCE_DIRECTORY(./src SRC_LIST)命令来
##将多个源文件加到变量中。
##每个模块都会有变量
##1:<name>_FOUND:该模块是否找到
##2:<name>_INCLUDE_DIR or <name>_INCLUDES:头文件目录
##3:<name>_LIBRARY or <name>_LIBRARIES:库文件
3.对整个工程进行分层,分成顶层CMakeLists和src中的CMakeLists
在上层目录中:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(stereo)
##添加c++编译器选项
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic -O3 -Wno-long-long")
##设置可执行文件的生成目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
##设置库文件生成目录
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
##CMAKE_BINARY_DIR PROJECT_BINARY_DIR 工程編譯目錄
##CMAKE_SOURCE_DIR PROJECT_SOURCE_DIR 工程頂層目錄
##添加头文件搜索路径
INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/include)
##添加库文件搜索路径
LINK_DIRECTORIES( ${PROJECT_SOURCE_DIR}/lib)
##添加OpenCL的头文件和库文件搜索路径
INCLUDE_DIRECTORIES( /usr/local/cuda-8.0/include)
LINK_DIRECTORIES (/usr/local/cuda-8.0/lib64)
##添加OpenCV的头文件和库文件搜索路径
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS})
##常用信息的打印,输出头文件路径
MESSAGE(STATUS "OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
LINK_DIRECTORIES( ${OpenCV_LIB_DIR} )
MESSAGE(STATUS "OpenCV_LIB_DIR: ${OpenCV_LIB_DIR}")
##指定代码所在的子目录
ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/src)
在src目录中:
##根据源文件生成动态库
ADD_LIBRARY(fileHelper SHARED fileHelper.cpp)
ADD_LIBRARY(stereo SHARED utli.cpp )
##添加动态库生成时需要链接的库文件
TARGET_LINK_LIBRARIES (stereo ${OpenCV_LIBS} OpenCL fileHelper)
##根据源文件生成可执行文
ADD_EXECUTABLE (main main.cpp)
##添加可执行文件生成时需要链接的库文件
TARGET_LINK_LIBRARIES (main stereo)
4.交叉编译
在上层目录中:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(stereo)
##设置编译的目标平台
SET(CMAKE_SYSTEM_NAME Linux)
##设置c++编译选项
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic -O3 -Wno-long-long")
##指定cmake查找的根目录
SET(CMAKE_FIND_ROOT_PATH /home/ll/daily_build_20160802/release_for_developer/sdk/sysroots/eagle8074)
##手动进行编译器的设置,不自动查找
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
##只在指定的根目录下进行头文件和库的查找
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
##设置c++和c编译器
SET(CMAKE_C_COMPILER /home/ll/daily_build_20160802/release_for_developer/sdk/gcc-linaro-arm-linux-gnueabihf-4.8-2013.08_linux/bin/arm-linux-gnueabihf-gcc )
SET(CMAKE_CXX_COMPILER /home/ll/daily_build_20160802/release_for_developer/sdk/gcc-linaro-arm-linux-gnueabihf-4.8-2013.08_linux/bin/arm-linux-gnueabihf-g++)
INCLUDE_DIRECTORIES(${CMAKE_FIND_ROOT_PATH}/usr/include)
LINK_DIRECTORIES (${CMAKE_FIND_ROOT_PATH}/usr/lib)
INCLUDE_DIRECTORIES(${CMAKE_FIND_ROOT_PATH}/opencv/include)
LINK_DIRECTORIES (${CMAKE_FIND_ROOT_PATH}/opencv/lib)
LINK_DIRECTORIES (${CMAKE_FIND_ROOT_PATH}/lib)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/include)
LINK_DIRECTORIES ( ${PROJECT_SOURCE_DIR}/lib)
ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/src)
在src目录中:
ADD_LIBRARY(fileHelper SHARED fileHelper.cpp)
ADD_LIBRARY(stereo SHARED utli.cpp )
TARGET_LINK_LIBRARIES (stereo opencv_imgproc opencv_core opencv_highgui opencv_imgcodecs OpenCL fileHelper)
ADD_EXECUTABLE (main main.cpp)
TARGET_LINK_LIBRARIES (main stereo )
在交叉编译以及库的链接过程中,有几个常见的linux命令可以使用。
##查看文件的依赖库
arm-linux-readelf -d libstereo.so
##对文件进行反编译
objdump -tT libopencv_imgcodecs.so
通过上述几个例子的学习,自己编写简单的CmakeLists文件应该不成问题了。