CMake笔记

Step1:基础起点

最基础的项目是由源代码构建的可执行文件。对于简单的项目,CMakeLists.txt只需要三行代码即可。

1.在Step1文件中创建一个CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.10)

project(Tutorial)

//生成可执行文件Tutorial
add_executable(Tutorial tutorial.cxx)

2.在Step1文件中,创建一个tutorial.cxx:

// 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

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 << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }

  // convert input to double
  // TODO 4: Replace atof(argv[1]) with std::stod(argv[1])
  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.首先,通过执行cmake可执行文件或cmake-gui来配置项目,然后使用选择的构建工具进行构建。

例如,通过命令行进入CMake源代码的目录中:Help/guide/tutorial,并创建一个构建文件夹:

mkdir Step1_build

2.接着,进入Step1_build文件夹,并且运行CMake配置项目和生成一个本地构建系统:

cd Step1_build
cmake -G "Visual Studio 15 2017 Win64" ..\Step1\

3.然后,调用构建系统编译或链接项目:

cmake --build .

添加版本号和配置头文件

添加的第一个特性就是可执行文件和项目的版本号。

虽然这些也可以在源代码中添加,但是在CMakeLists.txt中添加更加灵活。

  1. 修改CMakeLists.txt中的project()命令,明确项目的版本号:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)
  1. 配置头文件,把版本号传递给源代码:

configure_file(TutorialConfig.h.in TutorialConfig.h)

configure_file(<input> <output>)

configure_file会复制input file到output file,并替换其中用${VAR}和@VAR@包起来的CMake变量。

通常,会将输入文件定义为Version.h.in , 输出文件定义为Version.h。

因为配置的文件将被写到binary tree目录下(CMakeCache.txt所在的目录,即${PROJECT_BINARY_DIR}目录)所以我们必须将那个目录添加到include搜索路径列表中。

  1. 在CMakeLists.txt的最后添加上:

target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

  1. 在源代码目录下,新建一个文件TutorialConfig.h.in ,并添加下面代码:

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

指定C++标准

在CMake中指定C++标准,最简单的方法就是使用变量CMAKE_CXX_STANDARD。

  1. 在CMakeLists.txt中,在add_executable()前面,设置CMAKE_CXX_STANDARD为11,CMAKE_CXX_STANDARD_REQUIRED为True:

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)

重新构建

cd Step1_build
cmake --build .

Step2:添加库

添加一个库,这个库包含了求数值平方根的实现部分。那么可执行文件就直接调用库而不是使用编译器提供的求平方根的函数。

把库放到子目录MathFunctions中,这个目录包含了一个头文件MathFunctions.h和一个源文件mysqrt.cxx。这个源文件中有一个mysqrt()函数,这个函数类似编译器提供的sqrt()函数。

  1. 在MathFunctions文件夹的CMakeLists.txt中添加下面代码:

add_library(MathFunctions mysqrt.cxx)

为了使用新的库,需要在CMakeLists.txt的最上面,调用add_library(),使得库能够被构建。为了能够找到头文件MathFunctions.h,需要把MathFunctions目录添加到包含目录中。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kigha同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值