Cmake language
Cmake documentation:https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html
Control flow:
if**() / elseif() / else() / endif()
foreach() / endforeach()
while() / endwhile()
break() / continue() / **return()
Comments starts with #
such as:
# I am a comment
Creating a CMake project
Cmake entry point:CMakeLists.txt
- 1、put it at your peoject’s root
- 2、check the CMake version and detects the compiler
- 3、Recurse in sub-folders of each executable or library
the example of CMakeList.txt
cmake_minimun_required(VERSION x.y)
project(<name>)
option(SECRET_FEATYRE "Enable secert feature" OFF)
add_subdirectory(mylib)
add_subdirectory(myexy)
Executable
add_executable(<name>
[WIN32] [MACOSX_BUNDLE]
[source1] [source2 ...])
Library
add_library(<name>
[WIN32] [MACOSX_BUNDLE]
[source1] [source2 ...])
Library types:
- SHARED:Shared library(.dll, .so, .dylib)
- STATIC:Static Library(.lib, .a)
- INTERFACE
- Virtual target, usually used for header only libraries
- Doesn’t show up in IDEs(no target or files)
Custom atrget
add_custom_target(<name>
[COMMAND command] [args]
[DEPENDS depends]
[WORKING_DIRECTORY dir])
For both executable and libraries
target_include_directories(<target>
[PUBLIC | INTERFACE | PRIVATE]
<DIR>)
target_compile_definition(<target>
[PUBLIC | INTERFACE | PRIVATE]
<name> = <value>)
target_compile_options(<target>
[PUBLIC | INTERFACE | PRIVATE]
<option>)
target_compile_definitions(mylib
PRIVATE FASTPATH =1)
target_compile_option(mylib
PRIVATE -Wall)
target_include_directories(<name>
[PUBLIC] [INTERFACE][PRIVATE]
<DIR>
Adding a dependency on an another target
target_link_libraries(<target>
[PUBLIC | INTERFACE | PRIVATE]
<lib>)
Example
target_link_library(myexe
PRIVATE mylib)
Three types of dependencies:
- PRIVATE :Options are not propagated to users of the library, but users to build the current target.
- INTERFACE :Options are propagated to users of the library, but not used to build the current target.
- PUBLIC :Options are propagated to users of the library and used to build the current target.