CMake 学习笔记(生成头文件)

CMake 学习笔记(生成头文件)

经常,我们需要检测系统环境,然后来生成一些对应的头文件,这个头文件通常叫做“configured header file”。一般命名为 config.h。 CMake 有个 configure_file() 命令专门用来做这个事情。

下面我们给出一个简单的例子。这个例子来自与 cmake 官网上的 cmake tutorial 。网址如下:

https://cmake.org/cmake/help/latest/guide/tutorial/A%20Basic%20Starting%20Point.html

这个 cmake tutorial 篇幅不大,建议从这个教程入门。

这个例子是将软件的版本号传入了 TutorialConfig.h 。

代码如下:

// A simple program that computes the square root of a number
#include <cmath>
#include <cstdlib> // TODO 5: Remove this line
#include <iostream>
#include <string>

// TODO 11: Include TutorialConfig.h
#include "TutorialConfig.h"
int main(int argc, char* argv[])
{
  if (argc < 2) {
    // TODO 12: Create a print statement using Tutorial_VERSION_MAJOR
    //          and Tutorial_VERSION_MINOR
      std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
                << Tutorial_VERSION_MINOR << std::endl;
      std::cout << "Usage: " << argv[0] << " number" << std::endl;
      return 1;
  }
std::cout << CXX_COMPILER_ID << std::endl;

  // convert input to double
  // TODO 4: Replace atof(argv[1]) with std::stod(argv[1])
  //const double inputValue = atof(argv[1]);
    const double inputValue = std::stod(argv[1]);
  // calculate square root
  const double outputValue = sqrt(inputValue);
  std::cout << "The square root of " << inputValue << " is " << outputValue
            << std::endl;
  return 0;
}

可以看到 上面的源代码里使用到了 TutorialConfig.h。这个文件是 CMake 生成的。生成这个文件我们需要一个模板文件 TutorialConfig.h.in 。这个文件的内容如下:

// the configured options and settings for Tutorial
// TODO 10: Define Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#define CXX_COMPILER_ID "@CMAKE_CXX_COMPILER_ID@"

可以看到这个文件里有些 @…@ 。这些在生成 TutorialConfig.h 时会替换为真实的值。

CMakeLists.txt 文件如下:

# TODO 1: Set the minimum required version of CMake to be 3.10
cmake_minimum_required(VERSION 3.10)

# TODO 2: Create a project named Tutorial
project(Tutorial VERSION 1.0 LANGUAGES CXX)

# TODO 7: Set the project version number as 1.0 in the above project command

# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11
#         and the variable CMAKE_CXX_STANDARD_REQUIRED to True
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to
#         TutorialConfig.h
configure_file(TutorialConfig.h.in TutorialConfig.h)

# TODO 3: Add an executable called Tutorial to the project
# Hint: Be sure to specify the source file as tutorial.cxx
add_executable(Tutorial tutorial.cxx)

# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

里面 project(Tutorial VERSION 1.0 LANGUAGES CXX) 这句制定了软件的版本号。1.0 会被拆分成1 和 0 。 1 就是 MAJOR,CMake 会自动生成一个变量 Tutorial_VERSION_MAJOR 存放这个值,0 是MINOR,同理放在 Tutorial_VERSION_MINOR。

configure_file(TutorialConfig.h.in TutorialConfig.h)

这句告诉 cmake, 我们要根据 TutorialConfig.h.in 来生成 TutorialConfig.h。 注意这里的 TutorialConfig.h 会生成在 ${PROJECT_BINARY_DIR} 目录下。因此我们需要把这个目录添加到头文件搜索目录中。

# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

加上上面这句就可以编译执行了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CMakeLists.txt中指定添加文件路径的方法有两种。一种是使用include_directories函数,另一种是直接在CMakeLists.txt文件中写明路径。 使用include_directories函数的方法是在CMakeLists.txt文件中调用include_directories函数,并将需要添加的文件路径作为参数传入。例如,可以在函数中添加如下代码来指定文件路径: include_directories(路径1 路径2 ...) 另一种方法是直接在CMakeLists.txt文件中写明路径。你可以在CMakeLists.txt文件中找到以下形式的代码来指定文件路径: #include_directories(路径) 这两种方法都可以用来指定文件路径,具体选择哪一种方法取决于你的项目需求和个人喜好。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [CMake学习-添加文件路径,库路径,库](https://blog.csdn.net/snail_hunan/article/details/70238478)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [CMakeList添加指定路径的库和文件](https://blog.csdn.net/weixin_43466192/article/details/120240954)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值