Contents
  1. 1. Build document
  2. 2. Build a single file
  3. 3. Build multiple files in sub directory
  4. 4. Reference

Go to make build easier.

Build document

  • CMakeLists.txt
    • like Makefile
  • add_executable(<name> source1 [source2 …])
    • Adds an executable target called <name> to be built from the source files listed in the command invocation.
    • The <name> corresponds to the logical target name and must be globally unique within a project.
      1
      2
      3
      * c++11 support
      - add_compile_options() will add the options to gcc and g++
      - set(CMAKE_CXX_FLAGS) works for g++ only

if(CMAKE_COMPILER_IS_GNUCXX)
# add_compile_options(-std=c++11)
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=c++11”)
message(STATUS “optional:-std=c++11”)

endif(CMAKE_COMPILER_IS_GNUCXX)

1
2
3
4
5
6
7
8
9
10

# single source file
add_executable(stest single_list.cpp)

# multiple source files
add_executable(main
create1.cpp
create2.cpp
create3.cpp
)

Build a single file

  • a single source file is going to be built into an executable binary
    1
    2
    3
    cmake_minimum_required(VERSION 2.8)

    add_executable(stest single_list.cpp)

Build multiple files in sub directory

  • serveral files in different sub direcotries, build them all in the root directory, and also could build each in a sub directory.

    1
    2
    3
    4
    5
    6
    7
    8
    .
    ├── CMakeLists.txt
    ├── dlist
    │   ├── CMakeLists.txt
    │   └── double_list.cpp
    └── slist
    ├── CMakeLists.txt
    └── single_list.cpp
  • slist CMakeLists.txt

    1
    2
    3
    cmake_minimum_required(VERSION 2.8)

    add_executable(stest single_list.cpp)
  • dlist CMakeLists.txt

    1
    2
    3
    cmake_minimum_required(VERSION 2.8)

    add_executable(dtest double_list.cpp)
  • folder root

    1
    2
    3
    4
    cmake_minimum_required(VERSION 2.8)

    add_subdirectory(slist)
    add_subdirectory(dlist)

Reference

Contents
  1. 1. Build document
  2. 2. Build a single file
  3. 3. Build multiple files in sub directory
  4. 4. Reference