Skip to content

CMake Tutorial

doe300 edited this page Mar 6, 2015 · 1 revision

Tutorial for CMake

# Source: http://www.cmake.org/cmake-tutorial/
# List of all commands: http://www.cmake.org/cmake/help/v3.0/manual/cmake-commands.7.html


####
# Basic configurations
####

cmake_minimum_required (VERSION 3.0)

# name of the project
project (Tutorial)


####
# Adding wildcards to replace in files
####

# set specific header-fields (here the version)
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)

# for this to work, we need a file containing the above placeholders, e.g.
#	-------- version.h.in --------
#	#define VERSION_MAJOR @VERSION_MAJOR@	//the @VERSION_MAJOR@ will be replaced by 1
#	#define VERSION_MINOR @VERSION_MINOR@	//the @VERSION_MINOR@ will be replaced by 0
#	------------------------------

# additionally we need to tell CMake where to find the file and where to write the result
configure_file (
	"${PROJECT_SOURCE_DIR}/version.h.in"
	"${PROJECT_BINARY_DIR}/version.h"
)

# this will take the version.h.in, fill the placeholders and saves it as version.h

# now, because we created the version.h in the binary-folder (the results-folder), we need to include it for compilation
include_diretories("${PROJECT_BINARY_DIR}")


####
# Setting the executable
####
# For an executable project, we need to specify the executable file
add_executable (tutorial.cpp)


####
# Adding Files
####
# files are added via "add_library"
add_library (Library library.cpp)	#will add the file "library.cpp" as "Library"

# include all headers from the "tutorial" folder
include_directories ("${PROJECT_SOURCE_DIR}/tutorial")

# add directory "tutorial" to be built
add_subdirectory (tutorial)
# this requires the folder "tutorial" to contain an own CMakeLists.txt

# Uses libraries when linking a target
target_link_libraries (Tutorial Library)	#will use "Library" when linking "Tutorial"


####
# Optional code
####
# Options can be toggled ON/OFF and can be selected in the CMake GUI!?!
option (USE_MY_LIBRARY "use my provided library" ON)

# This options then can be used to build switches in CMake
if(USE_MY_LIBRARY)
	# ... some code to include my own library
endif (USE_MY_LIBRARY)

# Also, options can be passed to the code by calling
#	-------- version.h.in --------
#	//... code from before
#	#cmakedefine USE_MY_LIBRARY	//Creates a #define USE_MY_LIBRARY with the value 1 (ON) or 0 (OFF)
#	------------------------------


####
# Installing (Building)
####
# Install generates installation rules for the build system
# a targte is either the excutable (here "Tutorial") or a library (i.e. "Library")
install (TARGETS Tutorial DESTINATION bin)	# creates the finished executable in "./bin"
install (FILES "${PROJECT_BINARY_DIR}/SomeHeader.h" DESTINATION include)	#Copies the header into "./include" to be included by other programs


####
# Testing
####
enable_testing()
# automatically adds tests to the project
add_test (ItWorks Tutorial)	#this test is called 'ItWorks' and simply runs the Tutorial executable

add_test (HasConnection Tutorial no-such-host.de)	#The third argument is passed to the program
# adding properties to the tests
set_tests_properties (HasConnection PROPERTIES WILL_FAIL true)	#The test fails -> test succeeded if the program fails

add_test (FastEnough Tutorial 127.0.0.1)
set_tests_properties (FastEnough PROPERTIES TIMEOUT 5)	#This test must now complete in under 5 seconds


####
# Platform dependant code
####
# check, if C function exists in system (or libraries)
check_function_exists (fork FORK_EXISTS)	#stores 1 in FORK_EXISTS if there is a funcition fork()
# depending on this value, if...endif can be constructed, i.e. to load some surrogate library or the value can be passed to a header (see above)


####
#Packing
####
# very dependant on packager/installer
Clone this wiki locally