[build2] How to define a library implementing main()?

Klaim - Joël Lamotte mjklaim at gmail.com
Sun Sep 16 16:23:09 UTC 2018

This is in the process of trying to convert gtest to build2.
(you can check the current state and questions in buildfile comment
there: https://github.com/Klaim/gtest-build2 see the 'develop' branch)
Also I'm building with VS2017 so that's where reports are coming from.

I'm having trouble with several things which I'm figuring out
progressively but I'm being blocked in particular by the definition
and usage of the gtest_main target which you can find in GTest
CMakeLists.txt files:
Here is the source file it is made of:

The idea here is that if you don't want to have to copy paste the same
main() definition in all your tests, you can just link with gtest_main
and it will import the definition of main() it have.
I can't really ignore this target because it is used by samples and tests.

An alternative would be to just have a way to refer to that
gtest_main.cc file and let projects wanting it compile it as part of
their programs. I failed to find a way to obtain a variable containing
an absolute path to that file, so maybe it's possible but so far I
didn't find a simple way to do so.

If you try to build my repository with develop branch with:
    b gtest_build_samples=true

You will find that there are linking errors because of missing entry points:
    LINK : fatal error LNK1561: entry point must be defined

So far I didn't find the real source of the issue as the entry point
should have been defined but I'm suspecting that having a main
function marked as dllimport/export might be part of the issue.
However it does work when working when using CMake, so it's confusing.

I tried to tweak my script to use libue{gtest_main} instead of the
other kinds of library, with no change (or maybe I did it wrong, or
maybe the dllimport/export interacted in a bad way).

Anyway I would like to restart from another angle: how do you
recommend, in general, such a library just implementing main would be
defined and provided in build scripts?
It's not the only library I see that uses this kind of sceme so it
would be good to know how to do this in build2.

A. Joël Lamotte

More information about the users mailing list