最基本的项目是由源代码文件构建的可执行文件。
对于简单的项目,只需要三行 CMakeLists.txt
文件。
这将是我们教程的起点。在 step1
目录中创建一个 CMakeLists.txt
文件,该文件看起来像:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# 设置项目名为 Tutorial
project(Tutorial)
# 添加可执行文件
add_executable(Tutorial tutorial.cxx)
请注意,此示例在 CMakeLists.txt
文件中使用小写的命令。CMamke 本身是支持大写、小写、大小写混合的。
tutorial.cxx
的源代码如下,在 step1
目录中创建,可用于计算数字的平方根。
// 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;
}
构建和运行
这就是所需的一切 - 我们现在可以构建和运行我们的项目!
首先,运行 cmake
可执行文件或 cmake-gui
以配置项目,然后使用所选的构建工具进行构建。
例如,(如果您下载了源代码),我们可以从命令行导航到 CMake 源代码树的 Help/guide/tutorial
目录,并创建一个构建目录:
mkdir Step1_build
接下来,进入到该目录并运行 cmake
以配置项目并生成本机构建系统:
cd Step1_build
cmake ../Step1
然后调用该构建系统实际编译/链接项目:
cmake --build .
最后,尝试使用这些命令使用新构建的 Tutorial
(Tutorial 的生成路径和配置有关,默认可能在 Debug 目录下):
Tutorial 4294967296
Tutorial 10
Tutorial
添加版本号和配置的标头文件
我们将添加的第一个功能是为我们的可执行文件和项目提供版本号。虽然我们可以在源代码中专门执行此操作,但使用 CMakeLists.txt
提供了更大的灵活性。
首先,修改 CMakeLists.txt
文件以使用 project()
命令来设置项目名称和版本号。
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# 设置项目名和版本
project(Tutorial VERSION 1.0)
# 添加可执行文件
add_executable(Tutorial tutorial.cxx)
然后,配置头文件以将版本号传递给源代码:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# 设置项目名和版本
project(Tutorial VERSION 1.0)
# 添加可执行文件
add_executable(Tutorial tutorial.cxx)
configure_file(TutorialConfig.h.in TutorialConfig.h)
由于配置的文件将写入二叉树,因此我们必须将该目录添加到路径列表中以搜索包含文件。在 CmakeList.txt
的末尾添加以下行:
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
此时 CmakeList.txt
看起来是这样的:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# 设置项目名和版本
project(Tutorial VERSION 1.0)
# 添加可执行文件
add_executable(Tutorial tutorial.cxx)
configure_file(TutorialConfig.h.in TutorialConfig.h)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
使用您喜欢的编辑器,在源目录中创建 TutorialConfig.h.in
,并具有以下内容:
// 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@
的值。
下一个修改 tutorial.cxx
将 include 配置的头文件,TutorialConfig.h
。
最后,让我们通过更新 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>
#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
// 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;
}
指定 C++ 标准
接下来,让我们通过用 tutorial.cxx
中的 std::stod
替换 atof
,从而为我们的项目添加一些 C++11 的功能。同时,删除 #include <cstdlib>
。
// 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;
}
我们需要在 CMake 代码中显式声明它应该使用正确的标志。
在 CMake 中启用对特定 C++ 标准的支持的最简单方法是使用 CMAKE_CXX_STANDARD
变量。
对于本教程,请将 CMakeLists.txt
文件中的 CMAKE_CXX_STANDARD
变量设置为 11
,并将CMAKE_CXX_STANDARD_REQUIRED
设置为 True
。确保将 CMAKE_CXX_STANDARD
声明添加到对ADD_EXECUTABLE
的调用之上。
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# 设置项目名和版本
project(Tutorial VERSION 1.0)
# 指定 C++ 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# 添加可执行文件
add_executable(Tutorial tutorial.cxx)
configure_file(TutorialConfig.h.in TutorialConfig.h)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
重新构建
让我们重新构建我们的项目。我们已经创建了一个构建目录并运行了CMake,因此我们可以跳到构建步骤:
cd Step1_build
cmake --build .
现在,我们可以尝试使用与之前相同的命令来使用新构建的 Tutorial
:
Tutorial 4294967296
Tutorial 10
Tutorial
在没有任何参数的情况下运行可执行文件时,检查现在是否提示版本号。