首页 > 解决方案 > 运行第一个 gtest 示例时出现问题

问题描述

我安装了googletest

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

如此所述。我现在正在尝试编译以下代码

#include <gtest/gtest.h>

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS();
}

g++ --std=c++17 gt.cpp -o gt( g++ (GCC) version 10.2.0),我得到

gt.cpp:1:10: fatal error: gtest/gtest.h: No such file or directory
    1 | #include <gtest/gtest.h>
      |          ^~~~~~~~~~~~~~~
compilation terminated.

似乎缺少包含路径,但我不知道该放入哪个路径。

clang++ -std=c++17 gt.cpp -o gt,我得到

Undefined symbols for architecture x86_64:
  "testing::InitGoogleTest(int*, char**)", referenced from:
      _main in gt-7a908b.o
  "testing::UnitTest::GetInstance()", referenced from:
      RUN_ALL_TESTS() in gt-7a908b.o
  "testing::UnitTest::Run()", referenced from:
      RUN_ALL_TESTS() in gt-7a908b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

仅供参考,我不是 MACOS 11.6。

标签: c++installationcompiler-errorsincludegoogletest

解决方案


推荐阅读