码迷,mamicode.com
首页 > 其他好文 > 详细

cmake常用语句一览

时间:2015-07-31 12:57:34      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:cmake

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)

//引用环境变量
export FOO=/use/lib
$ENV{FOO}

//头文件路径
include_directories(
    include
    relative/path
    /absolute/path/
)

//链接库目录
link_directories(directory1 directory2 ...)

//链接函数库
target_link_libraries(a.out mylib ompl) //可以是cmake中的target,也可以是路径中的 libompl.so,即gcc 中 -lompl 参数

//源文件路径,在子目录中
add_subdirectory (
    someDirectory
    src/otherDirecotry
)

//寻找某个目录中的所有源文件,格式:aux_source_directory(<dir> <variable>)
aux_source_directory(src _srcFiles)

//生成库文件
add_library(mylib [SHARED|STATIC] 
  mylib.cpp
  other.cpp
)

//生成可执行程序
add_executable(a.out
    main.cpp 
    src/func.cpp
)

//设置变量
set(someVariable "some string")

//打印消息
message(STATUS "some status ${someVariable}")

//list操作
list(REMOVE_ITEM _srcFiles "src/f4.cpp")    //从变量中去掉某一项
list(APPEND <list> <element> [<element> ...])   //添加某一项到变量中

//检查编译器是否支持某一个编译选项
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_SOURCE_DIR( 相当于工程根目录 )
this is the directory, from which cmake was started, i.e. the top level source directory

CMAKE_CURRENT_SOURCE_DIR
this is the directory where the currently processed CMakeLists.txt is located in

CMAKE_CURRENT_BINARY_DIR
if you are building in-source, this is the same as CMAKE_CURRENT_SOURCE_DIR, otherwise this is the directory where the compiled or generated files from the current CMakeLists.txt will go to

PROJECT_SOURCE_DIR( =CMAKE_SOURCE_DIR 相当于工程根目录 )
contains the full path to the root of your project source directory, i.e. to the nearest directory where CMakeLists.txt contains the PROJECT() command

// 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 ..

版权声明:本文为博主原创文章,未经博主允许不得转载。

cmake常用语句一览

标签:cmake

原文地址:http://blog.csdn.net/gw569453350game/article/details/46683845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!