cmake

what's cmake

  1. CMakeLists.txt
  2. cmake PATH or ccmake PATH generate Makefile
  3. make

example for one file

In the fold Demo1

makeup CMakeLists.txt

1
2
3
4
5
6
7
8
# Version
cmake_minimum_required (VERSION 2.8)

# Project information
project (Demo1)

# generate object
add_executable(Demo main.cc)

complie project

cmake .

example for multi-files

1
2
3
4
5
6
7
./Demo2
|
+--- main.cc
|
+--- MathFunctions.cc
|
+--- MathFunctions.h
1
2
3
4
5
6
7
8
9
10
11
# VERSION
cmake_minimum_required (VERSION 2.8)

# Project information
project (Demo2)

# find source file save to DIR_SRCS
aux_source_directory(. DIR_SRCS)

# generate object
add_executable(Demo main.cc MathFunctions.cc) # add MathFunctions.cc
1
aux_source_directory(<dir> <variable>)
1
2
3
4
5
6
7
8
9
./Demo3
|
+--- main.cc
|
+--- math/
|
+--- MathFunctions.cc
|
+--- MathFunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
cmake_minimum_required (VERSION 2.8)

project (Demo3)

aux_source_directory(. DIR_SRCS)

# add subdirectory
add_subdirectory(math)

add_executable(Demo main.cc)

# add link lib
target_link_libraries(Demo MathFunctions)

complile options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cmake_minimum_required (VERSION 2.8)

project (Demo4)

configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)

# MathFunctions
option (USE_MYMATH
"Use provided math implementation" ON)

# MathFunctions
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/math")
add_subdirectory (math)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

aux_source_directory(. DIR_SRCS)

add_executable(Demo ${DIR_SRCS})
target_link_libraries (Demo ${EXTRA_LIBS})

install and test

add_test

support gbd

1
2
3
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

evironment check

CheckFunctionExists

version

1
2
set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)

install package

1
2
3
4
5
6
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE
"${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")
include (CPack)

move to cmake

  • autotools
    • am2cmake
  • qmake
    • qmake converter
  • visual studio