Cmake An Introduction
Cmake An Introduction
AN INTRODUCTION
Graduiertenkolleg EMS
Robert Jakob
Source Executable
I don‘t
You care
care
bar.cpp
foo.h
Executable
foo.cpp fb.cpp
You care
internet.lib internet.h
pde-solver.lib,
2.0 < Version <= 2.1.3
pde-solver.lib,
2.0 < Version <= 2.1.3
pde-solver.lib,
2.0 < Version <= 2.1.3
• qmake
Qt by Nokia‘s build system
• Scons
Python-based build system
• cmake
cross-plattform make system
• Platform independent
• Dependencies
• Libraries
Configuration
cmake Makefile
CMakeLists.txt
Files
Project files
Configuration
cmake Makefile
CMakeLists.txt
Files
Project files
Configuration
cmake
CMakeLists.txt
Makefile
Files
• make foo.exe
printf("%50s\n", buffer);
result = add(buffer);
printf("Result: %d\n", result);
fclose(tbz2File);
return 0;
}
fclose(tbz2File);
return 0;
}
• Linking
gcc -g adder.o main.o -lbz2 produces a.out
project(mygitness)
cmake_minimum_required(VERSION 2.6)
add_definitions(-Wall)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
Script execution
set(SOURCE
src/main.c
src/adder.c)
add_executable(cmakeexample ${SOURCE})
find_package (BZip2)
include_directories(${BZIP_INCLUDE_DIRS})
target_link_libraries (cmakeexample
${BZIP2_LIBRARIES})
project(mygitness)
cmake_minimum_required(VERSION 2.6) Preamble
add_definitions(-Wall)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(SOURCE
src/main.c
src/adder.c)
Source file definitions
add_executable(cmakeexample ${SOURCE})
Defining targets
find_package (BZip2)
include_directories(${BZIP_INCLUDE_DIRS})
target_link_libraries (cmakeexample
Libraries to link to
${BZIP2_LIBRARIES})
• Basic syntax
command(args...)
• Project definition
project (name [CXX] [C] [JAVA])
• Setting a variable
set(VARIABLE 2)
• Using a variable
${VARIABLE}
• Conditionals
if (FOO)
# comments
else (FOO)
# comments
endif (FOO)
• if(DEFINED var)
• if(EXISTS filename)
• if(EXISTS dirname)
if (TCL_LIBRARY)
target_link_library(fooexe ${TCL_LIBRARY})
endif ()
BZ2_find_package (BZip2)
include_directories(${BZIP_INCLUDE_DIRS})
target_link_libraries (cmakeexample
${BZIP2_LIBRARIES})
project(mygitness)
cmake_minimum_required(VERSION 2.6)
add_definitions(-Wall)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(SOURCE
src/main.c
src/adder.c)
add_executable(cmakeexample ${SOURCE})
find_package (BZip2)
include_directories(${BZIP_INCLUDE_DIRS})
target_link_libraries (cmakeexample
${BZIP2_LIBRARIES})
• Solution:
• make rebuild_cache
• Try: rm -R build-debug/
• Two possibilities:
set(SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_subdirectory("${PROJECT_SOURCE_DIR}/mymathmodule")
set(SOURCE
${SOURCE}
${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
PARENT_SCOPE
)
set(HEADERS
${HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
PARENT_SCOPE
)
• cmake -DCMAKE_BUILD_TYPE=Release
• User-definable options
• Shows up in GUI
option(BUILD_SPECIAL_PART „Build special part“ OFF)
$ cmake -DBUILD_SPECIAL_PART=ON
• Copy file from in_file to out_file and replace all variables with
their values:
configure_file(„{$PROJECT_SOURCE_DIR}/configure.h.in“
„{$PROJECT_BINARY_DIR}/configure.h“)
• Configure.h.in:
#cmakedefine BUILD_SPECIAL_PART
• Configure.h:
#define BUILD_SPECIAL_PART or /* #define BUILD_SPECIAL_PART */
• CPack
Installer creation
• CTest
Large test framework
• LaTeX
http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_CMake_to_build_LaTeX_documents.3F
• FAQ
http://www.cmake.org/Wiki/CMake_FAQ