google / google/googletest

[Bug]: undefined reference to `typeinfo for testing::Test' ,only link by gtest, instead of GTest::gtest

Open
#4,734 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39.5k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

### Describe the issue

awe@test_lib$ uname -a
Linux fedora 6.13.5-100.fc40.x86_64 `#1` SMP PREEMPT_DYNAMIC Thu Feb 27 15:10:07 UTC 2025 x86_64 GNU/Linux
awe@test_lib$ sudo dnf install gtest-devel
[sudo] awe 的密码:
上次元数据过期检查:1:31:22 前,执行于 2025年03月16日 星期日 20时06分46秒。
软件包 gtest-devel-1.14.0-4.fc40.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
# /CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.20)
project(test_lib)
set(CMAKE_PREFIX_PATH /home/awe/Desktop/dev/install/)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
link_libraries(pthread)
add_link_options(-finput-charset=Latin1 -fexec-charset=UTF-8)
find_package(GTest CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
enable_testing()
add_subdirectory(botan)
```
# botan/CMakeLists.txt
```cmake
find_package(leveldb CONFIG REQUIRED)
find_package(Botan CONFIG REQUIRED)
#include(GoogleTest)
#enable_testing()
link_libraries(Botan::Botan GTest::gtest GTest::gtest_main)
add_executable(botan_main main.cpp)
#gtest_discover_tests(botan_main)

```
# main.cpp
```cpp
#include
TEST(TestA,name) {
ASSERT_TRUE(true);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv); // 初始化 Google Test
return RUN_ALL_TESTS(); // 运行所有测试用例
}

```
====================[ 构建 | botan_main | debug ]=================================
/home/awe/Downloads/clion-2024.3/bin/cmake/linux/x64/bin/cmake --build /home/awe/Desktop/clion/test_lib/cmake-build-debug --target botan_main -j 6 -v
Change Dir: '/home/awe/Desktop/clion/test_lib/cmake-build-debug'

Run Build Command(s): /home/awe/Downloads/clion-2024.3/bin/ninja/linux/x64/ninja -v -j 6 botan_main
[1/1] : && /usr/lib64/ccache/c++ -g -finput-charset=Latin1 -fexec-charset=UTF-8 -fstack-protector -m64 -pthread botan/CMakeFiles/botan_main.dir/main.cpp.o -o botan/botan_main -Wl,-rpath,/home/awe/Desktop/dev/install/lib -lpthread /home/awe/Desktop/dev/install/lib/libbotan-3.so.7 /home/awe/Desktop/dev/install/lib64/libgtestd.a /home/awe/Desktop/dev/install/lib64/libgtest_maind.a /home/awe/Desktop/dev/install/lib64/libgtestd.a && :
FAILED: botan/botan_main
: && /usr/lib64/ccache/c++ -g -finput-charset=Latin1 -fexec-charset=UTF-8 -fstack-protector -m64 -pthread botan/CMakeFiles/botan_main.dir/main.cpp.o -o botan/botan_main -Wl,-rpath,/home/awe/Desktop/dev/install/lib -lpthread /home/awe/Desktop/dev/install/lib/libbotan-3.so.7 /home/awe/Desktop/dev/install/lib64/libgtestd.a /home/awe/Desktop/dev/install/lib64/libgtest_maind.a /home/awe/Desktop/dev/install/lib64/libgtestd.a && :
/usr/bin/ld: botan/CMakeFiles/botan_main.dir/main.cpp.o:(.rodata._ZTI16AES256CipherTest[_ZTI16AES256CipherTest]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: 错误:ld 返回 1
ninja: build stopped: subcommand failed.

but when use `link_libraries(Botan::Botan gtest)`,is running success,and why ? :
/home/awe/Desktop/clion/test_lib/cmake-build-debug/botan/botan_main
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestA
[ RUN ] TestA.name
[ OK ] TestA.name (0 ms)
[----------] 1 test from TestA (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.

### Steps to reproduce the problem

link gtest vs GTest::test

### What version of GoogleTest are you using?

awe@test_lib$ sudo dnf install gtest-devel
[sudo] awe 的密码:
上次元数据过期检查:1:31:22 前,执行于 2025年03月16日 星期日 20时06分46秒。
软件包 gtest-devel-1.14.0-4.fc40.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!

### What operating system and version are you using?

awe@test_lib$ uname -a
Linux fedora 6.13.5-100.fc40.x86_64 `#1` SMP PREEMPT_DYNAMIC Thu Feb 27 15:10:07 UTC 2025 x86_64 GNU/Linux

### What compiler and version are you using?

awe@test_lib$ gcc --version
gcc (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3)
Copyright © 2024 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

### What build system are you using?

awe@test_lib$ cmake --version
cmake version 3.30.7
awe@test_lib$ ninja --version
1.12.1

### Additional context

Very confused

Contributor guide

Open the contributing guide

Research direction

Start with botan/CMakeLists.txt and compare the generated link commands for GTest::gtest versus gtest using the provided Fedora, CMake, GCC, and Ninja versions. Reproduce the undefined reference and trace the imported target configuration; done means the cause is identified and the CMake linkage behavior is corrected or clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.