Cmake sample

Minimum cmake sample. It will create an executable for sample.cpp.

Windows

Download cmake-gui from http://www.cmake.org/. Execute using path as follows

cmake-win2

Configure and Generate.Choose build chain (compiler, linker …). Remeber that Visual Studio 11 means Visual Studio 2012. It will create solution and project files in build directory. Open and use it.

Linux

cd to the directory and

1
2
3
$ cd build/
$ cmake ..
$ make

Also ccmake can be used to configure options and debug and release configurations (remember to use first char with upper case e.g. Debug)

The CMake files

The one at main dir

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cmake_minimum_required(VERSION 2.8)

set(BINARY_NAME "sample")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

#link static
set(BUILD_SHARED_LIBS OFF)


set(CMAKE_CXX_FLAGS_DEBUG "-D_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}")
if(WIN32)
    #windows linker settings
else(WIN32)
    #linux linker settings
    set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ ${CMAKE_EXE_LINKER_FLAGS}")
endif(WIN32)

if(CMAKE_COMPILER_IS_GNUCC)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --std=c++0x -funsigned-char -frounding-math -fsignaling-nans -ffloat-store -mfpmath=sse -msse2 -fmessage-length=0 -Wall -Wextra -Wno-unknown-pragmas -Wno-unused-variable -Wno-unused-parameter -Wno-comment")
endif(CMAKE_COMPILER_IS_GNUCC)
if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++0x -funsigned-char -frounding-math -fsignaling-nans -ffloat-store -mfpmath=sse -msse2 -fmessage-length=0 -Wall -Wextra -Wno-unknown-pragmas -Wno-unused-variable -Wno-unused-parameter -Wno-comment")
endif(CMAKE_COMPILER_IS_GNUCXX)

if(WIN32)
    #windows include dirs
else(WIN32)
    #linux include dirs
    include_directories(SYSTEM /usr/local/include)
endif(WIN32)

add_subdirectory(src)

The other at src

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
set(PART_NAME ${BINARY_NAME})

set(${PART_NAME}_SRC
    #sources here
    sample.cpp
)

include_directories(${PROJECT_SOURCE_DIR})
add_executable(${PART_NAME} ${${PART_NAME}_SRC})

if(WIN32)
    target_link_libraries(${PART_NAME}
    #   windows libraries
    )
else(WIN32)
    target_link_libraries(${PART_NAME}
    #   linux libraries
    )
endif(WIN32)


install(TARGETS ${PART_NAME} RUNTIME DESTINATION usr/bin)

The files

cmake-sample-files-ls
download here cmake-template.zip