(1)CMake入门笔记--CMake官网教程

CMake官网提供的入门教程。

CMake官网教程地址:https://cmake.org/cmake-tutorial/

一. 基本开始

1. 基本项目构建

最基本的项目是从源代码文件生成的可执行文件。 对于简单的项目,只需要两三行CMakeLists.txt文件。 这将是我们教程的开始。 CMakeLists.txt文件如下所示:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)

需要注意的是,此示例在CMakeLists.txt文件中使用小写命令。 CMake支持大小写、甚至大小写混合的命令。 tutorial.cxx的源代码是计算一个数字的平方根,其第一个版本非常简单,如下所示:

// tutorial.cxx
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
    if (argc < 2)
    {
        fprintf(stdout,"Usage: %s number\n",argv[0]);
        return 1;
    }
    double inputValue = atof(argv[1]);
    double outputValue = sqrt(inputValue);
    fprintf(stdout,"The square root of %g is %g\n",
            inputValue, outputValue);
    return 0;
}

2. 添加版本号并配置头文件

我们将添加的第一个功能是为我们的可执行文件和项目提供版本号。 虽然您可以在源代码中实现这样的功能,但在CMakeLists.txt文件中执行此操作可提供更大的灵活性。 要添加版本号,我们修改CMakeLists.txt文件,如下所示:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  )

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

# add the executable
add_executable(Tutorial tutorial.cxx)

由于配置文件将被写入二进制树结构,我们必须将该目录添加到路径列表中以搜索包含文件。 然后,我们在源代码树中创建一个TutorialConfig.h.in文件,其中包含以下内容:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

当CMake配置此头文件时,@ Tutorial_VERSION_MAJOR @和@ Tutorial_VERSION_MINOR @的值将替换为CMakeLists.txt文件中的值。 接下来,我们修改tutorial.cxx以包含配置的头文件并使用版本号。

最终的源代码如下所示:

// tutorial.cxx
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"

int main (int argc, char *argv[])
{
  if (argc < 2)
  {
    fprintf(stdout,"%s Version %d.%d\n",
            argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR);
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
  }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

主要的改变,在于将头文件TutorialConfig.h包含进去,并将版本号作为使用信息的一部分打印了出来。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要创建一个cmake-demo项目的入门教程,首先需要在根目录下创建一个名为CMakeLists.txt的文件,并创建一个名为hello的子目录。可以使用以下命令完成这些操作:mkdir cmake-demo && cd cmake-demo touch CMakeLists.txt mkdir hello 。 然后,在CMakeLists.txt文件中编写CMake命令来配置项目。CMake脚本文件由一系列CMake命令组成,通常以.cmake作为后缀名。你可以使用cmake命令来配置项目,例如:cmake -D <var>=<value> [options] <path/to/source> 。 最后,你可以在hello目录下添加源代码文件,例如一个简单的hello.cpp文件,然后使用CMake来生成构建系统的文件,例如Makefile或Visual Studio的项目文件。运行cmake命令来生成构建系统文件,然后使用构建系统来构建项目。 请注意,这只是一个简单的cmake-demo入门教程的概述,实际上还有很多更详细的内容可以涉及。你可以参考cmake的文档来获取更多的信息,例如使用cmake --help或man cmake命令来查看文档。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [CMake构建工具使用教程](https://blog.csdn.net/zzy979481894/article/details/129109513)[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: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值