[Tests] Link gtest_main on the fetched-gtest path

The downloaded-gtest branch set GTEST_BOTH_LIBRARIES to `gtest` alone, while
FindGTest sets it to both halves. Every test target links that list rather than
picking an entry point, so on this path nothing provides main() and any test
source that does not define its own fails at link with `undefined reference to
main` pointing at crt1.o, naming neither gtest nor the line responsible.

That stayed latent while every test carried its own four-line main(). A test that
omits it now fails, and the error points at the C runtime rather than at gtest,
which is a long way from the cause. Restoring both halves makes the two paths
agree and drops the boilerplate requirement on new test sources.

The 18 sources that still define main() keep linking: gtest_main.cc defines
nothing but main, so the linker leaves its object in the archive unused rather
than reporting a duplicate.
This commit is contained in:
Lukas Brübach 2026-10-05 14:44:20 +02:00
parent 3f9d6d7087
commit 72aa830e7c

View file

@ -30,7 +30,11 @@ if(NOT GTEST_FOUND)
target_include_directories(gtest INTERFACE "$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/gtest-src/include>") target_include_directories(gtest INTERFACE "$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/gtest-src/include>")
set(GTEST_INCLUDE_DIRS "${CMAKE_BINARY_DIR}/gtest-src/include") set(GTEST_INCLUDE_DIRS "${CMAKE_BINARY_DIR}/gtest-src/include")
set(GTEST_BOTH_LIBRARIES gtest) # gtest_main is not optional here. FindGTest sets GTEST_BOTH_LIBRARIES to both halves, so this
# has to match it or the two paths disagree about what provides main(). A test source that
# defines its own main() gets away with the omission; one that does not fails at link with
# `undefined reference to main` pointing at crt1.o, which names neither gtest nor this line.
set(GTEST_BOTH_LIBRARIES gtest gtest_main)
endif() endif()
# Directory-scoped on purpose: the test subdirectories below add targets that include the # Directory-scoped on purpose: the test subdirectories below add targets that include the
@ -92,7 +96,7 @@ function(cockatrice_add_test)
# Only needed when gtest was downloaded and add_subdirectory'd above. An imported gtest # Only needed when gtest was downloaded and add_subdirectory'd above. An imported gtest
# target already carries its own dependency, so this must stay conditional. # target already carries its own dependency, so this must stay conditional.
if(NOT GTEST_FOUND) if(NOT GTEST_FOUND)
add_dependencies(${ARG_NAME} gtest) add_dependencies(${ARG_NAME} gtest gtest_main)
endif() endif()
# MANUAL targets are built but deliberately kept out of ctest: benchmarks that need a card # MANUAL targets are built but deliberately kept out of ctest: benchmarks that need a card