【 本文翻译自 CMake官网教程,整理一些示例程序作为自己学习笔记】
一、开始
最基本的项目是从源代码文件构建的可执行文件。对于简单项目,只需要一个三行文件即可。这将是我们教程的起点。在目录中创建一个文件,如下所示:CMakeLists.txt
# 最低要求版本
cmake_minimum_required(VERSION 3.10)
# 设置项目名称
project(Tutorial)
# 添加可执行程序
add_executable(Tutorial tutorial.cxx)
注意,此示例在文件中使用小写命令。CMake支持大写、小写和大小写混合命令。
示例程序:
test.cpp
//#########################################################################
//# File Name: test.cpp
//# Author:
//# mail:
//# Created Time: 2022年07月12日 星期二 16时04分51秒
//#########################################################################
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
std::cout << "Hello World!" << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# project name
project(test VERSION 1.0)
# executable name and the source code
add_executable(test test.cpp)
执行编译
mkdir build
cd build
cmake ..
make
编译和执行结果
二、添加版本号和配置的头文件
添加的第一个功能是为可执行文件和项目提供版本号,CMakeLists.txt有更大的灵活性。首先修改文件,使用project命令设置项目名称和版本号:
cmake_minimum_required(VERSION 3.10)
# 设置项目名称和版本号
project(Tutorial VERSION 1.0)
然后,配置文件将被写入二叉树,因此我们必须将该目录添加到路径列表中以搜索包含文件。将以下行添加到CMakeLists.txt文件末尾:
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@ 的值将被替换。
【编译时会根据TutorialConfig.h.in文件生成TutorialConfig.h,也可以手写TutorialConfig.h】
【@Tutorial_VERSION_MAJOR@ 和 @Tutorial_VERSION_MINOR@ 的值是根据项目名称和版本号自动填充的,比如project(Tutorial VERSION 3.1),那么Tutorial_VERSION_MAJOR和Tutorial_VERSION_MINOR分别是3和1】
接下来修改 tutorial.cxx 以包含配置的头文件 TutorialConfig.h。
最后,让我们通过更新tutorial.cxx来打印出可执行名称和版本号,如下所示:
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;
}
示例程序
test.cpp
//#########################################################################
//# File Name: test.cpp
//# Author:
//# mail:
//# Created Time: 2022年07月12日 星期二 16时04分51秒
//#########################################################################
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include "testConfig.h"
using namespace std;
int main(int argc, char* argv[])
{
if(argc < 2)
{
std::cout << argv[0] << " Version: " << test_VERSION_MAJOR << "." << test_VERSION_MINOR << std::endl;
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}
// convert input to double
const double inputValue = atof(argv[1]);
// caculate square root
const double outputValue = sqrt(inputValue);
std::cout << "The squart root of " << inputValue << " is " << outputValue << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# project name
project(test VERSION 3.1)
# executable name and the source code
add_executable(test test.cpp)
configure_file(testConfig.h.in testConfig.h)
target_include_directories(test PUBLIC "${PROJECT_BINARY_DIR}")
testConfig.h.in
// the configured options and settings for test
#define test_VERSION_MAJOR @test_VERSION_MAJOR@
#define test_VERSION_MINOR @test_VERSION_MINOR@
编译和执行结果
编译过程生成testConfig.h
三、指定C++标准
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)
使用标志位CMAKE_CXX_STANDARD_REQUIRED和CMAKE_CXX_STANDARD指定编译器的使用版本,如果CMAKE_CXX_STANDARD_REQUIRED设置为True,则必须使用CMAKE_CXX_STANDARD指定的版本,如果CMAKE_CXX_STANDARD_REQUIRED设置为OFF则CMAKE_CXX_STANDARD指定版本的为首选版本,如果没有会使用上一版本。
也可以使用CMAKE_CXX_FLAGS指定C++标准。