CMake学习之路

一 为什么使用CMake

 CMake是一个比make更高级的编译配置工具,它可以根据不同平台、不同的编译器,生成相应的Makefile或者vcproj项目。通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程。CMake自动生成的Makefile不仅可以通过make命令构建项目生成目标文件,还支持安装(make install)、测试安装的程序是否能正确执行(make test,或者ctest)、生成当前平台的安装包(make package)、生成源码包(make package_source)、产生Dashboard显示数据并上传等高级功能,只要在CMakeLists.txt中简单配置,就可以完成很多复杂的功能,包括写测试用例。如果有嵌套目录,子目录下可以有自己的CMakeLists.txt。总之很强大!

二 CMake安装
安装很简单

三 CMake基本使用和语法

command (args ...)

command 是命令名,大小写无关(注意:变量是大小写相关的)

args 是参数,参数如果有空格,应该用双引号括起来

变量引用用${VAR}语法

set命令可以将多变量可以放在一起,合成为一个变量,如下所示

set(Foo a b c)
command(${Foo})=command(a b c)
command("${Foo}")=command("a b c")

CMake可以直接获取系统环境变量和Windows注册值。为了获取系统环境变量,需要语法

$ENV{VAR}

四 CMake中的Hello World

CMakeLists.txt文件中

project(Hello)
add_executable(Hello Hello.c)

这只是最简单的版本,下面还有一个复杂一些的版本

cmake_minimum_required (2.6)
project (HELLO)
set (HELLO_SRCS Hello.c File2.c File3.c)
if (win32)
    set (HELLO_SRCS ${HELLO_SRCS} WinSupport.c)
else ()
    set (HELLO_SRCS  ${HELLO_SRCS} UnixSupport.c)
endif ()

add_executable (Hello ${HELLO_SRCS})

#look for Tcl library
find_library (TCL_LIBRARY NAMES tcl tcl84 tcl83 tcl82 tcl80 PATHS /usr/lib /usr/local/lib)

if (TCL_LIBRARY)
    target_link_library (HELLO ${TCL_LIBRARY})
endif ()

五 Run Cmake

cd Hello

cmake ..

make 

六 CMake帮助文档

安装完之后,进入/usr/share/cmake

目录形式如下

.
├── completions
├── editors
│   ├── emacs
│   └── vim
├── Help
│   ├── command
│   ├── generator
│   ├── include
│   ├── manual
│   ├── module
│   ├── policy
│   ├── prop_cache
│   ├── prop_dir
│   ├── prop_gbl
│   ├── prop_inst
│   ├── prop_sf
│   ├── prop_test
│   ├── prop_tgt
│   ├── release
│   └── variable
├── include
├── Modules
│   ├── CMakeAddFortranSubdirectory
│   ├── Compiler
│   ├── CompilerId
│   ├── FindCUDA
│   ├── FortranCInterface
│   │   └── Verify
│   ├── IntelVSImplicitPath
│   ├── Internal
│   └── Platform
└── Templates
    └── Windows

其中需要注意的是Help目录和Modules目录,Help目录为帮助文档,Modulues目录为find_package指令的查找目录之一。

 cmake --help

我们可以查看cmake帮助,这个很全,比如显示cmake的指令

 cmake --help-command-list 
add_compile_options
add_custom_command
add_custom_target
add_definitions
add_dependencies
add_executable
add_library
add_subdirectory
add_test
aux_source_directory
break
build_command
build_name
cmake_host_system_information
cmake_minimum_required
cmake_policy
configure_file
continue
create_test_sourcelist
ctest_build
ctest_configure
ctest_coverage
ctest_empty_binary_directory
ctest_memcheck
ctest_read_custom_files
ctest_run_script
ctest_sleep
ctest_start
ctest_submit
ctest_test
ctest_update
ctest_upload
define_property
else
elseif
enable_language
enable_testing
endforeach
endfunction
endif
endmacro
endwhile
exec_program
execute_process
export
export_library_dependencies
file
find_file
find_library
find_package
find_path
find_program
fltk_wrap_ui
foreach
function
get_cmake_property
get_directory_property
get_filename_component
get_property
get_source_file_property
get_target_property
get_test_property
if
include
include_directories
include_external_msproject
include_regular_expression
install
install_files
install_programs
install_targets
link_directories
link_libraries
list
load_cache
load_command
macro
make_directory
mark_as_advanced
math
message
option
output_required_files
project
qt_wrap_cpp
qt_wrap_ui
remove
remove_definitions
return
separate_arguments
set
set_directory_properties
set_property
set_source_files_properties
set_target_properties
set_tests_properties
site_name
source_group
string
subdir_depends
subdirs
target_compile_definitions
target_compile_features
target_compile_options
target_include_directories
target_link_libraries
target_sources
try_compile
try_run
unset
use_mangled_mesa
utility_source
variable_requires
variable_watch
while
write_file

再如cmake定义的变量

cmake --help-variable-list
<PROJECT-NAME>_BINARY_DIR
<PROJECT-NAME>_SOURCE_DIR
<PROJECT-NAME>_VERSION
<PROJECT-NAME>_VERSION_MAJOR
<PROJECT-NAME>_VERSION_MINOR
<PROJECT-NAME>_VERSION_PATCH
<PROJECT-NAME>_VERSION_TWEAK
APPLE
BORLAND
BUILD_SHARED_LIBS
CMAKE_<CONFIG>_POSTFIX
CMAKE_<LANG>_ARCHIVE_APPEND
CMAKE_<LANG>_ARCHIVE_CREATE
CMAKE_<LANG>_ARCHIVE_FINISH
CMAKE_<LANG>_COMPILER
CMAKE_<LANG>_COMPILER_ABI
CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN
CMAKE_<LANG>_COMPILER_ID
CMAKE_<LANG>_COMPILER_LOADED
CMAKE_<LANG>_COMPILER_TARGET
CMAKE_<LANG>_COMPILER_VERSION
CMAKE_<LANG>_COMPILE_OBJECT
CMAKE_<LANG>_CREATE_SHARED_LIBRARY
CMAKE_<LANG>_CREATE_SHARED_MODULE
CMAKE_<LANG>_CREATE_STATIC_LIBRARY
CMAKE_<LANG>_FLAGS
CMAKE_<LANG>_FLAGS_DEBUG
CMAKE_<LANG>_FLAGS_MINSIZEREL
CMAKE_<LANG>_FLAGS_RELEASE
CMAKE_<LANG>_FLAGS_RELWITHDEBINFO
CMAKE_<LANG>_IGNORE_EXTENSIONS
CMAKE_<LANG>_IMPLICIT_INCLUDE_DIRECTORIES
CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES
CMAKE_<LANG>_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES
CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES
CMAKE_<LANG>_LIBRARY_ARCHITECTURE
CMAKE_<LANG>_LINKER_PREFERENCE
CMAKE_<LANG>_LINKER_PREFERENCE_PROPAGATES
CMAKE_<LANG>_LINK_EXECUTABLE
CMAKE_<LANG>_OUTPUT_EXTENSION
CMAKE_<LANG>_PLATFORM_ID
CMAKE_<LANG>_SIMULATE_ID
CMAKE_<LANG>_SIMULATE_VERSION
CMAKE_<LANG>_SIZEOF_DATA_PTR
CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS
CMAKE_<LANG>_VISIBILITY_PRESET
CMAKE_ABSOLUTE_DESTINATION_FILES
CMAKE_ANDROID_API
CMAKE_ANDROID_API_MIN
CMAKE_ANDROID_GUI
CMAKE_APPBUNDLE_PATH
CMAKE_AR
CMAKE_ARCHIVE_OUTPUT_DIRECTORY
CMAKE_ARGC
CMAKE_ARGV0
CMAKE_AUTOMOC
CMAKE_AUTOMOC_MOC_OPTIONS
CMAKE_AUTOMOC_RELAXED_MODE
CMAKE_AUTORCC
CMAKE_AUTORCC_OPTIONS
CMAKE_AUTOUIC
CMAKE_AUTOUIC_OPTIONS
CMAKE_BACKWARDS_COMPATIBILITY
CMAKE_BINARY_DIR
CMAKE_BUILD_TOOL
CMAKE_BUILD_TYPE
CMAKE_BUILD_WITH_INSTALL_RPATH
CMAKE_CACHEFILE_DIR
CMAKE_CACHE_MAJOR_VERSION
CMAKE_CACHE_MINOR_VERSION
CMAKE_CACHE_PATCH_VERSION
CMAKE_CFG_INTDIR
CMAKE_CL_64
CMAKE_COLOR_MAKEFILE
CMAKE_COMMAND
CMAKE_COMPILER_2005
CMAKE_COMPILER_IS_GNU<LANG>
CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY
CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY_<CONFIG>
CMAKE_CONFIGURATION_TYPES
CMAKE_CROSSCOMPILING
CMAKE_CTEST_COMMAND
CMAKE_CURRENT_BINARY_DIR
CMAKE_CURRENT_LIST_DIR
CMAKE_CURRENT_LIST_FILE
CMAKE_CURRENT_LIST_LINE
CMAKE_CURRENT_SOURCE_DIR
CMAKE_CXX_COMPILE_FEATURES
CMAKE_CXX_EXTENSIONS
CMAKE_CXX_STANDARD
CMAKE_CXX_STANDARD_REQUIRED
CMAKE_C_COMPILE_FEATURES
CMAKE_C_EXTENSIONS
CMAKE_C_STANDARD
CMAKE_C_STANDARD_REQUIRED
CMAKE_DEBUG_POSTFIX
CMAKE_DEBUG_TARGET_PROPERTIES
CMAKE_DISABLE_FIND_PACKAGE_<PackageName>
CMAKE_DL_LIBS
CMAKE_EDIT_COMMAND
CMAKE_ERROR_DEPRECATED
CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION
CMAKE_EXECUTABLE_SUFFIX
CMAKE_EXE_LINKER_FLAGS
CMAKE_EXE_LINKER_FLAGS_<CONFIG>
CMAKE_EXPORT_NO_PACKAGE_REGISTRY
CMAKE_EXTRA_GENERATOR
CMAKE_EXTRA_SHARED_LIBRARY_SUFFIXES
CMAKE_FIND_LIBRARY_PREFIXES
CMAKE_FIND_LIBRARY_SUFFIXES
CMAKE_FIND_NO_INSTALL_PREFIX
CMAKE_FIND_PACKAGE_NAME
CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY
CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY
CMAKE_FIND_PACKAGE_WARN_NO_MODULE
CMAKE_FIND_ROOT_PATH
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM
CMAKE_FRAMEWORK_PATH
CMAKE_Fortran_FORMAT
CMAKE_Fortran_MODDIR_DEFAULT
CMAKE_Fortran_MODDIR_FLAG
CMAKE_Fortran_MODOUT_FLAG
CMAKE_Fortran_MODULE_DIRECTORY
CMAKE_GENERATOR
CMAKE_GENERATOR_PLATFORM
CMAKE_GENERATOR_TOOLSET
CMAKE_GNUtoMS
CMAKE_HOME_DIRECTORY
CMAKE_HOST_APPLE
CMAKE_HOST_SYSTEM
CMAKE_HOST_SYSTEM_NAME
CMAKE_HOST_SYSTEM_PROCESSOR
CMAKE_HOST_SYSTEM_VERSION
CMAKE_HOST_UNIX
CMAKE_HOST_WIN32
CMAKE_IGNORE_PATH
CMAKE_IMPORT_LIBRARY_PREFIX
CMAKE_IMPORT_LIBRARY_SUFFIX
CMAKE_INCLUDE_CURRENT_DIR
CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE
CMAKE_INCLUDE_DIRECTORIES_BEFORE
CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE
CMAKE_INCLUDE_PATH
CMAKE_INSTALL_DEFAULT_COMPONENT_NAME
CMAKE_INSTALL_MESSAGE
CMAKE_INSTALL_NAME_DIR
CMAKE_INSTALL_PREFIX
CMAKE_INSTALL_RPATH
CMAKE_INSTALL_RPATH_USE_LINK_PATH
CMAKE_INTERNAL_PLATFORM_ABI
CMAKE_JOB_POOL_COMPILE
CMAKE_JOB_POOL_LINK
CMAKE_LIBRARY_ARCHITECTURE
CMAKE_LIBRARY_ARCHITECTURE_REGEX
CMAKE_LIBRARY_OUTPUT_DIRECTORY
CMAKE_LIBRARY_PATH
CMAKE_LIBRARY_PATH_FLAG
CMAKE_LINK_DEF_FILE_FLAG
CMAKE_LINK_DEPENDS_NO_SHARED
CMAKE_LINK_INTERFACE_LIBRARIES
CMAKE_LINK_LIBRARY_FILE_FLAG
CMAKE_LINK_LIBRARY_FLAG
CMAKE_LINK_LIBRARY_SUFFIX
CMAKE_MACOSX_BUNDLE
CMAKE_MACOSX_RPATH
CMAKE_MAJOR_VERSION
CMAKE_MAKE_PROGRAM
CMAKE_MAP_IMPORTED_CONFIG_<CONFIG>
CMAKE_MATCH_COUNT
CMAKE_MFC_FLAG
CMAKE_MINIMUM_REQUIRED_VERSION
CMAKE_MINOR_VERSION
CMAKE_MODULE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS_<CONFIG>
CMAKE_MODULE_PATH
CMAKE_NOT_USING_CONFIG_FLAGS
CMAKE_NO_BUILTIN_CHRPATH
CMAKE_NO_SYSTEM_FROM_IMPORTED
CMAKE_OBJECT_PATH_MAX
CMAKE_OSX_ARCHITECTURES
CMAKE_OSX_DEPLOYMENT_TARGET
CMAKE_OSX_SYSROOT
CMAKE_PARENT_LIST_FILE
CMAKE_PATCH_VERSION
CMAKE_PDB_OUTPUT_DIRECTORY
CMAKE_PDB_OUTPUT_DIRECTORY_<CONFIG>
CMAKE_POLICY_DEFAULT_CMP<NNNN>
CMAKE_POLICY_WARNING_CMP<NNNN>
CMAKE_POSITION_INDEPENDENT_CODE
CMAKE_PREFIX_PATH
CMAKE_PROGRAM_PATH
CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE
CMAKE_PROJECT_NAME
CMAKE_RANLIB
CMAKE_ROOT
CMAKE_RUNTIME_OUTPUT_DIRECTORY
CMAKE_SCRIPT_MODE_FILE
CMAKE_SHARED_LIBRARY_PREFIX
CMAKE_SHARED_LIBRARY_SUFFIX
CMAKE_SHARED_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS_<CONFIG>
CMAKE_SHARED_MODULE_PREFIX
CMAKE_SHARED_MODULE_SUFFIX
CMAKE_SIZEOF_VOID_P
CMAKE_SKIP_BUILD_RPATH
CMAKE_SKIP_INSTALL_ALL_DEPENDENCY
CMAKE_SKIP_INSTALL_RPATH
CMAKE_SKIP_INSTALL_RULES
CMAKE_SKIP_RPATH
CMAKE_SOURCE_DIR
CMAKE_STAGING_PREFIX
CMAKE_STANDARD_LIBRARIES
CMAKE_STATIC_LIBRARY_PREFIX
CMAKE_STATIC_LIBRARY_SUFFIX
CMAKE_STATIC_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS_<CONFIG>
CMAKE_SYSROOT
CMAKE_SYSTEM
CMAKE_SYSTEM_IGNORE_PATH
CMAKE_SYSTEM_INCLUDE_PATH
CMAKE_SYSTEM_LIBRARY_PATH
CMAKE_SYSTEM_NAME
CMAKE_SYSTEM_PREFIX_PATH
CMAKE_SYSTEM_PROCESSOR
CMAKE_SYSTEM_PROGRAM_PATH
CMAKE_SYSTEM_VERSION
CMAKE_TOOLCHAIN_FILE
CMAKE_TRY_COMPILE_CONFIGURATION
CMAKE_TWEAK_VERSION
CMAKE_USER_MAKE_RULES_OVERRIDE
CMAKE_USER_MAKE_RULES_OVERRIDE_<LANG>
CMAKE_USE_RELATIVE_PATHS
CMAKE_VERBOSE_MAKEFILE
CMAKE_VERSION
CMAKE_VISIBILITY_INLINES_HIDDEN
CMAKE_VS_DEVENV_COMMAND
CMAKE_VS_INTEL_Fortran_PROJECT_VERSION
CMAKE_VS_MSBUILD_COMMAND
CMAKE_VS_MSDEV_COMMAND
CMAKE_VS_NsightTegra_VERSION
CMAKE_VS_PLATFORM_NAME
CMAKE_VS_PLATFORM_TOOLSET
CMAKE_WARN_DEPRECATED
CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION
CMAKE_WIN32_EXECUTABLE
CMAKE_XCODE_ATTRIBUTE_<an-attribute>
CMAKE_XCODE_PLATFORM_TOOLSET
CPACK_ABSOLUTE_DESTINATION_FILES
CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY
CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION
CPACK_INCLUDE_TOPLEVEL_DIRECTORY
CPACK_INSTALL_SCRIPT
CPACK_PACKAGING_INSTALL_PREFIX
CPACK_SET_DESTDIR
CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION
CTEST_BINARY_DIRECTORY
CTEST_BUILD_COMMAND
CTEST_BUILD_NAME
CTEST_BZR_COMMAND
CTEST_BZR_UPDATE_OPTIONS
CTEST_CHECKOUT_COMMAND
CTEST_CONFIGURATION_TYPE
CTEST_CONFIGURE_COMMAND
CTEST_COVERAGE_COMMAND
CTEST_COVERAGE_EXTRA_FLAGS
CTEST_CURL_OPTIONS
CTEST_CVS_CHECKOUT
CTEST_CVS_COMMAND
CTEST_CVS_UPDATE_OPTIONS
CTEST_DROP_LOCATION
CTEST_DROP_METHOD
CTEST_DROP_SITE
CTEST_DROP_SITE_CDASH
CTEST_DROP_SITE_PASSWORD
CTEST_DROP_SITE_USER
CTEST_GIT_COMMAND
CTEST_GIT_UPDATE_CUSTOM
CTEST_GIT_UPDATE_OPTIONS
CTEST_HG_COMMAND
CTEST_HG_UPDATE_OPTIONS
CTEST_MEMORYCHECK_COMMAND
CTEST_MEMORYCHECK_COMMAND_OPTIONS
CTEST_MEMORYCHECK_SANITIZER_OPTIONS
CTEST_MEMORYCHECK_SUPPRESSIONS_FILE
CTEST_MEMORYCHECK_TYPE
CTEST_NIGHTLY_START_TIME
CTEST_P4_CLIENT
CTEST_P4_COMMAND
CTEST_P4_OPTIONS
CTEST_P4_UPDATE_OPTIONS
CTEST_SCP_COMMAND
CTEST_SITE
CTEST_SOURCE_DIRECTORY
CTEST_SVN_COMMAND
CTEST_SVN_OPTIONS
CTEST_SVN_UPDATE_OPTIONS
CTEST_TEST_TIMEOUT
CTEST_TRIGGER_SITE
CTEST_UPDATE_COMMAND
CTEST_UPDATE_OPTIONS
CTEST_UPDATE_VERSION_ONLY
CTEST_USE_LAUNCHERS
CYGWIN
ENV
EXECUTABLE_OUTPUT_PATH
LIBRARY_OUTPUT_PATH
MINGW
MSVC
MSVC10
MSVC11
MSVC12
MSVC14
MSVC60
MSVC70
MSVC71
MSVC80
MSVC90
MSVC_IDE
MSVC_VERSION
PROJECT_BINARY_DIR
PROJECT_NAME
PROJECT_SOURCE_DIR
PROJECT_VERSION
PROJECT_VERSION_MAJOR
PROJECT_VERSION_MINOR
PROJECT_VERSION_PATCH
PROJECT_VERSION_TWEAK
UNIX
WIN32
WINCE
WINDOWS_PHONE
WINDOWS_STORE
XCODE_VERSION

七 深入学习cmake

cmake实践

mastering cmake

cmake的帮助文档

 

 

  

posted @ 2017-04-12 20:20  IQIT  阅读(19887)  评论(1编辑  收藏  举报