Skip to content

Commit 4580868

Browse files
优化模板项目结构,参考 cmdline 项目和 mcpp-style-ref 编码规范
- 重构 src/templates.cppm,遵循 mcpp-style-ref 模块文件结构规范 - 改进 xmake.lua,添加模块策略和子目录 includes - 新增 tests/xmake.lua 独立测试构建配置,集成 gtest - 新增 examples/ 目录,包含基础示例 - 新增 .github/workflows/ci.yml 多平台 CI(Linux/macOS/Windows) - 新增 docs/architecture.md 架构文档 - 更新 CMakeLists.txt 同步通配符模式 - 完善 .gitignore 和 README.md Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 199066e commit 4580868

11 files changed

Lines changed: 507 additions & 66 deletions

File tree

.github/workflows/ci.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
9+
jobs:
10+
build-linux:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Setup xmake
17+
uses: xmake-io/github-action-setup-xmake@v1
18+
with:
19+
xmake-version: latest
20+
package-cache: true
21+
22+
- name: Install dependencies
23+
run: |
24+
sudo apt-get update
25+
sudo apt-get install -y build-essential
26+
27+
- name: Install Xlings
28+
run: curl -fsSL https://d2learn.org/xlings-install.sh | bash
29+
30+
- name: Install GCC 15.1 with Xlings
31+
run: |
32+
export PATH=/home/xlings/.xlings_data/bin:$PATH
33+
xlings install gcc@15.1 -y
34+
35+
- name: Build
36+
run: |
37+
export PATH=/home/xlings/.xlings_data/bin:$PATH
38+
xmake -y -vv
39+
40+
- name: Test
41+
run: |
42+
export PATH=/home/xlings/.xlings_data/bin:$PATH
43+
xmake run templates_test
44+
45+
- name: Run examples
46+
run: |
47+
export PATH=/home/xlings/.xlings_data/bin:$PATH
48+
xmake run basic
49+
50+
build-macos:
51+
runs-on: macos-latest
52+
steps:
53+
- name: Checkout
54+
uses: actions/checkout@v4
55+
56+
- name: Setup xmake
57+
uses: xmake-io/github-action-setup-xmake@v1
58+
with:
59+
xmake-version: latest
60+
package-cache: true
61+
62+
- name: Install LLVM 20
63+
run: brew install llvm@20
64+
65+
- name: Build
66+
run: |
67+
xmake f --toolchain=llvm --sdk=/opt/homebrew/opt/llvm@20
68+
xmake -y -vv
69+
70+
build-windows:
71+
runs-on: windows-latest
72+
steps:
73+
- name: Checkout
74+
uses: actions/checkout@v4
75+
76+
- name: Setup xmake
77+
uses: xmake-io/github-action-setup-xmake@v1
78+
with:
79+
xmake-version: latest
80+
package-cache: true
81+
82+
- name: Build
83+
run: xmake -y -vv
84+
85+
- name: Test
86+
run: xmake run templates_test
87+
88+
- name: Run examples
89+
run: xmake run basic

.gitignore

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1-
.xlings
1+
# xmake
22
.xmake
3-
build
3+
build
4+
5+
# xlings
6+
.xlings
7+
8+
# Compiled Object files
9+
*.slo
10+
*.lo
11+
*.o
12+
*.obj
13+
14+
# Precompiled Headers
15+
*.gch
16+
*.pch
17+
18+
# Linker files
19+
*.ilk
20+
21+
# Debugger Files
22+
*.pdb
23+
24+
# Compiled Dynamic libraries
25+
*.so
26+
*.dylib
27+
*.dll
28+
29+
# Compiled Static libraries
30+
*.lai
31+
*.la
32+
*.a
33+
*.lib
34+
35+
# Executables
36+
*.exe
37+
*.out
38+
*.app
39+
40+
# Debug information files
41+
*.dwo
42+
*.d
43+
44+
# CMake
45+
CMakeCache.txt
46+
CMakeFiles/
47+
cmake_install.cmake
48+
Makefile

CMakeLists.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ set(CMAKE_CXX_MODULE_STD 1)
77

88
project(mcpplibs-templates VERSION 1.0.0 LANGUAGES CXX)
99

10-
# Library target
10+
# Library
1111
add_library(mcpplibs-templates STATIC)
1212

13+
file(GLOB_RECURSE MODULE_SOURCES "src/*.cppm")
14+
1315
target_sources(mcpplibs-templates
1416
PUBLIC
1517
FILE_SET CXX_MODULES FILES
16-
src/templates.cppm
18+
${MODULE_SOURCES}
1719
)
1820

19-
# Test executable
20-
add_executable(tests tests/main.cpp)
21-
target_link_libraries(tests PRIVATE mcpplibs-templates)
21+
# Test
22+
add_executable(templates_test tests/main.cpp)
23+
target_link_libraries(templates_test PRIVATE mcpplibs-templates)
2224

23-
# Enable testing
2425
enable_testing()
25-
add_test(NAME mcpplibs-templates-test COMMAND tests)
26+
add_test(NAME mcpplibs-templates-test COMMAND templates_test)
27+
28+
# Example
29+
add_executable(basic examples/basic.cpp)
30+
target_link_libraries(basic PRIVATE mcpplibs-templates)

README.md

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,95 @@
11
# mcpplibs templates
22

3-
mcpplibs templates...
3+
> C++23 模块化库项目模板 - `import mcpplibs.templates;`
44
5-
`src/templates.cppm`
5+
基于 C++23 模块的库项目模板,提供标准化的项目结构、构建配置和 CI/CD 流水线,帮助快速创建 mcpplibs 风格的模块化 C++ 库。
66

7-
```cpp
8-
module; // 0 - global module declaration
9-
10-
// 1 - include & macro area
11-
// #include <stdio.h>
7+
## 特性
128

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+
- **开箱即用** — 包含示例、测试和架构文档
1814

19-
// 4 - module implementation partition
20-
namespace mcpplibs {
15+
## 项目结构
2116

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 工具链配置
2834
```
2935

30-
`tests/main.cpp`
36+
## 快速开始
3137

3238
```cpp
33-
// 6 - import module
39+
import std;
3440
import mcpplibs.templates;
3541

36-
auto main() -> int {
37-
// 7 - call exported function
38-
mcpplibs::hello_mcpp();
42+
int main() {
43+
mcpplibs::templates::hello_mcpp();
44+
return 0;
3945
}
4046
```
4147

42-
## Install & Config
48+
## 安装与配置
4349

4450
```bash
4551
xlings install
4652
```
4753

48-
## Build & Run
54+
## 构建与运行
4955

50-
**Using xmake**
56+
**使用 xmake**
5157

5258
```bash
53-
xmake build
54-
xmake r
59+
xmake build # 构建库
60+
xmake run basic # 运行基础示例
61+
xmake run templates_test # 运行测试
5562
```
5663

57-
**Using CMake**
64+
**使用 CMake**
5865

5966
```bash
6067
cmake -B build -G Ninja
6168
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)
6387
```
6488

65-
## Other
89+
## 相关链接
6690

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)
6795
- [入门教程: 动手学现代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

Comments
 (0)