文章目录
- CMakeLists.txt编写
- 1.CMakeLists.txt模板
- 2.设置编程语言版本
- 3.设置编译类型Debug,Release
- 4.设置获取文件列表
- 5.添加include目录
- 6.配置编译选项
CMakeLists.txt编写
1.CMakeLists.txt模板
一个使用opencv 的 CMakeLists.txt
# cmake最低版本要求
cmake_minimum_required(VERSION 3.0)
# 项目名称
project(trilinear_fast)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -march=native -fopenmp -msse -msse2 -msse3 -msse4 -mavx -O2 -DNDEBUG")
set(CMAKE_BUILD_TYPE Release)
#set(OpenCV_DIR /usr/local/lib/cmake/opencv4)
#set(OpenCV_INCLUDE_DIRS /usr/local/include/opencv4)
find_package(OpenCV REQUIRED)
include_directories(include ${OpenCV_INCLUDE_DIRS})
message(STATUS "project name = ${PROJECT_NAME}")
message(STATUS "OpenCV_LIBRARIES name = ${OpenCV_LIBRARIES}")
#添加静态库
file(GLOB_RECURSE SRC ./src/*.cpp )
add_library(lut3d STATIC ${SRC})
add_executable(${PROJECT_NAME} main.cpp)
# include head file of static lib
target_include_directories(${PROJECT_NAME} PRIVATE ./src)
# 该可执行文件调用的静态库
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES} lut3d)
对应的目录:
2.设置编程语言版本
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
3.设置编译类型Debug,Release
set(CMAKE_BUILD_TYPE Release)
4.设置获取文件列表
MATH_LIB_SRC 指示:找到的math目录下的 c文件
file(GLOB_RECURSE MATH_LIB_SRC src/c/math/*.c)
add_library(math STATIC ${MATH_LIB_SRC})
5.添加include目录
表示在src/include/目录中寻找头文件
include_directories(src/include)
6.配置编译选项
add_compile_options命令添加的编译选项是针对所有编译器的(包括c和c++编译器)
CMAKE_C_FLAGS或CMAKE_CXX_FLAGS变量则是分别只针对c和c++编译器
add_compile_options(-Wall -Wextra -pedantic -Werror)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -std=c99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -DNDEBUG")
以上基本覆盖了常用的一些CMAKE 指令, 后续用到再补充