目录
1.python脚本
2.CMakeLists.txt
(1)file(MAKE_DIRECTORY [...])
3.调用生成的C++文件
4.配置
5.构建,生成源码
6.运行exe
????????能够在构建时生成源代码是实用开发人员工具箱中的一个强大功能,他们希望根据某些规则生成可能很长且具有竞争力的代码,同时避免在源代码存储库中显式跟踪生成的代码。例如,我们可以想象根据检测到的平台或体系结构生成不同的源代码。或者,我们可以使用Python的简单性,在构建时根据我们在配置步骤中收集的输入生成显式和高效的C++代码。
????????本文通过使用add_custom_command()函数执行python命令生成C++文件primes.hpp。
1.python脚本
????????generate.py 脚本接受两个命令行参数:一个整数范围和一个输出文件名。
"""
Generates C++ vector of prime numbers up to max_number
using sieve of Eratosthenes.
"""
import pathlib
import sys
#参数1
max_number = int(sys.argv[-2])
#参数2
output_file_name = pathlib.Path(sys.argv[-1])
numbers = range(2, max_number + 1)
is_prime = {number: True for number in numbers}
for number in numbers:
current_position = number
if is_prime[current_position]:
while current_position <= max_number:
current_position += number
is_prime[current_position] = False
#存放质数
primes = (number for number in numbers if is_prime[number])
code = """#pragma once
#include <vector>
const std::size_t max_number = {max_number};
std::vector<int> & primes() {{
static std::vector<int> primes;
{push_back}
return primes;
}}
"""
push_back = '\n'.join([' primes.push_back({:d});'.format(x) for x in primes])
output_file_name.write_text(
code.format(max_number=max_number, push_back=push_back))
2.CMakeLists.txt
????????注意python软件需要安装python3。
(1)file(MAKE_DIRECTORY [<directories>...])
file(MAKE_DIRECTORY [<directories>...])
????????在指定目录处创建子目录,如果它们的父目录不存在,也会创建它们的父目录。
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-03 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#检测Python解释器
find_package(PythonInterp QUIET REQUIRED)
#将生成的代码放在 ${CMAKE_CURRENT_BINARY_DIR}/generate 下
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/generated)
#质数上限
set(MAX_NUMBER "100" CACHE STRING "Upper bound for primes")
#定义一个自定义命令来生成头文件,它执行 generate.py 脚本,并接受 ${MAX_NUMBER} 和文件路径( ${CMAKE_CURRENT_BINARY_DIR}/generated/primes.hpp )作为参数
add_custom_command(
OUTPUT
${CMAKE_CURRENT_BINARY_DIR}/generated/primes.hpp
COMMAND
${PYTHON_EXECUTABLE} generate.py ${MAX_NUMBER} ${CMAKE_CURRENT_BINARY_DIR}/generated/primes.hpp
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS
generate.py
)
#定义可执行文件及其目标,包括目录和依赖关系
add_executable(example "")
target_sources(example
PRIVATE
example.cpp
${CMAKE_CURRENT_BINARY_DIR}/generated/primes.hpp
)
target_include_directories(example
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/generated
)
3.调用生成的C++文件
#include "primes.hpp"
#include <iostream>
#include <vector>
int main() {
std::cout << "all prime numbers up to " << max_number << ":";
for (auto prime : primes())
std::cout << " " << prime;
std::cout << std::endl;
return 0;
}
4.配置
5.构建,生成源码
生成的primes.hpp:
#pragma once
#include <vector>
const std::size_t max_number = 100;
std::vector<int> & primes() {
static std::vector<int> primes;
primes.push_back(2);
primes.push_back(3);
primes.push_back(5);
primes.push_back(7);
primes.push_back(11);
primes.push_back(13);
primes.push_back(17);
primes.push_back(19);
primes.push_back(23);
primes.push_back(29);
primes.push_back(31);
primes.push_back(37);
primes.push_back(41);
primes.push_back(43);
primes.push_back(47);
primes.push_back(53);
primes.push_back(59);
primes.push_back(61);
primes.push_back(67);
primes.push_back(71);
primes.push_back(73);
primes.push_back(79);
primes.push_back(83);
primes.push_back(89);
primes.push_back(97);
return primes;
}
6.运行exe
|