https://www.cnblogs.com/chengxuyuancc/p/5347646.html

1. 利用configure_file配置的头文件


cmake可以通过可配置的头文件来产生实际的头文件,如下面的可配置头文件version.h.in,里面@@引用的变量可以通过CMakeLists.txt来设置,最后通过cmake来替换version.h.in文件中的变量并生成version.h内容。

  1. // version.h.in
  2. #pragma once
  3. #define BLACKWIDOW_MAJOR @BLACKWIDOW_MAJOR@
  4. #define BLACKWIDOW_MINOR @BLACKWIDOW_MINOR@
  5. #define BLACKWIDOW_PATCH @BLACKWIDOW_PATCH@
  1. // CMakeLists.txt
  2. # config version.h.in
  3. set(BLACKWIDOW_MAJOR 1)
  4. set(BLACKWIDOW_MINOR 0)
  5. set(BLACKWIDOW_PATCH 0)
  6. configure_file("${PROJECT_SOURCE_DIR}/include/blackwidow/version.h.in"
  7. "${PROJECT_SOURCE_DIR}/include/blackwidow/version.h")
  1. check_xxx函数检测系统环境生成HAVE_XXX系列宏定义
需要include 函数使用 使用例子
include(CheckIncludeFile) check_include_file check_include_file(“unistd.h” HAVE_UNISTD_H)
include(CheckLibraryExists) check_library_exists check_library_exists(crc32c crc32c_value “” HAVE_CRC32C)
check_library_exists(tcmalloc malloc “” HAVE_TCMALLOC)
check_library_exists(snappy snappy_compress “” HAVE_SNAPPY)
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(fdatasync “unistd.h” HAVE_FDATASYNC)
check_cxx_symbol_exists(F_FULLFSYNC “fcntl.h” HAVE_FULLFSYNC)
check_cxx_symbol_exists(O_CLOEXEC “fcntl.h” HAVE_O_CLOEXEC)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wthread-safety HAVE_CLANG_THREAD_SAFETY)
include(CheckCXXSourceCompiles)
# Test whether C++17 has_include is available.
check_cxx_source_compiles(“
#if defined(
has_include) && __has_include()
#include
#endif
int main() { std::string str; return 0; }
“ HAVE_CXX17_HAS_INCLUDE)
include (CheckFunctionExists) check_function_exists (printf HAVE_PRINTF) check_function_exists (printf HAVE_PRINTF)