Cmake

From IGEP - ISEE Wiki

Jump to: navigation, search

Start Developing with CMAKE

The cross-platform, open-source make system. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice. CMake is quite sophisticated: it is possible to support complex environments requiring system configuration, pre-processor generation, code generation, and template instantiation.


Useful information

CMAKE;wiki page
Media:Cmake-material.pdf

Install cmake

Using Ubuntu 12.04 is very simple install it using apt-get as:

sudo apt-get install cmake cmake-gui

Create your cmake hello world

Manually Build

First we will create a new directory:

mkdir cmake_hello_world

Enter inside the directory and create your main.c file with the hello world code:

 
#include <stdio.h>

int main (int argn, char *argv[])
{
	printf("hello world\n");
	return 0;
}
after create your main.c file compile it with gcc as:
gcc main.c -o hello_world
and execute it with
./hello_world
hello world

CMAKE Build

first we must create the cmake configuration file named: CMakeLists.txt with this content:

PROJECT(Hello_World2)
ADD_EXECUTABLE(hello_World2 main.c)

Where: PROJECT: Project Name ADD_EXECUTABLE: Is the executable name and the source files

mcaro@ubuntu:~/Projects/test$ ls -al
total 16
drwx------ 2 mcaro mcaro 4096 Feb  9 02:52 .
drwxrwxr-x 3 mcaro mcaro 4096 Feb  8 14:19 ..
-rw-rw-r-- 1 mcaro mcaro   58 Feb  9 02:52 CMakeLists.txt
-rw-rw-r-- 1 mcaro mcaro   96 Feb  9 02:51 main.c
mcaro@ubuntu:~/Projects/test$

Now we're ready for execute cmake and check the options available:

mcaro@ubuntu:~/Projects/test$ cmake
cmake version 2.8.7
Usage

  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>

Options
  -C <initial-cache>          = Pre-load a script to populate the cache.
  -D <var>:<type>=<value>     = Create a cmake cache entry.
  -U <globbing_expr>          = Remove matching entries from CMake cache.
  -G <generator-name>         = Specify a makefile generator.
  -Wno-dev                    = Suppress developer warnings.
  -Wdev                       = Enable developer warnings.
  -E                          = CMake command mode.
  -i                          = Run in wizard mode.
  -L[A][H]                    = List non-advanced cached variables.
  --build <dir>               = Build a CMake-generated project binary tree.
  -N                          = View mode only.
  -P <file>                   = Process script mode.
  --find-package              = Run in pkg-config like mode.
  --graphviz=[file]           = Generate graphviz of dependencies.
  --system-information [file] = Dump information about this system.
  --debug-trycompile          = Do not delete the try_compile build tree.
                                Only useful on one try_compile at a time.
  --debug-output              = Put cmake in a debug mode.
  --trace                     = Put cmake in trace mode.
  --warn-uninitialized        = Warn about uninitialized values.
  --warn-unused-vars          = Warn about unused variables.
  --no-warn-unused-cli        = Do not warn about command line options.
  --check-system-vars         = Find problems with variable usage in system
                                files.
  --help-command cmd [file]   = Print help for a single command and exit.
  --help-command-list [file]  = List available listfile commands and exit.
  --help-commands [file]      = Print help for all commands and exit.
  --help-compatcommands [file]= Print help for compatibility commands.
  --help-module module [file] = Print help for a single module and exit.
  --help-module-list [file]   = List available modules and exit.
  --help-modules [file]       = Print help for all modules and exit.
  --help-custom-modules [file]= Print help for all custom modules and exit.
  --help-policy cmp [file]    = Print help for a single policy and exit.
  --help-policies [file]      = Print help for all policies and exit.
  --help-property prop [file] = Print help for a single property and exit.
  --help-property-list [file] = List available properties and exit.
  --help-properties [file]    = Print help for all properties and exit.
  --help-variable var [file]  = Print help for a single variable and exit.
  --help-variable-list [file] = List documented variables and exit.
  --help-variables [file]     = Print help for all variables and exit.
  --copyright [file]          = Print the CMake copyright and exit.
  --help,-help,-usage,-h,-H,/?= Print usage information and exit.
  --help-full [file]          = Print full help and exit.
  --help-html [file]          = Print full help in HTML format.
  --help-man [file]           = Print full help as a UNIX man page and exit.
  --version,-version,/V [file]= Show program name/version banner and exit.

Generators

The following generators are available on this platform:
  Unix Makefiles              = Generates standard UNIX makefiles.
  CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  Eclipse CDT4 - Unix Makefiles
                              = Generates Eclipse CDT 4.0 project files.
  KDevelop3                   = Generates KDevelop 3 project files.
  KDevelop3 - Unix Makefiles  = Generates KDevelop 3 project files.

Now we will select generate "Unix Makefiles" as:

mcaro@ubuntu:~/Projects/test$ cmake -G "Unix Makefiles"
-- The C compiler identification is GNU
-- The CXX 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
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mcaro/Projects/test
mcaro@ubuntu:~/Projects/test$ ls -al
total 44
drwx------ 3 mcaro mcaro  4096 Feb  9 02:53 .
drwxrwxr-x 3 mcaro mcaro  4096 Feb  8 14:19 ..
-rw-rw-r-- 1 mcaro mcaro 10805 Feb  9 02:53 CMakeCache.txt
drwxrwxr-x 6 mcaro mcaro  4096 Feb  9 02:53 CMakeFiles
-rw-rw-r-- 1 mcaro mcaro  1585 Feb  9 02:53 cmake_install.cmake
-rw-rw-r-- 1 mcaro mcaro    58 Feb  9 02:52 CMakeLists.txt
-rw-rw-r-- 1 mcaro mcaro    96 Feb  9 02:51 main.c
-rw-rw-r-- 1 mcaro mcaro  4701 Feb  9 02:53 Makefile

Cmake was create the Makefile for build your project and we can test it using make

mcaro@ubuntu:~/Projects/test$ make
Scanning dependencies of target hello_World2
[100%] Building C object CMakeFiles/hello_World2.dir/main.c.o
Linking C executable hello_World2
[100%] Built target hello_World2
mcaro@ubuntu:~/Projects/test$ ls -al
total 52
drwx------ 3 mcaro mcaro  4096 Feb  9 02:56 .
drwxrwxr-x 3 mcaro mcaro  4096 Feb  8 14:19 ..
-rw-rw-r-- 1 mcaro mcaro 10805 Feb  9 02:53 CMakeCache.txt
drwxrwxr-x 6 mcaro mcaro  4096 Feb  9 02:56 CMakeFiles
-rw-rw-r-- 1 mcaro mcaro  1585 Feb  9 02:53 cmake_install.cmake
-rw-rw-r-- 1 mcaro mcaro    58 Feb  9 02:52 CMakeLists.txt
-rwxrwxr-x 1 mcaro mcaro  7158 Feb  9 02:56 hello_World2
-rw-rw-r-- 1 mcaro mcaro    96 Feb  9 02:51 main.c
-rw-rw-r-- 1 mcaro mcaro  4701 Feb  9 02:53 Makefile
mcaro@ubuntu:~/Projects/test$ ./hello_World2 
Hello World

Its all !!