CMake Tutorial6 增加扩展命令和生成的文件

本文介绍了如何使用CMake的add_custom_command生成源文件,并将其加入到构建过程。通过示例展示了MathFunctions库如何利用MakeTable程序生成Table.h,然后在编译时链接到目标。此外,还涉及了系统自省检查log和exp函数是否存在,并根据情况添加编译定义。最终,通过构建并运行示例,验证了整个流程的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 内容
  1. 本文主要演示两项:(1) add_custom_command 的使用;(2) 如果在源代码中使用过程中生成的源文件。
  2. CMake Tutorial5 增加系统自省 基础上修改。
  3. 增加 MathFunctions/MakeTable.cxx 文件,内容如下:
// A simple program that builds a sqrt table
#include <cmath>
#include <fstream>
#include <iostream>

int main(int argc, char* argv[])
{
  // make sure we have enough arguments
  if (argc < 2) {
    return 1;
  }

  std::ofstream fout(argv[1], std::ios_base::out);
  const bool fileOpen = fout.is_open();
  if (fileOpen) {
    fout << "double sqrtTable[] = {" << std::endl;
    for (int i = 0; i < 10; ++i) {
      fout << sqrt(static_cast<double>(i)) << "," << std::endl;
    }
    // close the table with a zero
    fout << "0};" << std::endl;
    fout.close();
  }
  return fileOpen ? 0 : 1; // return 0 if wrote the file
}
  1. 更改 MathFunctions/CMakeLists.txt 文件
# --add
add_executable(MakeTable MakeTable.cxx)
# 将内容输出到Table.h
add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  DEPENDS MakeTable
  )

add_library(MathFunctions 
            mysqrt.cxx
            ${CMAKE_CURRENT_BINARY_DIR}/Table.h   # Table.h作为目标MathFunctions的源文件
            )

# state that anybody linking to us needs to include the current source dir
# to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # 增加Table.h所在路径为目标搜索路径
          )

# does this system provide the log and exp functions?
include(CheckSymbolExists)
check_symbol_exists(log "math.h" HAVE_LOG)
check_symbol_exists(exp "math.h" HAVE_EXP)
if(NOT (HAVE_LOG AND HAVE_EXP))
  unset(HAVE_LOG CACHE)
  unset(HAVE_EXP CACHE)
  set(CMAKE_REQUIRED_LIBRARIES "m")
  check_symbol_exists(log "math.h" HAVE_LOG)
  check_symbol_exists(exp "math.h" HAVE_EXP)
  if(HAVE_LOG AND HAVE_EXP)
    target_link_libraries(MathFunctions PRIVATE m)
  endif()
endif()

# add compile definitions
if(HAVE_LOG AND HAVE_EXP)
  target_compile_definitions(MathFunctions
                             PRIVATE "HAVE_LOG" "HAVE_EXP")
endif()
二 构建
  1. mkdir build
  2. cd build
  3. cmake …
  4. cmake --build .
  5. ./Tutorial 9
Use the table to help find an initial value 
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
Computing sqrt of 9 to be 3
The square root of 9 is 3
  1. 看下生成的Table.h文件
lee@leedeMacBook-Pro build % ls
CMakeCache.txt          Makefile                Tutorial 
cmake_install.cmake     CMakeFiles              MathFunctions 
TutorialConfig.h
lee@leedeMacBook-Pro build % cd MathFunctions 
lee@leedeMacBook-Pro MathFunctions % ls
CMakeFiles              MakeTable               Makefile           
Table.h                 cmake_install.cmake     libMathFunctions.a
lee@leedeMacBook-Pro MathFunctions % cat Table.h  
double sqrtTable[] = {
0,
1,
1.41421,
1.73205,
2,
2.23607,
2.44949,
2.64575,
2.82843,
3,
0};
lee@leedeMacBook-Pro MathFunctions % 
三 Github
  1. 已上传Github
四 参考
  1. cmake
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值