Search code examples
installationgoogletestgooglemockconan

After installing gtest, gmock_main.cc replaces my main.cc


I experience that when 'gtest' is installed by 'conan', my 'gmock_main' somehow replaces my 'main'.

I am step-by-step adding more libraries to my program, using more and more lines to conanfile.txt, clear the generated files in subdirectory build, and run "cmake..; cmake --build .". After this I run the built binary.

My conanfile.txt is

[requires]
SystemC/2.3.3@minres/stable
SystemCVerification/2.0.1@minres/stable
doxygen_installer/1.8.15@bincrafters/stable
qt/5.12.0@bincrafters/stable
gtest/1.8.1@bincrafters/stable
#flex/2.6.4@bincrafters/stable

[generators]
cmake

[options]
SystemC:stdcxx=14
SystemCVerification:stdcxx=14

Until I add the line

gtest/1.8.1@bincrafters/stable

everything looks file, my binary starts and makes what I want. After installing gtest, the reply from MY OWN, NON-GTEST-RELATED program changes from

       SystemC 2.3.3-Accellera --- Jun 15 2019 21:14:08
        Copyright (c) 1996-2018 by all Contributors,
        ALL RIGHTS RESERVED
 Correct usage:

to

Running main() from gmock_main.cc
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[  PASSED  ] 0 tests.

If I remove the line and remove gtest from conan database, rebuild my binary, it works fine again. I have no idea, what can cause such a strange error? Where to find the reason? Could be an accidental name clatching?

AFTER SUBMISSION:

I guessed that the problem is somehow caused by gtest. So I made the followings: I saved the value of ${CONAN_LIBS} without and with having the line

gtest/1.8.1@bincrafters/stable

active in my conanfile.txt

scv;
systemc;ssl;crypto;pcre2-posix;pcre2-8;pcre2-16;pcre2-32;double-conversion;
freetyped;harfbuzz;jpeg;sqlite3;pgcommon;pq;pthread;odbc;odbccr;odbcinst;ltdl;dl;bz2;png16d;m;z

as well as with gtest:

scv;
gmock_maind;gmockd;gtestd;
systemc;ssl;crypto;pcre2-posix;pcre2-8;pcre2-16;pcre2-32;double-conversion;
freetyped;harfbuzz;jpeg;sqlite3;pgcommon;pq;pthread;odbc;odbccr;odbcinst;ltdl;dl;bz2;png16d;m;z

For better visibility, I separated the second line, really containing the names of libraries installed with the suspected line. After that, I edited the respective CMakeLists.txt (I removed the actual ${CONAN_LIBS} and replaced it with the former value, before asking conan to install gtest.)

target_link_libraries(A
#    ${CONAN_LIBS}
scv;systemc;ssl;crypto;pcre2-posix;pcre2-8;pcre2-16;pcre2-32;double-conversion;freetyped;harfbuzz;jpeg;sqlite3;pgcommon;pq;pthread;odbc;odbccr;odbcinst;ltdl;dl;bz2;png16d;m;z
)

And, my app works as expected. What is going on here? How can gmock_main.cc replace my own .cc? (actually, a int sc_main(int argc,char *argv[]) but probably is makes no difference) Or better: how can I avoid it?

I understand that 'main' call in a library can replace my own 'main'. Why is a main function in the library and how can I avoid it (I mean a way other than making this manual editing).

SECOND EDIT: I also noticed that if I leave the

${CONAN_LIBS} in the test branch, gmock_maind; take over the control: already it puts my tests into its own main program, as I am using a System C based main program (i.e. int sc_main(int argc, char* argv[]) ), it is not really good for me. Finally, I edited again the list of conan install libs and removed

gmock_maind;gmockd;

and now finally my apps make what I expect.

It is not quite the easiness I expected for the change. As far as I know, since 1.8.0 gmock and gtest come in one package. I do not need gmock but it is OK. But what is the reason of adding gmock_maind, that kils all subprojects, except the ones which were for havin genter code here a gmock main test file, nothing else.

Is there some option to get rid off this gmock_maind, or whihc is the newest gtest that does not comprise gmock?


Solution

  • I suppose it is caused by the standard option behavior of the gtest package. These are the default options of gtest 1.10:

       default_options = {"shared": False, "build_gmock": True, "fPIC": True, "no_main": False,"debug_postfix": 'd', "hide_symbols": False}
    

    As you can see, no_main is false - this is the reason why when you link against gtest you also get the main from gtest delivered, which hides your own main.

    It should be enough to set

    [options]
    gtest:no_main=True   
    

    or

    default_options = {"gtest:no_main":True}
    

    if you use a conanfile.py or call conan like

    conan install . -o gtest:no_main=True 
    

    if you prefer to not modify your conanfile at all.

    I suggest to use the new conan-center https://conan.io/center/gtest/1.10.0/?user=&channel=&revision=&os=&tab=recipe since it is the newest and best maintained site for conan packages.

    To learn more about setting conan options take a look here: https://docs.conan.io/en/latest/reference/conanfile/attributes.html#options

    Edit: In your specific case the reason why the linker picks gmocks main and not yours, instead of complaining about multiple defined symbols is that you use systemcs's sc_main - main, which is actually not the correct name for the entrypoint (the name needs always to be "main") but inside systemsc there is a main function that calls your sc_main as if it would be the actual entrypoint. I assume that there is also a preprocessor mechanism in systemsc that prevents linker errors when another standard main function is defined somewhere else. Read more about here: https://forums.accellera.org/topic/5932-how-exactly-sc_main-works/