Platform independent builds with Cmake
A wide variety of programming languages are used by engineers and scientists. Tie them all together (C, C++, C#, Cuda, Fortran, etc.) in a platform-independent and simple way using CMake or Meson. These high-level build systems generate low-level build system backend files for Ninja (strongly recommended) or Make.
Assume a single-file C++ program that uses the Math library and Boost for flexible command-line input of numerous parameters. Turn on additional compiler warnings to help avoid common coding pitfalls. Consider an example CMakeLists.txt for a C++ and Fortran project, line by line.
Language(s) selection:
project(zakharov CXX)
Naming a project facilitates packaging and installation.
CXX
is required to enable the hooks for the language(s) you used.
The most frequently used languages include
tag | language |
---|---|
C | C |
C# | C# |
CXX | C++ |
Fortran | Fortran |
Languages that aren’t built into Cmake such as Pascal can be added via custom Cmake modules.
Compiler options:
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
add_compile_options(-mtune=native -Wall -Wextra -Wpedantic -fexceptions -Warray-bounds)
endif()
-mtune=native
- use specialized optimizations for your particular CPU
-Wall -Wextra -Wpedantic -Warray-bounds
- turn on warnings for common programming mistakes
-fexceptions
- more detailed debug info with no speed penalty–enabled by default on Clang.
find_package(Boost REQUIRED COMPONENTS filesystem program_options)
We use Boost:
filesystem
- directory manipulation
program-options
- advanced command-line parsing
add_executable(zakh zakh.cpp)
target_link_libraries(zakh PRIVATE Boost::filesystem Boost::program_options)
target_compile_features(modules PRIVATE cxx_std_11)
This project requires C++11 features, so an old compiler not supporting C++11 will emit a configuration error.
zakh
- the exe file that will be created on compile, run with
./zakh
. zakh.cpp
- the files making up “zakh”
Compiling a simple project with CMake: It’s convenient to create a separate directory, typically build/
under your main code directory.
Let’s say your main code directory is ~/code/zakharov
, then do
# configure
cmake -B build/`
# build program
cmake --build build/ --parallel
# run program
./zakh
Let’s say you edit the code–rebuild and run by:
cmake --build build/ --parallel
./zakh
Normally you do not need to reconfigure CMake if just editing source code.
CMake alternatives include Meson.
Related: