5

I working on lib and have a unit tests for it based on gtest framework. So setup for unittests compiled fine. However when I try a little bit different setup for measuring code coverage, compilation fails with this error:

[ 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

and this is how my main.cpp file looks like:

#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();
    };
}

So what could I try to fix this?


There is how "CmakeCxxCompilerId.cpp" looks like: link

silent_coder
  • 6,222
  • 14
  • 47
  • 91
  • 2
    Seems to be a duplicate of http://stackoverflow.com/questions/22176957/cmake-finds-more-than-one-main-function/22177285 – Argenet Jan 05 '17 at 21:50
  • 3
    I would take a look at CMakeCXXCompilerId.cpp:514 – Jonas Jan 05 '17 at 21:51
  • @Jonas there is a link https://repl.it/FBoX – silent_coder Jan 05 '17 at 21:56
  • When you use GLOB_RECURSE it is better to use [out of source](http://voices.canonical.com/jussi.pakkanen/2013/04/16/why-you-should-consider-using-separate-build-directories/) builds. Otherwise the CMakeLists.txt file gets ugly with lots of hacks. – Aelian Aug 19 '18 at 04:17

3 Answers3

23

I assume you using file(GLOB_RECURSE... in top level directory to get list of sources for your project. That's approach is very dangerous and you really could see it now, since you getting into troubles. But as workaround try something like this:

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

this will solve your immediate problem, but you need to reathink general approach to avoid similar story in future.

Because right know you searching across all the sources in directory and compiler simply find 2 main function, which is ofcourse not allowed in C++ world. Code snippet I provide to you just exclud code in cmake-build-debug folder from compilation.

Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
4

Look https://public.kitware.com/Bug/view.php?id=11043 Use something like this:

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

If one will use GLOB_RECURSE instead, it will also check CMakeFiles folder and what is inside. It will find cpp file with main function generated by cmake, that causes multiple definition of "main" function.

1

CMake must have found another *.cpp file in your project that contains one more main(). You should revise cmake build project. Check this cmake finds more than one main function

Community
  • 1
  • 1
Alex P.
  • 279
  • 2
  • 6
  • Better to link to duplicates in an comment than in a **new answer**. This issue seems to be a duplicate of [cmake finds more than one main function](http://stackoverflow.com/questions/22176957/cmake-finds-more-than-one-main-function). – cwschmidt Jan 05 '17 at 22:17