CMake入门实战 (一)——Step 1: A Basic Starting Point


最基础的项目是直接由源码执行编译。对于简单的项目,一个三行代码的 CMakeLists.txt文件是所有项目所需要的。创建一个 CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

# add the executable
add_executable(Tutorial tutorial.cxx)

请注意,此示例在 CMakeLists.txt 文件中使用小写命令。 CMake 支持大写、小写和大小写混合命令。 tutorial.cxx 的源代码在 Step1 目录中提供,可用于计算数字的平方根。


tutorial.cxx:


// A simple program that computes the square root of a number
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  if (argc < 2) {
    std::cout << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }

  // convert input to double
  const double inputValue = atof(argv[1]);

  // calculate square root
  const double outputValue = sqrt(inputValue);
  std::cout << "The square root of " << inputValue << " is " << outputValue
            << std::endl;
  return 0;
}


1. Build and Run

使用cmake指令执行或者cmake-gui去配置项目并且通过我们所选择的构建工具去构建项目。

例如,我们可以在项目的目录下新建一个build文件夹:

mkdir Step1_build

然后,进入文件夹中,执行Cmake去配置项目并生成本机构建系统:

cd Step1_build
cmake ..

然后调用该构建系统来实际编译/链接项目:

cmake --build .
# 或者
make

最后,尝试通过以下命令使用新构建的教程:

Tutorial 4294967296
Tutorial 10
Tutorial

2. 添加一个版本号和配置头文件

你可以直接在源代码中添加版本号,但在 CMakeLists.txt 文件中提供版本号将会更加灵活,我们将文件修改如下:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

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

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# add the executable
add_executable(Tutorial tutorial.cxx)

# 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}"
                           )

因为配置文件将会写入到构建目录中,所以我们将这个目录添加到包含文件的搜索路径中。在源代码中添加 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_VERSION_MAJOR@ 中的Tutorial必须与 CMakeLists.txt中的项目名一致,否则会报错。接下来我们将头文件包含到 tutorial.cxx 中并且使用这个版本号,代码如下:

// A simple program that computes the square root of a number
#include <cmath>
#include <iostream>
#include <string>

#include "TutorialConfig.h"

int main(int argc, char* argv[])
{
  if (argc < 2) {
    // report version
    std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
              << Tutorial_VERSION_MINOR << std::endl;
    std::cout << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }

  // convert input to double
  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;
}

主要的改变是包含了头文件并且在使用方法信息中打印了版本号,测试:

./Tutorial

3. 使用VScode自动生成CMakeLists.txt和main.cpp

确保已经按照以下插件
在这里插入图片描述
在vscode中打开空文件夹chap1
在这里插入图片描述

ctrl+shift+p,选择cmake:Quick Start
在这里插入图片描述
输入项目名
在这里插入图片描述

如果要编译成可执行文件,则选择:Executable:
在这里插入图片描述
至此,即可自动生成了两个文件:
在这里插入图片描述
生成的文件模板如下
在这里插入图片描述
在这里插入图片描述
进一步地,如果我们希望VSCode 自动生成头文件的#ifndef #define #endif,可以参考文章:
VSCode 自动生成头文件的#ifndef #define #endif

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值