Skip to content

Latest commit

 

History

History
167 lines (126 loc) · 5.15 KB

README.md

File metadata and controls

167 lines (126 loc) · 5.15 KB

CMAKE Samples

Description

CMake examples of daily use.

Samples

01-app

Single file to multiple files application alongwith setting up a specific version of CXX version.

  • 101:

    • A very basic cmake application.
  • 102:

    • A very basic cmake application with an option to set build type.
  • 103:

    • Hello world app to instruct where to install executables.
  • 104:

    • Build & install application having multiple source files.
  • 105:

    • Build & install application having multiple sources files + private header.
  • 106:

    • Build & install application having multiple sources files + private header + version file generated by cmake template.
  • 107:

    • A hello-world of std::cxx14.

02-library

Create static and shared libraries and how to use these libraries with application in different ways.

  • 201:

    • A static library using std::cxx14.
  • 202:

    • A shared library using std::cxx14.
    • Also see 211
  • 203:

    • An application using a static library (direct linking).
  • 204:

    • An application using a shared library (direct linking).
  • 205:

    • An application using a libary via .pc (using pkg-config)
    • pkg-config can be referred to static or shared library.
  • 206:

    • Generate .cmake file for a library (static).
  • 207:

    • An application using a static library (using .cmake).
    • TODO: Inherit compiler specific flags from library just like pkg-config can do that.
  • 208:

    • Generate .cmake file for a library (shared library).
  • 209:

    • An application using a shared library (using .cmake).
    • TODO: Inherit compiler specific flags from library just like pkg-config can do that.
  • 210:

    • A single project to build shared and static lib (pkg-config, .cmake).
  • 211:

    • An enhanced version of 202. Generator expressions are used.

03-external_libs

Use 3rd party libraries in an application.

  • 301:
    • A hello-world using cmake.BOOST.

04-misc

Miscellaneous usages.

  • 401:

    • Application packaging using cpack.
  • 402:

    • How to use cmake custom command and target interfaces.
      • add_custom_command links to different stages of build PRE_BUILD, PRE_LINK, POST_BUILD
      • Link add_custom_command with custom target.
  • 403:

    • ExternalProject: Create custom targets to build projects in external trees.
  • 404:

    • ExternalProject: Pull GoogleTest (gtest) as cmake external project.

TODOs

  • Linking of protobuf, flatbuffers and nanopb via cmake.

Tips

Set a variable

set(TEST_VAR "value")               # Overwrite old value if it set
set(TEST_VAR "${TEST_VAR} value")   # Append  new value to old

Print general info

message("-----------------------------------------------------------")
message("Lib Version            : ${VERSION_STRING}")
message("Compiler Id            : ${CMAKE_CXX_COMPILER_ID}")
message("Compiler (c) version   : ${CMAKE_C_COMPILER_VERSION}")
message("Compiler (cxx) version : ${CMAKE_CXX_COMPILER_VERSION}")
message("-----------------------------------------------------------")

Show all variables

##
message("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
# Get all variables as a list
get_cmake_property(varList VARIABLES)

# Filter variable to have only those starting with our required package
list(FILTER varList INCLUDE REGEX "^sample_lib*")
message("Variables associated to sample_lib are: ")
foreach (_variableName ${varList})
    message("    ${_variableName}=${${_variableName}}")
endforeach()
message("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
##

Generate targets

  • Show all targets generated by cmake for a project
cmake --build /path/to/build/folder --target help
  • Run a specific target
cmake --build /path/to/build/folder --target <target_name>

Generate compiler files

  • Save compiler intermediate files
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --save-temps")