wiki:Tools/CMake

CMake

An ancient C++ build tool. It generates Makefiles or build.ninjas from predefined templates.

Installing (you have to have cmake from your distro)

git clone --depth=1 --recourse-submodules --shallow-submodules \ 
          https://gitlab.kitware.com/cmake/cmake.git
cd cmake

cmake \
    -DBUILD_CONFIG=Release \
    -DINSTALL_LAYOUT=STANDALONE \
    -DCC=clang -DCXX="clang++ -stdlib=libc++" \
    -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
    -DCMAKE_C_FLAGS="-m64 -march=native -mtune=native -O3" \
    -DCMAKE_CXX_FLAGS="-m64 -march=native -mtune=native -O3 -std=c++17 -stdlib=libc++" \
    -DCMAKE_C_LINK_FLAGS="-rtlib=compiler-rt -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack" \
    -DCMAKE_CXX_LINK_FLAGS="-rtlib=compiler-rt -stdlib=libc++ -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack" \
    -DCMAKE_LD_FLAGS="-rtlib=compiler-rt -stdlib=libc++" \
    .

make -s all

A minimal modern C++ project

Will work with any LSP enabled editor, such as Emacs or Vim or VSCode.

mkdir foo
cd foo
vi CMakeLists.txt

add this minimal but complete configuration, which explicitly forces Clang and libc++ (omit any CMAKE_CXX and CMAKE_CXX_FLAGS for the default cross-platform behavior)

project(Foo)
add_executable(a.out fac.cc)

set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_ARG1 "-stdlib=libc++")

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -Wextra -pedantic -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp=libomp")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -march=native -mtune=native")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og -g -ggdb")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fopenmp=libomp")

# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


cmake_minimum_required(VERSION 3.10.1)

and then

mkdir build
cd build
cmake ..
make

This will generate build/compile_commands.json which some LSP tools require.


the minimal CMakeLists.txt is this

project(Foo)
add_executable(a.out foo.cpp) # set the executable name and the list of source files.

set(CMAKE_CXX_STANDARD 17)

# -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

cmake_minimum_required(VERSION 3.10.1)
Last modified 15 months ago Last modified on Aug 31, 2019, 8:17:40 AM
Note: See TracWiki for help on using the wiki.