Key Concepts (Mastering CMake)

1 Key Concepts (Mastering CMake)

1.1 Main Structures

Keyword

targets, generators and commands, etc.

Source files -> Targets [EXE|LIB]

Directory, Local Generator, Global Generator

Main Structure

1) cmake

  1. controls the cmake process
  2. can be created and used in various GUIs

2) cmake has one cmGlobalGenerator

  1. abstract base class
  2. child classes responsible for platform-specific build process

3) cmGlobalGenerator has many cmLocalGenerator

  1. abstract base class
  2. child classes responsible for platform-specific build file generation

4) cmLocalGenerator has one cmMakefile

Parse CMakeLists.txt and store information from CMakeLists.txt file

  1. List of targets and variables
  2. Optional flags
  3. List of libaries
  4. List of include pathes

5) cmCommand

  1. abstract base class
  2. child classes responsible for implementing all commands in CMake

1.2 Targets

add_library, add_executable and add_custom_target

EX#1

add_library (foo STATIC foo1.c foo2.c)

EX#2

add_library (foo foo.cxx)
target_link_libraries (foo bar)

add_executable (foobar foobar.cxx)
target_link_libraries (foobar foo)

1.3 Source Files [TODO]

1.4 Directories, Generators, Tests, and Properties [TODO]

1.5 Variables and Cache Entries [TODO]

EX#1 Control Flow

set (FOO 1)

if (${FOO} LESS 2)
    set (FOO 2)
else (${FOO} LESS 2)
    set (FOO 3)
endif (${FOO} LESS 2)

EX#2 Var Scope

function (foo)
  message (${test}) # test is 1 here
  set (test 2)
  message (${test})
endfunction()

set (test 1)
foo()
message (${test})

EX#3 Parent Scrope

function (foo)
  message (${test})
  set (test 2 PARENT_SCOPE)
  message (${test)}
endfunction()

set (test 1)
foo()
message (${test}) # test will now be 2 here

EX#4 List

set (items_to_buy apple orange pear bear)

foreach (item ${items_to_buy})
  message ("Don't forget to buy one ${item}")
endforeach()

EX#5 Option

option (USE_JPEG "Do you want to use the JPEG library")

or

set (USE_JPEG ON CACHE BOOL "include jpge support?")

TYPES: BOOL, PATH, FILEPATH, STRING

1.6 CheatSheet

1.6.1 Useful command:

include_directories

add_subdirectory

add_library
add_executable

target_link_libraries

find_package
1.6.1.1 Generate && Build

Case#1 Visual Studio Win64

>> cd build
>> cmake -G "Visual Studio 14 2015 Win64" ..
>> cmake --build .

Case#2 Visual Studio Win32

>> cd build
>> cmake -G "Visual Studio 14 2015" ..
>> cmake --build .

Case#3 Build Specific Target

>> cmake --build . --target HelloWorld

1.7 Reference

[1] CMake 3.0.2 Documetation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值