CMake Tutorial 笔记

CMakeLists.txt必要内容

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

# add the executable
add_executable(Tutorial tutorial.cxx)

添加版本

CMakeLists.txt
修改:
project(Tutorial)->project(Tutorial VERSION 1.0)
添加:
#configure a header file to pass the version number to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
#add that directory to the list of paths to search for include files
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" )
新建.h.in文件如下

TutorialConfig.h.in
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

则Tutorial_VERSION_MAJOR,Tutorial_VERSION_MINOR在C++源文件为宏定义

C++版本要求

CMakeLists.txt
添加:

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

添加库

├── build
├── CMakeLists.txt
├── MathFunctions
│ ├── CMakeLists.txt
│ ├── MathFunctions.h
│ └── mysqrt.cxx
├── TutorialConfig.h.in
└── tutorial.cxx

新建 MathFunctions/CMakeLists.txt

add_library(MathFunctions mysqrt.cxx)

CMakeLists.txt添加

# add the MathFunctions library
add_subdirectory(MathFunctions)
# 在add_executable(Tutorial tutorial.cxx)之后
target_link_libraries(Tutorial PUBLIC MathFunctions)

修改

target_include_directories(Tutorial PUBLIC
                          "${PROJECT_BINARY_DIR}"
                          "${PROJECT_SOURCE_DIR}/MathFunctions"
                          )

选择编译

CMakeLists.txt添加和修改

option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND EXTRA_LIBS MathFunctions)
  list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()

# add the executable
add_executable(Tutorial tutorial.cxx)

target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           ${EXTRA_INCLUDES}
                           )

cxx修改:

#ifdef USE_MYMATH
#  include "MathFunctions.h"
#endif
#ifdef USE_MYMATH
  const double outputValue = mysqrt(inputValue);
#else
  const double outputValue = sqrt(inputValue);
#endif

.h.in添加

#cmakedefine USE_MYMATH

shell命令
cmake ../Step2 -DUSE_MYMATH=OFF

库的范用化

MathFunctions/CMakeLists.txt
添加

target_include_directories(MathFunctions
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          )

INTERFACE means things that consumers require but the producer doesn't.也就是说,使用这个库的人才会使用这条编译指令。
而原本的CMakeLists.txt不用添加此库的目录,只用连接库。

安装

MathFunctions/CMakeLists.txt

install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)

CMakeLists.txt

install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  DESTINATION include
  )

Shell命令
cmake --install .
制定目录
cmake --install . --prefix "/home/myuser/installdir"

你可能感兴趣的:(CMake Tutorial 笔记)