【发布时间】:2017-05-20 14:27:23
【问题描述】:
我在开发 lib 并基于 gtest 框架对其进行单元测试。所以单元测试的设置编译得很好。但是当我尝试一些不同的设置来测量代码覆盖率时,编译失败并出现以下错误:
[ 10%] Linking CXX executable coverageRun.exe
CMakeFiles/coverageRun.dir/tests/src/main.cpp.o: In function `main':
/cygdrive/d/code/tests/src/main.cpp:24: multiple definition of `main'
CMakeFiles/coverageRun.dir/CMakeFiles/3.6.3/CompilerIdCXX/CMakeCXXCompilerId.cpp.o:/cygdrive/d/code/cmake-build-debug/CMakeFiles/3.6.3/CompilerIdCXX/CMakeCXXCompilerId.cpp:514: first defined here
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/coverageRun.dir/build.make:1215: coverageRun.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:101: CMakeFiles/coverageRun.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:113: CMakeFiles/coverageRun.dir/rule] Error 2
make: *** [Makefile:175: coverageRun] Error 2
这就是我的main.cpp 文件的样子:
#include <iostream>
#include <gtest/gtest.h>
#include "helpers/helper.h"
#include "helpers/pathHelper.h"
namespace code{
bool isPreconditionsOK(){
auto testRoot = helper::readEnvVar(helper::path::TEST_ROOT);
bool result = true;
if(testRoot.empty()){
std::cerr << "Env. variable " << helper::path::TEST_ROOT
<< " should be set before test start." << std::endl;
result = false;
}
return result;
}
}
int main(int argc, char **argv) {
if(code::isPreconditionsOK()){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
};
}
那么我可以尝试解决这个问题吗?
“CmakeCxxCompilerId.cpp”是这样的:link
【问题讨论】:
-
我会看看 CMakeCXXCompilerId.cpp:514
-
@Jonas 有链接repl.it/FBoX
-
当您使用 GLOB_RECURSE 时,最好使用 out of source 构建。否则 CMakeLists.txt 文件会因为大量的黑客攻击而变得丑陋。
标签: c++ compilation cmake googletest