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 Systems Software / cmake
posted at 09. Jan '18
last updated at 12. May '21

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)

or

# must be before project() command
SET(CMAKE_C_COMPILER gcc)
SET(CMAKE_CXX_COMPILER gcc)

note: build scripts should be compiler agnostic and should not rely on a precise path to 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

GET_DIRECTORY_PROPERTY(output INCLUDE_DIRECTORIES)
message("INCLUDE_DIRECTORIES is ${output}")

Show Content of a Variable

message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")

where CMAKE_CXX_FLAGS_DEBUG is the name of variable

Detect GCC or Clang

if (CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  # do stuff
endif()

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

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} --std=c99 -g -ggdb -O0 ${CMAKE_CXX_FLAGS_COMMON}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} --std=c99 -Werror -O3 ${CMAKE_CXX_FLAGS_COMMON}")

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
  "nss/passwd.c"
)

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}
  SOVERSION ${nss-maria_VERSION_MAJOR}
)

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