CMake 在 Ubuntu 中找不到 GoogleTest 所需的库

2021-12-26 00:00:00 ubuntu cmake c++ googletest qnx

类似问题此处.

这是我的 CMakeLists.txt:

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# Add test cpp file
add_executable(foo foo.cpp)

# Link test executable against gtest & gtest_main
target_link_libraries(foo ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)

还有我的 foo.cpp:

And my foo.cpp:

#include <gtest/gtest.h>

TEST(sample_test_case, sample_test)
{
    EXPECT_EQ(1, 1);
}

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

现在,使用 g++ 编译器时一切正常.但是,在尝试使用 QNX 的编译器 ntox86-c++ 时,我遇到了这个问题:

Now, all works fine when using the g++ compiler. However, when attempting to use QNX's compiler, ntox86-c++, I run into this problem:

CMake 错误在/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE):找不到 GTest(丢失:GTEST_LIBRARY GTEST_INCLUDE_DIRGTEST_MAIN_LIBRARY)

CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE): Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)

我在 Ubuntu 上使用 ntox86-c++ 编译器、googletest 和 cmake-gui.

I am on Ubuntu using the ntox86-c++ compiler, googletest, and cmake-gui.

是什么?

推荐答案

Google test 可能没有正确安装(libgtest-dev 可能只安装需要编译的源文件).我遇到了同样的问题,我按照 http://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

Google test was probably not properly installed (libgtest-dev may install only source files that needed to be compiled). I had the same problem and I followed the instructions from http://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

sudo apt-get install libgtest-dev
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make

#copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib

这对我有用.

相关文章