CMake is a cross-platform, open-source build system. This tool allows you to test, compile, and create packages of your source code.
The problem that CMake tries to solve is the problem of Makefiles and Autoconfigure on cross-platforms (different make interpreters have different commands) and the ease-of-use on linking 3rd party libraries.
CMake is an extensible, open-source system that manages the build process in an operating system and compiler-agnostic manner. Unlike many cross-platform systems, CMake is designed to be used in conjunction with the native build environment. Simple configuration files placed in each source directory (called CMakeLists.txt files) are used to generate standard build files (e.g., makefiles on Unix and projects/workspaces in Windows MSVC) which are used in the usual way.
1# In CMake, this is a comment
2
3# To run our code, please perform the following commands:
4# - mkdir build && cd build
5# - cmake ..
6# - make
7#
8# With those steps, we will follow the best practice to compile into a subdir
9# and the second line will request to CMake to generate a new OS-dependent
10# Makefile. Finally, run the native Make command.
11
12#------------------------------------------------------------------------------
13# Basic
14#------------------------------------------------------------------------------
15#
16# The CMake file MUST be named as "CMakeLists.txt".
17
18# Setup the minimum version required of CMake to generate the Makefile
19cmake_minimum_required (VERSION 2.8)
20
21# Raises a FATAL_ERROR if version < 2.8
22cmake_minimum_required (VERSION 2.8 FATAL_ERROR)
23
24# We define the name of our project, and this changes some directories
25# naming convention generated by CMake. We can send the LANG of code
26# as the second param
27project (learncmake C)
28
29# Set the project source dir (just convention)
30set( LEARN_CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} )
31set( LEARN_CMAKE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} )
32
33# It's useful to set up the current version of our code in the build system
34# using a `semver` style
35set (LEARN_CMAKE_VERSION_MAJOR 1)
36set (LEARN_CMAKE_VERSION_MINOR 0)
37set (LEARN_CMAKE_VERSION_PATCH 0)
38
39# Send the variables (version number) to the source code header
40configure_file (
41 "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
42 "${PROJECT_BINARY_DIR}/TutorialConfig.h"
43)
44
45# Include Directories
46# In GCC, this will invoke the "-I" command
47include_directories( include )
48
49# Where are the additional libraries installed? Note: provide includes
50# path here, subsequent checks will resolve everything else
51set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/modules/" )
52
53# Conditions
54if ( CONDITION )
55 # Output!
56
57 # Incidental information
58 message(STATUS "My message")
59
60 # CMake Warning, continue processing
61 message(WARNING "My message")
62
63 # CMake Warning (dev), continue processing
64 message(AUTHOR_WARNING "My message")
65
66 # CMake Error, continue processing, but skip generation
67 message(SEND_ERROR "My message")
68
69 # CMake Error, stop processing and generation
70 message(FATAL_ERROR "My message")
71endif()
72
73if( CONDITION )
74
75elseif( CONDITION )
76
77else( CONDITION )
78
79endif( CONDITION )
80
81# Loops
82foreach(loop_var arg1 arg2 ...)
83 COMMAND1(ARGS ...)
84 COMMAND2(ARGS ...)
85 ...
86endforeach(loop_var)
87
88foreach(loop_var RANGE total)
89foreach(loop_var RANGE start stop [step])
90
91foreach(loop_var IN [LISTS [list1 [...]]]
92 [ITEMS [item1 [...]]])
93
94while(condition)
95 COMMAND1(ARGS ...)
96 COMMAND2(ARGS ...)
97 ...
98endwhile(condition)
99
100
101# Logic Operations
102if(FALSE AND (FALSE OR TRUE))
103 message("Don't display!")
104endif()
105
106# Set a regular, cache, or environment variable to a given value.
107# If the PARENT_SCOPE option is given, the variable will be set in the scope
108# above the current scope.
109# `set(<variable> <value>... [PARENT_SCOPE])`
110
111# How to reference variables inside quoted and unquoted arguments?
112# A variable reference is replaced by either the variable value or by the
113# empty string if the variable is not set.
114${variable_name}
115
116# Lists
117# Setup the list of source files
118set( LEARN_CMAKE_SOURCES
119 src/main.c
120 src/imagem.c
121 src/pather.c
122)
123
124# Calls the compiler
125#
126# ${PROJECT_NAME} refers to Learn_CMake
127add_executable( ${PROJECT_NAME} ${LEARN_CMAKE_SOURCES} )
128
129# Link the libraries
130target_link_libraries( ${PROJECT_NAME} ${LIBS} m )
131
132# Where are the additional libraries installed? Note: provide includes
133# path here, subsequent checks will resolve everything else
134set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMake/modules/" )
135
136# Compiler Condition (gcc ; g++)
137if ( "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" )
138 message( STATUS "Setting the flags for ${CMAKE_C_COMPILER_ID} compiler" )
139 add_definitions( --std=c99 )
140endif()
141
142# Check for OS
143if( UNIX )
144 set( LEARN_CMAKE_DEFINITIONS
145 "${LEARN_CMAKE_DEFINITIONS} -Wall -Wextra -Werror -Wno-deprecated-declarations -Wno-unused-parameter -Wno-comment" )
146endif()