{{language |exec=interpreted |site= |hopl=no |tags=cmake}} [[Category:Utility]]

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 [[make|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
cmake_minimum_required(VERSION 2.6)
project(simpletime C)  # This project will use the C compiler.

# Check if this system has clock_gettime() or gettimeofday(),
# then generate config.h with the results.
check_symbol_exists(clock_gettime sys/time.h HAVE_CLOCK_GETTIME)
check_symbol_exists(gettimeofday sys/time.h HAVE_GETTIMEOFDAY)
configure_file( config.h)

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

# This target will make an executable from the given
# source files and any included header files.
add_executable(simpletime simpletime.c)

# The install target will install the executable
install(TARGETS simpletime DESTINATION bin)

/* config.h, generated from by CMake ${CMAKE_VERSION} */


/* simpletime.c */
#include "config.h"

#include <sys/time.h>
#include <stdio.h>
#include <time.h>

 * Simply print the current time.
	struct timespec ts;
	clock_gettime(CLOCK_REALTIME, &ts);
	printf("The time is %s" "and %ld nanoseconds.\n",
	    ctime(&ts.tv_sec), ts.tv_nsec);
#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);
	time_t sec;
	sec = time(NULL);
	printf("The time is %s", ctime(&sec));
	return 0;

If the three files CMakeLists.txt, 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.