Language
CMake
This programming language may be used to instruct a computer to perform a task.
Official website
Execution method: Interpreted
Lang tag(s): cmake


Listed below are all of the tasks on Rosetta Code which have been solved using CMake.

CMake is a cross-platform make system, for compiling C, C++ or Fortran programs. CMake uses a configuration file (CMakeLists.txt) to run configure tests, find libraries, create targets, and generate a build system for Unix make or another build tool.
This configuration file runs commands in CMake's own scripting language.

For Rosetta Code, most examples work with CMake 2.6 or later in process script mode (cmake -P myscript.cmake).

Many examples use features from 2.6, especially function().

In cmake -P mode, CMake runs the script but never creates a project. So there are no configure tests and no targets.

Simple project

This example builds a small C program after checking if #include <sys/time.h> provides clock_gettime() or gettimeofday().

CMakeLists.txt

<lang cmake># CMakeLists.txt cmake_minimum_required(VERSION 2.6) project(simpletime C) # This project will use the C compiler.

  1. Check if this system has clock_gettime() or gettimeofday(),
  2. then generate config.h with the results.

include(CheckSymbolExists) check_symbol_exists(clock_gettime sys/time.h HAVE_CLOCK_GETTIME) check_symbol_exists(gettimeofday sys/time.h HAVE_GETTIMEOFDAY) configure_file(config.h.in config.h)

  1. Allow C to #include <config.h>, if doing out-of-source build.

include_directories(${CMAKE_CURRENT_BINARY_DIR})

  1. This target will make an executable from the given
  2. source files and any included header files.

add_executable(simpletime simpletime.c)

  1. The install target will install the executable
  2. to ${CMAKE_INSTALL_PREFIX}/bin.

install(TARGETS simpletime DESTINATION bin)</lang>

config.h.in

<lang c>/* config.h, generated from config.h.in by CMake ${CMAKE_VERSION} */

  1. cmakedefine HAVE_CLOCK_GETTIME
  2. cmakedefine HAVE_GETTIMEOFDAY</lang>

simpletime.c

<lang c>/* simpletime.c */

  1. include "config.h"
  1. if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETTIMEOFDAY)
  2. include <sys/time.h>
  3. endif
  4. include <stdio.h>
  5. include <time.h>

/*

* Simply print the current time.
*/

int main() {

  1. if defined(HAVE_CLOCK_GETTIME)

struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); printf("The time is %s" "and %ld nanoseconds.\n", ctime(&ts.tv_sec), ts.tv_nsec);

  1. elif defined(HAVE_GETTIMEOFDAY)

struct timeval tv; gettimeofday(&tv, NULL); printf("The time is %s" "and %ld microseconds.\n", ctime(&tv.tv_sec), tv.tv_usec);

  1. else

time_t sec; sec = time(NULL); printf("The time is %s", ctime(&sec));

  1. endif

return 0; }</lang>

If the three files CMakeLists.txt, config.h.in and simpletime.c are in .., then cmake .. configures the project.

$ cmake ..
-- The C compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Looking for clock_gettime
-- Looking for clock_gettime - found
-- Looking for gettimeofday
-- Looking for gettimeofday - found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kernigh/field/rc/simpletime/work
$ make
Scanning dependencies of target simpletime
[100%] Building C object CMakeFiles/simpletime.dir/simpletime.c.o
Linking C executable simpletime
[100%] Built target simpletime
$ ./simpletime
The time is Wed Sep  7 17:10:55 2011
and 993848603 nanoseconds.