【问题标题】:C++ GTest error "ld: library not found for -lgtest" on MacOS在 MacOS 上 C++ GTest 错误“ld: library not found for -lgtest”
【发布时间】:2020-12-10 08:51:00
【问题描述】:

我尝试将 gtest 包含到我的 C++ 项目中。我使用的 IDE 是 CLion。我的 CMAKE 里有这个

include_directories(/usr/local/include)
include_directories(/usr/local/lib)

这在我正在处理的文件中

#include "gtest/gtest.h"

我已经使用这些命令在终端中安装了 gtest:

git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
make install

我认为使用 gtest 就足够了,但编译器另有说明。这是我收到的错误消息

ld: library not found for -lgtest
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人知道我该如何解决吗?感谢任何尝试!

解决方案:我按照 googletest github 页面上的 README.md 文件中的说明进行操作,它就像一个魅力。 https://github.com/google/googletest/blob/master/googletest/README.md

【问题讨论】:

  • 调查make install 的安装位置。
  • 欢迎来到 Stack Overflow!请在您的问题帖子中提供完整 CMake 文件。这将有助于使问题更加清晰,以便我们提供更好的解决方案。
  • 嗨@drescherjm,我很确定在“/usr/local/include”中,我什至尝试从文件“/Users/myusername/googletest/googletest/include”中复制路径
  • 然后检查/usr/local/lib/的库

标签: c++ macos cmake googletest clion


【解决方案1】:

通常gtest是一个静态库,所以需要指定libgtest.a,类似这样:

g++ -std=c++17 -O3 -pedantic-errors test.cpp /usr/lib/libgtest.a -o test

【讨论】:

  • 嗨@artm,感谢您的建议。我在终端中尝试该命令但收到以下错误:clang: error: no such file or directory: 'test.cpp' clang: error: no such file or directory: '/usr/lib/libgtest.a' clang: error: no input files 我需要在特定目录中运行它吗?我是否需要更改这些文件名以匹配我的实际文件?
【解决方案2】:

我按照来自 googletest github 页面的 README.md 文件上的指示进行操作,它的工作原理就像一个魅力。 https://github.com/google/googletest/blob/master/googletest/README.md

【讨论】: