2017-01-05 1 views
2

lib에서 작업 중이며 gtest 프레임 워크를 기반으로하는 단위 테스트가 있습니다. unittests에 대한 설정은 입니다. 내가 코드 커버리지를 측정하기 위해 조금 다른 설정을하려고 할 때 그러나 컴파일이 오류와 함께 실패합니다C++ 컴파일 오류 : 'main'의 다중 정의이지만 프로젝트의 주요 기능은 1 개뿐입니다.

[ 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

+2

이 http://stackoverflow.com/questions/22176957/cmake-finds-more-than-one-main-function/22177285의 중복 것 같다 – Argenet

+3

나는 CMakeCXXCompilerId.cpp에서 살펴 것 : 514 – Jonas

+0

를 @ Jonas에 https://repl.it/FBoX 링크가 있습니다 –

답변

4

귀하의 프로젝트 소스 목록을 얻으려면 최상위 디렉토리에 file(GLOB_RECURSE...을 사용한다고 가정합니다. 그것은 접근법이 매우 위험하고 당신이 문제에 빠지기 때문에 당신은 그것을 지금 볼 수 있습니다.

file(GLOB_RECURSE REMOVE_CMAKE "cmake-build-debug/*") 
list(REMOVE_ITEM your_list_of_sources ${REMOVE_CMAKE}) 

이 즉각적인 문제를 해결할 것입니다,하지만 당신은 미래에 비슷한 이야기를 방지하기 위해 일반적인 접근을 reathink해야하지만 같은 해결 방법을 다음과 같이 뭔가를하려고합니다.

디렉토리와 컴파일러의 모든 소스를 검색하면 C++ 환경에서는 허용되지 않는 2 가지 주요 기능을 쉽게 찾을 수 있습니다. 코드 스 니펫 컴파일에서 cmake-build-debug 폴더의 코드를 제외하면됩니다.

1

CMake가 포함 된 프로젝트의 다른 * .cpp 파일을 발견해야합니다 하나 더 주(). cmake 빌드 프로젝트를 수정해야합니다. 이것을 확인하십시오 cmake finds more than one main function

+0

** 새로운 답변 **보다 댓글에 중복 된 항목을 연결하는 것이 좋습니다. 이 문제는 [cmake가 하나 이상의 주요 기능을 찾는다] (http://stackoverflow.com/questions/22176957/cmake-finds-more-than-one-main-function)와 중복되는 것으로 보입니다. – cwschmidt

0

https://public.kitware.com/Bug/view.php?id=11043이 같은 사용 무언가 :

FILE(GLOB TARGET_H "${CMAKE_SOURCE_DIR}/*.h") 
FILE(GLOB TARGET_CPP "${CMAKE_SOURCE_DIR}/*.cpp") 
SET(TARGET_SRC ${TARGET_CPP} ${TARGET_H}) 
... 

한 대신 GLOB_RECURSE를 사용할 경우, 그것은 또한 CMakeFiles 폴더 무엇 내부에를 확인합니다. cmake가 생성 한 main 함수를 사용하여 "main"함수의 다중 정의를 일으키는 cpp 파일을 찾습니다.

관련 문제