在mcpp.toml的[dependences]添加gtest依赖时,gtest_main与程序入口main函数冲突问题 #168
Copy link
Copy link
Closed
mcpplibs/mcpp-index
#45Description
Activity
如果只是 tests 下面用的, 可以放到
[dev-dependencies]下面. mcpp build 是不构建 tests的. mcpp test 会自动识别Lines 45 to 47 in 453faba
[dev-dependencies] gtest = "1.15.2" - added a commit that references this issue
on Jun 25, 2026 - added a commit that references this issue
on Jun 25, 2026 已在 mcpp 0.0.65 修复(已发布:
xlings install mcpp@0.0.65 -y)。根因
mcpp add gtest把 gtest 写进[dependencies](常规依赖),mcpp build时 gtest 的gtest_main.cc(自带main)被链进应用,与应用自身main冲突 →LNK2005 / duplicate symbol: main。修复:通用「feature 门控源」
- gtest 描述符把
gtest_main.cc归入mainfeature,默认不链:gtest = "1.15.2"→ 只链框架,不再撞 main(app 自带 main 也安全);- 需要 gtest 提供 main(只写
TEST宏、不写 main)→gtest = { version = "1.15.2", features = ["main"] }。
- 这是通用机制(
[mcpp].features.<名>.sources),非 gtest 特例;旧 mcpp 兼容(行为不变)。
另外
- 测试框架建议用
mcpp add --dev gtest(写入[dev-dependencies]),由mcpp test消费 ——mcpp test对「带/不带 main 的测试」均自动处理。
设计详见仓库
.agents/docs/2026-06-25-gtest-main-feature-and-add-dev-design.md。- gtest 描述符把
- added a commit that references this issue
on Jul 15, 2026
Metadata
Metadata
Assignees
Labels
No labels
平台:win11
mcpp version:0.0.64
问题复现:
in:
mcpp add gtest@1.15.2
out:
Adding gtest v1.15.2 to dependencies
Run
mcpp buildto fetch and build with the new dependency.in:
mcpp build
out:
Resolving toolchain
Resolved llvm@20.1.7 → @mcpp/registry/data/xpkgs/xim-x-llvm/20.1.7/bin/clang++.exe
Updating package index (auto-refresh)
Downloading mcpplibs.gtest v1.15.2
Downloading compat.gtest v1.15.2
Downloading compat:compat.gtest@1.15.2
Inferred sources [src/**/*.{cppm,cpp,cc,c}]
Compiling add v0.1.0 (.)
Compiling gtest v1.15.2
error: build failed
gtest_main.o : error LNK2005: main already defined in main.o
bin\hello.exe : fatal error LNK1169: one or more multiply defined symbols found
clang++: error: linker command failed with exit code 1169 (use -v to see invocation)