Skip to content

CMake Primer

aolivier23 edited this page May 11, 2021 · 3 revisions

What is CMake

CMake is a replacement for Makefiles aimed at c++ projects. It's what's known as a build system. If you have worked with MINERvA's Gaudi/AnaTool components, CMake does the same job as CMT. CMake has a few advantages over writing Makefiles yourself:

  1. It's platform independent. CMake's higher level commands are supposed to know how to do their jobs equally effectively on Ubuntu, OS X, and Windows 10. You don't need to worry about how a Windows user should cd into a build subdirectory.
  2. It has higher level commands. CMake macros do things like add_executable instead of calling g++ directly. CMake versions after version 3 are better about this than older versions.

Installing Just this Package with CMake

  1. Install dependencies
  2. Make a working directory. Mine is called MINERvA101_2021/
  3. Download the complete source from MinervaExpt: git clone https://github.com/MinervaExpt/MINERvA-101-Cross-Section.git src #Makes a src subdirectory for you
  4. Make a build directory: mkdir opt && cd opt && mkdir build && cd build && mkdir MINERvA-101-Cross-Section && cd MINERvA-101-Cross-Section #opt for optimized build as opposed to debug build
  5. Run cmake to generate a build system: cmake ../../../src -DCMAKE_INSTALL_PREFIX=`pwd`/.. -DCMAKE_BUILD_TYPE=Release
  6. Compile and install: make install

Debug builds

For debugging, produce a debug build by creating a second build directory and replacing step 5 with: cmake ../../src -DCMAKE_INSTALL_PREFIX=`pwd`/.. -DCMAKE_BUILD_TYPE=Debug. This will turn on assert()s and include debug symbols that gdb and valgrind use to tell you line numbers of memory errors. You probably want to rebuild each dependency in debug mode too to maximize helpfulness of the debugger.

Clone this wiki locally