cmake_minimum_required(VERSION 2.8.2 FATAL_ERROR)
project("ProjName")
// 不推荐使用add_definitions来设置编译选项,作用如同cmake -D
add_definitions(
-std=c++11 # Or -std=c++0x
-Wall
-Wfatal-errors
# Other flags
)
// 一般通过下条语句设置编译选项
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wfatal-errors -fPIC")
//变量:CMAKE_PREFIX_PATH: Path used for searching by FIND_XXX(), with appropriate suffixes added。
set(CMAKE_PREFIX_PATH /path/path/path)
//头文件路径
include_directories(
include
other/include
)
//源文件路径,在子目录中
add_subdirectory (
someDirectory
src/otherDirecotry
)
//寻找某个目录中的所有源文件,格式:aux_source_directory(<dir> <variable>)
aux_source_directory(src _srcFiles)
//list操作
list(REMOVE_ITEM _srcFiles "src/f4.cpp") //从变量中去掉某一项
list(APPEND <list> <element> [<element> ...]) //添加某一项到变量中
//链接库目录
link_directories(directory1 directory2 ...)
//生成库文件
add_library(mylib [SHARED]
mylib.cpp
other.cpp
)
//生成可执行程序
add_executable(a.out
main.cpp
src/func.cpp
)
//链接函数库
target_link_libraries(a.out mylib)
//设置变量
set(someVariable "some string")
//打印消息
message(STATUS "some status ${someVariable}")
//检查编译器是否支持某一个编译选项
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
// cmake的交叉编译环境设置,创建文件toolchain.cmake,添加以下内容:
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)
# specify the cross compiler
SET(CMAKE_C_COMPILER /opt/arm/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /opt/arm/arm-linux-gnueabihf-g++)
# where is the target environment
SET(CMAKE_FIND_ROOT_PATH /opt/arm/install)
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
//If this file is named toolchain.cmake and is located in your home directory and you are building in the subdirectory build then you can do:
~/src$ cd build
~/src/build$ cmake -DCMAKE_TOOLCHAIN_FILE=~/toolchain.cmake ..
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-03 17:57:29