Translation CMake

This commit is contained in:
tangxin 2020-05-17 19:05:28 +08:00
parent f21a011eb4
commit 2f4158a7dd

View File

@ -0,0 +1,168 @@
---
category: tool
tool: cmake
contributors:
- ["Bruno Alano", "https://github.com/brunoalano"]
translators:
- ["tx23", "https://github.com/tx23"]
filename: CMake-cn
lang: zh-cn
---
CMake
CMake Makefile 跨平台的自动配置问题(不同的make解释器有不同的命令)
CMake
CMake使 CMakeLists.txt
使Unix makefiles Windows MSVC projects/workspaces
```cmake
# CMake ,
#
# - mkdir build && cd build
# - cmake ..
# - make
#
#
# Cmake Makefile
# make
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
#
# Cmake CMakeLists.txt
# MakefileCMake
cmake_minimum_required (VERSION 2.8)
# 2.8 FATAL_ERROR
cmake_minimum_required (VERSION 2.8 FATAL_ERROR)
# Cmake
#
project (learncmake C)
#
set( LEARN_CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
set( LEARN_CMAKE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
# semver
set (LEARN_CMAKE_VERSION_MAJOR 1)
set (LEARN_CMAKE_VERSION_MINOR 0)
set (LEARN_CMAKE_VERSION_PATCH 0)
#
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
#
# GCC, "-I"
include_directories( include )
# includes
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/modules/" )
#
if ( CONDITION )
#
#
message(STATUS "My message")
# CMake
message(WARNING "My message")
# CMake 警告 (dev)
message(AUTHOR_WARNING "My message")
# CMake
message(SEND_ERROR "My message")
# CMake
message(FATAL_ERROR "My message")
endif()
if( CONDITION )
elseif( CONDITION )
else( CONDITION )
endif( CONDITION )
#
foreach(loop_var arg1 arg2 ...)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
endforeach(loop_var)
foreach(loop_var RANGE total)
foreach(loop_var RANGE start stop [step])
foreach(loop_var IN [LISTS [list1 [...]]]
[ITEMS [item1 [...]]])
while(condition)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
endwhile(condition)
#
if(FALSE AND (FALSE OR TRUE))
message("Don't display!")
endif()
#
# PARENT_SCOPE
# `set(<variable> <value>... [PARENT_SCOPE])`
# How to reference variables inside quoted and unquoted arguments?
#
${variable_name}
#
#
set( LEARN_CMAKE_SOURCES
src/main.c
src/imagem.c
src/pather.c
)
#
#
# ${PROJECT_NAME} Learn_CMake
add_executable( ${PROJECT_NAME} ${LEARN_CMAKE_SOURCES} )
#
target_link_libraries( ${PROJECT_NAME} ${LIBS} m )
# includes
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/modules/" )
# 编译条件 (gcc ; g++)
if ( "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" )
message( STATUS "Setting the flags for ${CMAKE_C_COMPILER_ID} compiler" )
add_definitions( --std=c99 )
endif()
# OS
if( UNIX )
set( LEARN_CMAKE_DEFINITIONS
"${LEARN_CMAKE_DEFINITIONS} -Wall -Wextra -Werror -Wno-deprecated-declarations -Wno-unused-parameter -Wno-comment" )
endif()
```
###
+ [CMake tutorial](https://cmake.org/cmake-tutorial/)
+ [CMake documentation](https://cmake.org/documentation/)
+ [Mastering CMake](http://amzn.com/1930934319/)
+ [An Introduction to Modern CMake](https://cliutils.gitlab.io/modern-cmake/)