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