# xmake-multi-cpp-modules **Repository Path**: da-liii/xmake-multi-cpp-modules ## Basic Information - **Project Name**: xmake-multi-cpp-modules - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-24 - **Last Updated**: 2026-06-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # xmake-multi-cpp-modules 基于 [xmake](https://xmake.io) 的多模块 C++ 示例项目。演示如何将代码拆分为多个静态库和一个可执行入口,并为每个库模块配备独立的单元测试。 ## 项目结构 ```text xmake-multi-cpp-modules/ ├── xmake.lua # 根项目配置 ├── README.md ├── .gitignore ├── a/ # 模块 a:静态库 │ ├── include/a/a.h │ ├── src/a.cpp │ ├── tests/test_a.cpp │ └── xmake.lua ├── b/ # 模块 b:静态库 │ ├── include/b/b.h │ ├── src/b.cpp │ ├── tests/test_b.cpp │ └── xmake.lua ├── c/ # 模块 c:静态库,依赖 a 和 b │ ├── include/c/c.h │ ├── src/c.cpp │ ├── tests/test_c.cpp │ └── xmake.lua └── d/ # 模块 d:可执行文件入口,依赖 c ├── src/main.cpp └── xmake.lua ``` ## 模块依赖 ```text mod_d (binary) └── mod_c (static) ├── mod_a (static) └── mod_b (static) ``` > **注意:** xmake 的 target 名使用了 `mod_` 前缀。因为单字母名 `a`、`b`、`c` 会生成链接参数 `-la`、`-lb`、`-lc`,与系统库冲突。 ## 环境要求 - [xmake](https://xmake.io/#/guide/installation) >= 2.8.0 - 支持 C++17 的编译器(g++、clang++、MSVC 等) ## 编译 ```bash xmake f -m release -y xmake ``` 运行可执行文件: ```bash xmake run mod_d ``` 预期输出: ```text hello from module c (hello from module a, hello from module b) ``` ## 编译单个模块 所有可用 target: ```text mod_a mod_b mod_c mod_d test_mod_a test_mod_b test_mod_c ``` 只构建 `mod_c`(会自动先构建其依赖 `mod_a`、`mod_b`): ```bash xmake build mod_c ``` 只构建可执行入口: ```bash xmake build mod_d ``` ## 测试单个模块 运行模块 c 的单元测试(会自动先构建 `test_mod_c` 及其依赖): ```bash xmake test "test_mod_c/*" ``` 加上 `-v` 查看 doctest 详细输出: ```bash xmake test -v "test_mod_c/*" ``` 运行所有测试: ```bash xmake test -v ``` ## 运行单元测试 测试使用 [doctest](https://github.com/doctest/doctest) 编写,通过 `xmake test` 执行。 ```bash xmake test -v ``` 预期结果: ```text 100% tests passed, 0 test(s) failed out of 3 ``` ## 切换编译模式 ```bash xmake f -m debug -y xmake ``` 支持的编译模式:`release`、`debug`。 ## 项目用途 本项目是 [Mogan](https://github.com/XmacsLabs/mogan) 代码拆分工作的参考原型:将原本集中在单个静态库中的代码,按功能域拆分为更细粒度的模块,每个模块独立编译、独立测试,最终由顶层可执行目标组合。