Skip to content

Prevent linker warning about GTest::gtest#294

Draft
ClausKlein wants to merge 5 commits intobemanproject:mainfrom
ClausKlein:feature/prevent-linker-warning
Draft

Prevent linker warning about GTest::gtest#294
ClausKlein wants to merge 5 commits intobemanproject:mainfrom
ClausKlein:feature/prevent-linker-warning

Conversation

@ClausKlein
Copy link

@ClausKlein ClausKlein commented Jan 23, 2026

Fix duplicative GTest::gtest GTest::gtest_main dependency

for #292

  • Needs first a final decision how to work with dependency_provider cmake module, if needed?

Fix duplicative GTest::gtest GTest::gtest_main dependency
@coveralls
Copy link

coveralls commented Jan 23, 2026

Coverage Status

coverage: 100.0%. remained the same
when pulling 6f91f4b on ClausKlein:feature/prevent-linker-warning
into 2dd6d28 on bemanproject:main.

Copy link
Member

@camio camio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BUILD_GMOCK is an implementation detail of some implementations of find_package(GTest). Since it is not part of the basic find_package(GTest) contract, it is subject to change and, consequently, I don't think we should use it.

The other part of this PR looks reasonable to me. The GTest API directly mentions gtest_main as an alternative to gtest as opposed to an addition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set(BUILD_GMOCK OFF)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set(BUILD_GMOCK OFF)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but we need more: set(INSTALL_GTEST OFF) # Disable GoogleTest installation

See #295 for more info

@ClausKlein ClausKlein requested a review from camio January 24, 2026 12:19
@ClausKlein
Copy link
Author

ClausKlein commented Jan 24, 2026

The other part of this PR looks reasonable to me. The GTest API directly mentions gtest_main as an alternative to gtest as opposed to an addition.

see too https://discourse.cmake.org/t/missing-warning-if-a-public-link-dependencies-is-not-to-be-installed/15475
and https://discourse.cmake.org/t/how-to-use-fetchcontent-correctly-for-building-external-dependencies/3686

Comment on lines +168 to +170
# NO! EXCLUDE_FROM_ALL
)
set(INSTALL_GTEST OFF) # Disable GoogleTest installation
# NO! set(INSTALL_GTEST OFF) # Disable GoogleTest installation
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong here, it is a general cmake module!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concur gtest stuff doesn't below here. @nickelpro I assume you concur?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it doesn't belong here, but it isn't clear to me where it does belong. We need to set this to off in order to avoid installing gtest as part of our installation package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second one is incorrect, the first I believe to be correct.
Excluding from the all target will still require a target to be built if used, but it will not be part of the install step.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Than your project can't be installed if you have public used dependencies!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A project should not install its own dependencies, in general? Unless I've misunderstood something.
I don't expect, or want, say, Google test, or libfmt, to be installed because I use them.
I probably do want to record what the as built software was built against in the CPS data.

I'm afraid I've missed some context, can you link back to the example that is not working with install?

Also, I have thousands of packages that have both gtest and out equivalent of gtest main (where that main also initialized some common infrastructure) and I don't see a linker warning. Can you c/p the warning?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A project should not install its own dependencies, in general? Unless I've misunderstood something. I don't expect, or want, say, Google test, or libfmt, to be installed because I use them. I probably do want to record what the as built software was built against in the CPS data.

GTest is not the problem, except you build a shared libgtest and want to install your tests.

-- Detecting CXX compile features - done
CMake Warning at /Users/clausklein/Workspace/cpp/boost-git/libs/type_index/CMakeLists.txt:53 (message):
  Boost.type_index: `import std;` is not available for C++20


CMake Warning at /Users/clausklein/Workspace/cpp/boost-git/libs/any/CMakeLists.txt:54 (message):
  Boost.Any: `import std;` is not available for C++20


-- boost-install-library(boost_config): COMPONENT 'boost'
-- boost_install_library(boost_config): EXPORT_NAME boost_config for TARGET 'boost_config'
-- boost-install-library(boost_mp11): COMPONENT 'boost'
-- boost_install_library(boost_mp11): EXPORT_NAME boost_mp11 for TARGET 'boost_mp11'
-- boost-install-library(boost_mp11): Add find_dependency(Boost::core)
-- boost-install-library(boost_describe): COMPONENT 'boost'
-- boost_install_library(boost_describe): EXPORT_NAME boost_describe for TARGET 'boost_describe'
-- boost-install-library(boost_describe): Add find_dependency(Boost::mp11)
-- boost-install-library(boost_container_hash): COMPONENT 'boost'
-- boost_install_library(boost_container_hash): EXPORT_NAME boost_container_hash for TARGET 'boost_container_hash'
-- boost-install-library(boost_container_hash): Add find_dependency(Boost::describe)
-- boost-install-library(boost_throw_exception): COMPONENT 'boost'
-- boost_install_library(boost_throw_exception): EXPORT_NAME boost_throw_exception for TARGET 'boost_throw_exception'
-- boost-install-library(boost_throw_exception): Add find_dependency(Boost::assert)
-- boost-install-library(boost_type_index): COMPONENT 'boost'
-- boost_install_library(boost_type_index): EXPORT_NAME boost_type_index for TARGET 'boost_type_index'
-- boost-install-library(boost_type_index): 'boost_type_index' has INTERFACE_HEADER_SETS=headers_public
-- boost-install-library(boost_type_index): 'boost_type_index' has CXX_MODULE_SETS=modules_public
-- boost-install-library(boost_type_index): Add find_dependency(Boost::boost_container_hash)
-- boost-install-library(boost_any): COMPONENT 'boost'
-- boost_install_library(boost_any): EXPORT_NAME boost_any for TARGET 'boost_any'
-- boost-install-library(boost_any): 'boost_any' has INTERFACE_HEADER_SETS=headers_public
-- boost-install-library(boost_any): 'boost_any' has CXX_MODULE_SETS=modules_public
-- boost-install-library(boost_any): Add find_dependency(Boost::type_index)
-- boost-install-library(boost_any): Add find_dependency(Boost::config)
-- boost-install-library(boost_any): Add find_dependency(Boost::throw_exception)
-- Configuring done (1.6s)
CMake Error in CMakeLists.txt:
  install(EXPORT "boost_throw_exception-targets" ...) includes target
  "boost_throw_exception" which requires target "boost_assert" that is not in
  any export set.


-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
make: *** [build/compile_commands.json] Error 1
bash-5.3$ 

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our you get later this:

1/1 Test #6: find-package-test ................***Failed    0.22 sec
Internal cmake changing into directory: /Users/clausklein/Workspace/cpp/beman-project/exemplar/build/tests/beman/exemplar/find-package-test
======== CMake output     ======
use ccache
CMake Warning at /Users/clausklein/Workspace/cpp/beman-project/exemplar/cmake/prelude.cmake:46 (message):
  $CXX is not set
Call Stack (most recent call first):
  CMakeLists.txt:5 (include)


CXXFLAGS=-stdlib=libc++
'brew' '--prefix' 'llvm'
LLVM_DIR=/usr/local/Cellar/llvm/21.1.8_1
CMAKE_CXX_STDLIB_MODULES_JSON=/usr/local/Cellar/llvm/21.1.8_1/lib/c++/libc++.modules.json
CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=/usr/local/Cellar/llvm/21.1.8_1/include/c++/v1;/usr/local/Cellar/llvm/21.1.8_1/lib/clang/21/include;/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include
CMAKE_CXX_STDLIB_MODULES_JSON=/usr/local/Cellar/llvm/21.1.8_1/lib/c++/libc++.modules.json
CMAKE_CXX_SCAN_FOR_MODULES=ON
CMake Error at /usr/local/share/cmake/Modules/CMakeFindDependencyMacro.cmake:93 (find_package):
  By not providing "Findbeman.inplace_vector.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "beman.inplace_vector", but CMake did not find one.

  Could not find a package configuration file provided by
  "beman.inplace_vector" with any of the following names:

    beman.inplace_vectorConfig.cmake
    beman.inplace_vector-config.cmake

  Add the installation prefix of "beman.inplace_vector" to CMAKE_PREFIX_PATH
  or set "beman.inplace_vector_DIR" to a directory containing one of the
  above files.  If "beman.inplace_vector" provides a separate development
  package or SDK, be sure it has been installed.
Call Stack (most recent call first):
  /usr/local/share/cmake/Modules/CMakeFindDependencyMacro.cmake:125 (__find_dependency_common)
  /Users/clausklein/Workspace/cpp/beman-project/exemplar/build/stagedir/lib/cmake/beman.exemplar/beman.exemplar-config.cmake:3 (find_dependency)
  CMakeLists.txt:16 (find_package)


Configuring incomplete, errors occurred!
======== End CMake output ======
Error: cmake execution failed


0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.26 sec

The following tests FAILED:
	  6 - find-package-test (Failed)
Errors while running CTest
bash-5.3$ 

@ClausKlein ClausKlein marked this pull request as draft January 25, 2026 09:00
find_package(GTest REQUIRED)
set(BUILD_GMOCK OFF) # not needed yet
set(INSTALL_GTEST OFF) # Disable GoogleTest installation too
find_package(GTest 1.16 CONFIG REQUIRED)
Copy link
Author

@ClausKlein ClausKlein Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steve-downey
Copy link
Member

I've lost the thread a bit, and I'll have to go back and reread all the links, but gtest showing up in our install sounds like we've tried to simplify install too much and we're not describing precisely what components to install?

Exemplar is below tiny so some machinery seems overkill. But it should not be expected that you need to rewrite chunks of exemplar, or the cookiecutter generated project, because you now have a bunch more parts.

In my opinion, exemplar should scale smoothly to a few installable components and a couple dozen files. North of 100 files may not fit well, but two orders of magnitude is a change in kind, not just degree. Exemplar is a scaled down small to medium sized project.

Larger packages can and will build multiple installable components. In particular being able to install and then test examples is important when dynamic libraries are involved, but should not be part of the "-dev" package, or library package. Making sure those knobs are exposed means someone doesn't have to rewrite our build system or needlessly repackage our packages.

Apologies for possibly misapprehending, and channeling Emily Litella, in which case "Nevermind".

@ClausKlein
Copy link
Author

I still wait for any decision?

Comment on lines +168 to +170
# NO! EXCLUDE_FROM_ALL
)
set(INSTALL_GTEST OFF) # Disable GoogleTest installation
# NO! set(INSTALL_GTEST OFF) # Disable GoogleTest installation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second one is incorrect, the first I believe to be correct.
Excluding from the all target will still require a target to be built if used, but it will not be part of the install step.

target_link_libraries(
beman.exemplar.tests.identity
PRIVATE beman::exemplar GTest::gtest GTest::gtest_main
PRIVATE beman::exemplar GTest::gtest_main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gtest_main target and library depend on the gtest library, but does not duplicate it. The only thing in libgtest_main.a is gtest_main.cc.o.

Since the use of gtest is not purely transitive, but direct, the project should link what it uses, and not rely on transitive linking of dependencies.

Any error or warning is not from the dependency but an indication of some other problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments