Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)copy! share!
posted in category Digital Despotism / Web Development
posted at 09. Jan '18

Cmake Notes

Put this into CMakeLists.txt file. Copied and adapted from various sources on the internet. Lot of time spent on this as documentation doesn’t include examples and people don’t ask that much stuff about cmake.

Build Project

usual mantra. to clean build files (analogue to make clean), just remove Debug directory.

to build Release build, run cmake .. -DRELEASE

mkdir Debug
cd Debug
cmake ..
# cmake .. -DDEBUG
# cmake .. -DRELEASE

Set C or C++ Compiler

# must be before project() command
SET(CMAKE_C_COMPILER /usr/local/Cellar/gcc@6/6.4.0/bin/gcc-6)
SET(CMAKE_CXX_COMPILER /usr/local/Cellar/gcc@6/6.4.0/bin/gcc-6)


# must be before project() command

note: build scripts should be compiler agnostic and should rely on a precise path of a compiler, so use this with care or for debug

Set C or C++ Compiler in Different Way

A classic way is also to set C and CXX environment variables which are respected by cmake

export C=gcc
export CXX=g++

Use Headers From Some Library Which Is Not Installed In System

include_directories(AFTER "/Users/pixiedust/Downloads/glibc-2.24")
include_directories(AFTER "/Users/pixiedust/Downloads/glibc-2.24/include")
include_directories(AFTER "/Users/pixiedust/Downloads/glibc-2.24/sysdeps/x86")
include_directories(AFTER "/Users/pixiedust/Downloads/glibc-2.24/sysdeps/generic")
include_directories(AFTER "/Users/pixiedust/Downloads/glibc-2.24/stdio-common")

List Include Path

lists additional paths for inclusion, analogues to -I in gcc, include_directories in cmake. Doesn’t list system dirs though

message("INCLUDE_DIRECTORIES is ${output}")

Show Content of a Variable


where CMAKE_CXX_FLAGS_DEBUG is the name of variable

Detect GCC or Clang

  # do stuff

Compiler Flags for GCC and Clang

aimed for maximum speed and code quality. copied from various sources from internet

set(CMAKE_CXX_FLAGS_COMMON "-pipe -pedantic -Wall -Wextra -Wshadow -Wno-missing-field-initializers
  -Wno-unused-parameter -Wold-style-definition -Wdeclaration-after-statement -Wmissing-declarations
  -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wmissing-noreturn -Wpointer-arith
  -Wcast-align -Wwrite-strings -Winline -Wformat-nonliteral -Wformat-security -Wswitch-enum
  -Winit-self -Wmissing-include-dirs -Wundef -Waggregate-return -Wnested-externs
  -Wunsafe-loop-optimizations -Winvalid-pch -Wcast-qual

Additional Flags for Clang

check everything

set(CMAKE_CXX_FLAGS_CLANG "-Wall -Wextra -Wmost -Weverything")

My Compiler Flags for Debug and Production Releases


CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE are predefined by cmake - Debug and Release builds.

Build .so file (shared object library)

add_library(nss-maria SHARED

set (nss-maria_VERSION_MAJOR 2)
set (nss-maria_VERSION_MINOR 1)
set (nss-maria_VERSION_PATCH 0)

set(nss-maria_VERSION ${nss-maria_VERSION_MAJOR}.${nss-maria_VERSION_MINOR}.${nss-maria_VERSION_PATCH})

set_target_properties(nss-maria PROPERTIES
  VERSION ${nss-maria_VERSION}

set library + version. copied from KDE5 build configuration. generates these files

lrwxrwxrwx  1 pixiedust  staff     17 Dec 27 20:19 libnss-maria.so -> libnss-maria.so.2
lrwxrwxrwx  1 pixiedust  staff     21 Dec 27 20:19 libnss-maria.so.2 -> libnss-maria.so.2.1.0
-rwxr-xr-x  1 pixiedust  staff  10616 Dec 27 20:19 libnss-maria.so.2.1.0

Add Comment