与GTest的链接失败 [英] Linking against GTest fails

查看:127
本文介绍了与GTest的链接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

set(CMAKE_CXX_STANDARD 17)

# adding library
set(ST_SRC simple_tree.cpp)
add_library(st ${ST_SRC})
target_include_directories(st PUBLIC ${PROJECT_SOURCE_DIR}/include/data_structures/simple_tree/)

# adding googletest
set(GOOGLETEST_PATH ~/local/googletest)
set(GTEST_INCLUDE_DIR ~/local/include/)
set(GTEST_LIBRARY ~/local/lib/)
set(GTEST_MAIN_LIBRARY ~/local/lib/)
find_package(GTest REQUIRED)

# adding tests
set(TEST_TARGET test_simple_tree)
add_executable(${TEST_TARGET} test_simple_tree.cpp)
target_include_directories(${TEST_TARGET}
                        PUBLIC
                        ${PROJECT_SOURCE_DIR}/include/data_structures/simple_tree
                        ${GOOGLETEST_PATH}
                        ${GTEST_INCLUDE_DIR})
target_link_libraries(${TEST_TARGET} PUBLIC st)
target_link_libraries(${TEST_TARGET} PUBLIC gtest gtest_main)

基本上,我已将 googletest 安装到我的主目录中,而不是系统范围内。
find_package()命令显然成功。但是,尝试构建 test_simple_tree 失败,并显示以下内容:

Basically, I've installed googletest into my home directory rather than system-wide. The find_package() command apparently succeeds. However, trying to build test_simple_tree fails with:

/usr/bin/ld: cannot find -lgtest
/usr/bin/ld: cannot find -lgtest_main

在此 CMakeLists.txt 里面,我还能如何告诉链接器在其他地方
查找 gtest

Inside this CMakeLists.txt, how else can I tell the linker to look elsewhere for the gtest?

编辑:阅读文档后,我已修复了 Gtest 问题,如下所述。但是,出现了以下问题: CMake导入的目标包含不存在的路径

After reading the docs, I've fixed the Gtest issue as described below. However, the following issue cropped up: CMake imported target includes non-existent path

推荐答案

如果 find_package()成功找到GTest包括/库,它应该根据CMake为您填充目标 FindGTest 文档:

If find_package() was successful in finding the GTest includes/libraries, it should populate targets for you, per the CMake FindGTest documentation:


此模块定义了以下 IMPORTED 目标:

GTest :: GTest
Google测试 gtest 库(如果找到);自动添加Thread :: Thread

GTest::GTest: The Google Test gtest library, if found; adds Thread::Thread automatically

GTest :: Main
Google测试 gtest_main 库(如果找到)

您应该在 target_link_libraries中使用它们()命令。另外,CMake将填充 GTEST_INCLUDE_DIRS ,但GTest包含目录应从上述导入的目标中拉入。

You should use these in your target_link_libraries() command instead. Also, CMake will populate GTEST_INCLUDE_DIRS, but the GTest include directories should be pulled in from the imported targets mentioned above.

另一个重要说明:我不确定您是否发布了CMake代码的 all ,但在您的代码中看不到 project()调用。因此, $ {PROJECT_SOURCE_DIR} 变量可能不是您所期望的。通常,优良作法是在CMake顶部使用 project()声明项目。

Another important note: I'm not sure if you posted all of your CMake code, but I don't see a project() call in your code. As a result, the ${PROJECT_SOURCE_DIR} variable may not be what you expect. In general, it is good practice to declare your project with project() at the top of your CMake.

经过这些修改的CMake文件可能看起来像这样:

Your CMake file with these modifications could look something like this:

cmake_minimum_required(VERSION 3.15)

project(simple_tree_example)

set(CMAKE_CXX_STANDARD 17)

# adding library
set(ST_SRC simple_tree.cpp)
add_library(st ${ST_SRC})
target_include_directories(st PUBLIC 
    ${PROJECT_SOURCE_DIR}/include/data_structures/simple_tree/)

# adding googletest
set(GOOGLETEST_PATH ~/local/googletest)
set(GTEST_INCLUDE_DIR ~/local/include/)
set(GTEST_LIBRARY ~/local/lib/)
set(GTEST_MAIN_LIBRARY ~/local/lib/)
find_package(GTest REQUIRED)

# adding tests
set(TEST_TARGET test_simple_tree)
add_executable(${TEST_TARGET} test_simple_tree.cpp)
target_include_directories(${TEST_TARGET}
                        PUBLIC
                        ${PROJECT_SOURCE_DIR}/include/data_structures/simple_tree
                        ${GOOGLETEST_PATH})
target_link_libraries(${TEST_TARGET} PUBLIC st GTest::GTest GTest::Main)

这篇关于与GTest的链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆