Category:CMake
This programming language may be used to instruct a computer to perform a task.
Official website |
---|
Execution method: | Interpreted |
---|---|
Lang tag(s): | 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.
In process script mode (cmake -P myscript.cmake
), CMake runs the script but cannot create a project or any targets.
Example
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.
- Check if this system has clock_gettime() or gettimeofday(),
- 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)
- Allow C to #include <config.h>, if doing out-of-source build.
include_directories(${CMAKE_CURRENT_BINARY_DIR})
- 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
- 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} */
- cmakedefine HAVE_CLOCK_GETTIME
- cmakedefine HAVE_GETTIMEOFDAY</lang>
simpletime.c
<lang c>/* simpletime.c */
- include "config.h"
- if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETTIMEOFDAY)
- include <sys/time.h>
- endif
- include <stdio.h>
- include <time.h>
/*
* Simply print the current time. */
int main() {
- 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);
- 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);
- else
time_t sec; sec = time(NULL); printf("The time is %s", ctime(&sec));
- 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.
Subcategories
This category has the following 3 subcategories, out of 3 total.
@
- CMake examples needing attention (empty)
- CMake Implementations (empty)
- CMake User (5 P)
Pages in category "CMake"
The following 27 pages are in this category, out of 27 total.