You are on page 1of 8

Introduction

References

CMake
or how to make Makefiles automatically. . .
Simon Leblanc

October 23, 2014

Introduction

References

What is CMake?

CMake is a build system generator.

It can generate:
Makefiles
Ninja build files (faster alternative to Makefiles)
Xcode projects
Visual Studio projects
...

Introduction

References

Why should you use it?

Pros:

Cons:

Automated (find package)

Another language to learn

Cross-platform

Not a particularly nice one


(kind of like bash)

Open-source, gratis
Widespread, well
documented
Very flexible
GUI (useful on Windows)

Best for big projects.

Introduction

How does it work?


Write your CMake commands in CMakeLists.txt. Then run:
$ mkdir build && cd build
$ cmake ..
or
$ ccmake .. # for curses interface
$ cmake -G Xcode .. # to create an Xcode project
You can now edit, build and run as usual.
$ emacs ../main.cpp # Hack, hack...
$ make
$ ./myprog

References

Introduction

How does it work?


Write your CMake commands in CMakeLists.txt. Then run:
$ mkdir build && cd build
$ cmake ..
or
$ ccmake .. # for curses interface
$ cmake -G Xcode .. # to create an Xcode project
You can now edit, build and run as usual.
$ emacs ../main.cpp # Hack, hack...
$ make
$ ./myprog
To undo everything, just delete the build directory.

References

Introduction

References

Example CMakeLists.txt (1)


# My Duffing super-project
cmake_minimum_required(VERSION 2.8)
project(Duffing CXX)
# Find Boost
find_package(Boost REQUIRED program_options date_time)
include_directories(${Boost_INCLUDE_DIRS})
list(APPEND LIBRARIES ${Boost_LIBRARIES})
# Find HDF5
#find_package(HDF5 REQUIRED CXX)
#include_directories({HDF5_INCLUDE_DIRS})
#list(APPEND LIBRARIES {HDF5_LIBRARIES})

Introduction

References

Example CMakeLists.txt (2)


# Add sources and headers
set(SOURCES main.cpp duffing.cpp duffing.hpp)
# Create duffing target
add_executable(run_duffing ${SOURCES})
target_link_libraries(run_duffing ${LIBRARIES})

# Create test target


add_executable(test_duffing test.cpp duffing.cpp duffing.hp
target_link_libraries(test_duffing ${LIBRARIES})
# Set special flags for special files
set_property(SOURCE duffing.cpp PROPERTY
COMPILE_FLAGS "-ftemplate-depth=512")

Introduction

References

References

Homepage: http://www.cmake.org
Downloads: http://www.cmake.org/download/
Documentation:
http://www.cmake.org/cmake/help/v3.0/

You might also like