How to exclude second main while linking test exe?


(Robin Jansohn) #1

I’m trying to integrate unit tests in my native gradle project but I can’t seem to find a working solution. The problem occurs while linking the test executable since there are two wmains available. Does anyone know how to exclude one of them?

Here’s a minimal example of my setup:

Project structure

build.gradle
src
    -> main
        -> cpp
            -> main.cpp
            -> registry.cpp
        -> headers
            -> registry.hpp
    -> test
        -> cpp
            -> main_test.cpp
            -> test_registry.cpp
libs
    -> googletest
        -> 1.7.0
            -> include
                -> ...
            -> lib
                -> libgtest.a

build.gradle

apply plugin: 'cpp'
apply plugin: 'google-test-test-suite'

model {
    platforms {
        x86 {
            architecture "x86"
        }
        x64 {
            architecture "x86_64"
        }
    }
    
    components {
        main(NativeExecutableSpec) {
        	baseName "Registry"
            targetPlatform "x86"
            binaries.all {
                cppCompiler.args "-std=c++11", "-municode", "-mwindows"
                linker.args "-municode", "-mwindows"
            }
        }
    }
    
    testSuites {
        mainTest(GoogleTestTestSuiteSpec) {
            testing $.components.main
            
            sources {
                cpp.source.srcDir 'src/test/cpp'
            }
        }
    }
    
    repositories {
        libs(PrebuiltLibraries) {
            googleTest {
                headers.srcDir "libs/googletest/1.7.0/include"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile =
                        file("libs/googletest/1.7.0/lib/libgtest.a")
                }
            }
        }
    }
}

model {
    binaries {
        withType(GoogleTestTestSuiteBinarySpec) {
            lib library: "googleTest", linkage: "static"

            cppCompiler.args "-std=c++11", "-municode"
            linker.args "-municode"
        }
    }
}

Error message

:compileMainExecutableMainCpp
:linkMainExecutable
:mainExecutable
:assemble
:compileMainTestGoogleTestExeMainCpp
:compileMainTestGoogleTestExeMainTestCpp
:linkMainTestGoogleTestExe
C:\Users\minimal\build\objs\mainTest\mainCpp\e7f4uxujatdodel7e7qw5uhsp\main.obj:main.cpp:(.text+0x0): multiple definition of `wmain'
C:\Users\minimal\build\objs\mainTest\mainTestCpp\271ezc0ay5ubap2l962cnectq\main_test.obj:main_test.cpp:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

:linkMainTestGoogleTestExe FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':linkMainTestGoogleTestExe'.
> A build operation failed.
      Linker failed while linking mainTest.exe.

options.txt

-o
C:\\Users\\minimal\\build\\exe\\mainTest\\mainTest.exe
C:\\Users\\minimal\\build\\objs\\mainTest\\mainTestCpp\\271ezc0ay5ubap2l962cnectq\\main_test.obj
C:\\Users\\minimal\\build\\objs\\mainTest\\mainTestCpp\\dp6ieaohq04qqqa31sdfwrsxj\\test_registry.obj
C:\\Users\\minimal\\build\\objs\\mainTest\\mainCpp\\68sxcjmhakj69ha7wqtijofs3\\Registry.obj
C:\\Users\\minimal\\build\\objs\\mainTest\\mainCpp\\e7f4uxujatdodel7e7qw5uhsp\\main.obj
C:\\Users\\minimal\\libs\\googletest\\1.7.0\\lib\\libgtest.a
-municode
-m32

Any help is greatly appreciated!


(Robin Jansohn) #2

I refactored the project structure to a multi-project version as suggested here (http://stackoverflow.com/a/42727485/3375325). The root project only contains the application main, the rest is moved to a API library project where the library is built and tested.