概述
cmake文件基本结构
文件名 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial)
# add the executable
add_executable(Tutorial tutorial.cxx)
指令介绍
名称 | 参数 | 说明 |
---|---|---|
set | 参数名 值 | 定义变量 |
add_executable | 可执行程序名 源文件 | 生成可执行程序 |
target_link_libraries | ||
messages | ||
find_package | ||
include_directories | ||
include |
简单使用技巧
保存安装和编译信息
make | tee LOG_make.txt
make install | tee LOG_install.txt
make >& LOG_make.txt
make install >& LOG_install.txt
C++ 版本设置
# Check C++xx support 17,14,11,0x
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
set(CMAKE_CXX_STANDARD 17)
add_definitions(-DCOMPILEDWITHC17)
message(STATUS "Using flag -std=c++17.")
elseif(COMPILER_SUPPORTS_CXX14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(CMAKE_CXX_STANDARD 14)
add_definitions(-DCOMPILEDWITHC14)
message(STATUS "Using flag -std=c++14.")
elseif(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 11)
add_definitions(-DCOMPILEDWITHC11)
message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
set(CMAKE_CXX_STANDARD 0x)
add_definitions(-DCOMPILEDWITHC0X)
message(STATUS "Using flag -std=c++0x.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
编译器设置
set (CMAKE_C_COMPILER "/usr/bin/gcc-9")
set (CMAKE_CXX_COMPILER "/usr/bin/g++-9")
生成路径设置
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
指定 Module 查找路径
list(APPEND CMAKE_MODULE_PATH /home/ubuntu-segate/apps/g2o/cmake_modules)
find_package( CSparse)
MESSAGE("CSparse: lib: " ${CSPARSE_LIB} " include: " ${CSPARSE_INCLUDE_DIR})
文件批量添加,并移除指定文件
file(GLOB G2O_LIB_NAMES /usr/local/lib/libg2o*.so)
file(GLOB SUB_SRC ${PROJECT_SOURCE_DIR}/src/*)
file(GLOB SUB_INCLUDE ${PROJECT_SOURCE_DIR}/include/*)
list(REMOVE_ITEM SUB_SRC ${PROJECT_SOURCE_DIR}/src/user.cpp)
add_library(${PROJECT_NAME} SHARED
${SUB_SRC}
${SUB_INCLUDE}
)
最后
以上就是拼搏滑板为你收集整理的cmake 简单使用的全部内容,希望文章能够帮你解决cmake 简单使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复