|
1 | 1 | # mcpplibs templates |
2 | 2 |
|
3 | | -mcpplibs templates... |
| 3 | +> C++23 模块化库项目模板 - `import mcpplibs.templates;` |
4 | 4 |
|
5 | | -`src/templates.cppm` |
| 5 | +基于 C++23 模块的库项目模板,提供标准化的项目结构、构建配置和 CI/CD 流水线,帮助快速创建 mcpplibs 风格的模块化 C++ 库。 |
6 | 6 |
|
7 | | -```cpp |
8 | | -module; // 0 - global module declaration |
9 | | - |
10 | | -// 1 - include & macro area |
11 | | -// #include <stdio.h> |
| 7 | +## 特性 |
12 | 8 |
|
13 | | -// 2 - module declaration |
14 | | -export module mcpplibs.templates; |
15 | | - |
16 | | -// 3 - import area |
17 | | -import std; |
| 9 | +- **C++23 模块** — `import mcpplibs.templates;` |
| 10 | +- **双构建系统** — 同时支持 xmake 和 CMake |
| 11 | +- **CI/CD** — GitHub Actions 多平台构建(Linux / macOS / Windows) |
| 12 | +- **标准化结构** — 遵循 [mcpp-style-ref](https://github.com/mcpp-community/mcpp-style-ref) 编码规范 |
| 13 | +- **开箱即用** — 包含示例、测试和架构文档 |
18 | 14 |
|
19 | | -// 4 - module implementation partition |
20 | | -namespace mcpplibs { |
| 15 | +## 项目结构 |
21 | 16 |
|
22 | | - // 5 - exported entities |
23 | | - export void hello_mcpp() { |
24 | | - std::println("hello mcpp!"); |
25 | | - } |
26 | | - |
27 | | -} // namespace mcpplibs |
| 17 | +``` |
| 18 | +mcpplibs-templates/ |
| 19 | +├── src/ # 模块源码 |
| 20 | +│ └── templates.cppm # 主模块接口 |
| 21 | +├── tests/ # 测试 |
| 22 | +│ ├── main.cpp |
| 23 | +│ └── xmake.lua |
| 24 | +├── examples/ # 示例 |
| 25 | +│ ├── basic.cpp |
| 26 | +│ └── xmake.lua |
| 27 | +├── docs/ # 文档 |
| 28 | +│ └── architecture.md |
| 29 | +├── .github/workflows/ # CI/CD |
| 30 | +│ └── ci.yml |
| 31 | +├── xmake.lua # xmake 构建配置 |
| 32 | +├── CMakeLists.txt # CMake 构建配置 |
| 33 | +└── config.xlings # xlings 工具链配置 |
28 | 34 | ``` |
29 | 35 |
|
30 | | -`tests/main.cpp` |
| 36 | +## 快速开始 |
31 | 37 |
|
32 | 38 | ```cpp |
33 | | -// 6 - import module |
| 39 | +import std; |
34 | 40 | import mcpplibs.templates; |
35 | 41 |
|
36 | | -auto main() -> int { |
37 | | - // 7 - call exported function |
38 | | - mcpplibs::hello_mcpp(); |
| 42 | +int main() { |
| 43 | + mcpplibs::templates::hello_mcpp(); |
| 44 | + return 0; |
39 | 45 | } |
40 | 46 | ``` |
41 | 47 |
|
42 | | -## Install & Config |
| 48 | +## 安装与配置 |
43 | 49 |
|
44 | 50 | ```bash |
45 | 51 | xlings install |
46 | 52 | ``` |
47 | 53 |
|
48 | | -## Build & Run |
| 54 | +## 构建与运行 |
49 | 55 |
|
50 | | -**Using xmake** |
| 56 | +**使用 xmake** |
51 | 57 |
|
52 | 58 | ```bash |
53 | | -xmake build |
54 | | -xmake r |
| 59 | +xmake build # 构建库 |
| 60 | +xmake run basic # 运行基础示例 |
| 61 | +xmake run templates_test # 运行测试 |
55 | 62 | ``` |
56 | 63 |
|
57 | | -**Using CMake** |
| 64 | +**使用 CMake** |
58 | 65 |
|
59 | 66 | ```bash |
60 | 67 | cmake -B build -G Ninja |
61 | 68 | cmake --build build |
62 | | -./build/tests |
| 69 | +ctest --test-dir build |
| 70 | +``` |
| 71 | + |
| 72 | +## 集成到构建工具 |
| 73 | + |
| 74 | +### xmake |
| 75 | + |
| 76 | +```lua |
| 77 | +add_repositories("mcpplibs-index https://github.com/mcpplibs/mcpplibs-index.git") |
| 78 | + |
| 79 | +add_requires("templates") |
| 80 | + |
| 81 | +target("myapp") |
| 82 | + set_kind("binary") |
| 83 | + set_languages("c++23") |
| 84 | + add_files("main.cpp") |
| 85 | + add_packages("templates") |
| 86 | + set_policy("build.c++.modules", true) |
63 | 87 | ``` |
64 | 88 |
|
65 | | -## Other |
| 89 | +## 相关链接 |
66 | 90 |
|
| 91 | +- [mcpp-style-ref | 现代C++编码/项目风格参考](https://github.com/mcpp-community/mcpp-style-ref) |
| 92 | +- [mcpplibs/cmdline | 命令行解析库](https://github.com/mcpplibs/cmdline) |
| 93 | +- [mcpp社区官网](https://mcpp.d2learn.org) |
| 94 | +- [mcpp | 现代C++爱好者论坛](https://mcpp.d2learn.org/forum) |
67 | 95 | - [入门教程: 动手学现代C++](https://github.com/Sunrisepeak/mcpp-standard) |
68 | | -- [mcpp | 现代C++爱好者论坛](https://forum.d2learn.org/category/20) |
69 | | -- [mcpp-community | 现代C++爱好者社区](https://github.com/mcpp-community) |
70 | | -- [d2learn社区](https://github.com/d2learn) |
|
0 commit comments