【问题标题】:gtest and libtorch undefined reference to `testing::internal::gtest 和 libtorch 未定义对 `testing::internal:: 的引用
【发布时间】:2020-11-08 16:19:31
【问题描述】:

我正在尝试将 gtest 与 libtorch 一起使用。目前,我有一个基本的测试程序:

#include <gtest/gtest.h>

TEST(WorldMapTests, FirstTest) {
    ASSERT_TRUE(true);
}

然后我有一个主制作,我在第三方文件夹中获取火炬,如下所示:

set(TORCH_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/third-party/libtorch")
set(Torch_DIR "${TORCH_INSTALL_PREFIX}/share/cmake/Torch")
set(pybind11_INCLUDE_DIR "${TORCH_INSTALL_PREFIX}/include/pybind11")

然后我的测试结果如下:

cmake_minimum_required(VERSION 3.0)

set(CMAKE_CXX_STANDARD 14)

set(This ${PROJECT_NAME}_test)

set(Sources
  cpp/WorldMapTests.cpp
)


############################################################## gtest
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)

--- SNIP --- (test configure stuff from the github)

endif()
############################################################## gtest




add_executable(${This} ${Sources})

target_link_libraries(${This} PUBLIC
    ${TORCH_LIBRARIES}
    gtest
    gtest_main
    ${PROJECT_NAME}
)

target_include_directories(${This} PUBLIC ${TORCH_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
target_compile_options(${This} PUBLIC ${TORCH_CXX_FLAGS})

add_test(
    NAME ${This}
    COMMAND ${This}
)

当我在发布版本中编译时很好。如果我的链接目录中不包含“${TORCH_LIBRARIES}”也可以。但是,当我链接到 Torch 并在调试下进行测试时,我得到以下信息:

.../test/cpp/WorldMapTests.cpp:5: undefined reference to `testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)'

我认为这是因为 torch 使用了测试并且是在发布版本下编译的。我在torch下的lib文件夹中手动找到了一个gtest.so,删除了看看能不能解决,但是没有。

【问题讨论】:

  • 你好像没有链接gtest,gtest_main只提供应用入口点
  • 谢谢。我现在尝试添加它。仍然没有运气:(。
  • PROJECT_NAME 变量的内容是什么?为什么您使用${PROJECT_NAME} 进行链接?您的代码没有定义名称为 ${PROJECT_NAME} 的库。
  • 我省略了我的主要 cmake,因为我不确定它是否相关?它包含项目定义。我是新手,但我认为它会提供更多控制权,因此我以后可以轻松地编辑名称。
  • 对不起,我误解了你的问题。在我的主 cmake 中,我定义了一个名为“${PROJECT_NAME}”的共享库,我想稍后在我的 gtest 中进行测试。现在看看它,我可能只是用测试源编译它而不是链接它。尽管有或没有它都不会改变错误。

标签: c++ cmake googletest libtorch


【解决方案1】:

经过相当多的研究,我能够做对了。只是想为遇到相同问题的任何人发布更新。问题来自 libtorch 在发布模式下编译而不是调试导致与 gtest 库发生冲突。在 Windows 上,这很容易解决,因为 torch 提供了预编译的开发库。但是,在 linux 上,您需要从源代码编译您自己的,然后链接它。我使用以下指南为 c++ "https://kezunlin.me/post/54e7a3d8/" 编译了火炬。

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多