我正在开发一个库,并有基于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"看起来是这样的:链接