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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24295876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:41:25  来源:igfitidea点击:

CMake cannot find GoogleTest required library in Ubuntu

c++ubuntucmakegoogletestqnx

提问by Erich

Similar issue here.

类似的问题在这里

This is my CMakeLists.txt:

这是我的 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)

And my foo.cpp:

还有我的 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();
}

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

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

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)

/usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake 中的 CMake 错误:97(MESSAGE):找不到 GTest(缺少:GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)

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

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

What gives?

是什么赋予了?

回答by detrick

Google test was probably not properly installed (libgtest-devmay 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/

Google test 可能没有正确安装(libgtest-dev可能只安装需要编译的源文件)。我遇到了同样的问题,我按照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

This worked for me.

这对我有用。

回答by mrts

As explained by @detrick, the Ubuntu package libgtest-devonly installs sources, so you need to build and install the libraries yourself.

正如@detrick 所解释的,Ubuntu 包libgtest-dev只安装源代码,因此您需要自己构建和安装库。

However, there is a much simpler way for building and installing since Ubuntu 18.04 than the manual commands in other answers:

但是,从 Ubuntu 18.04 开始,有一种比其他答案中的手动命令更简单的构建和安装方法:

sudo apt install libgtest-dev build-essential cmake
cd /usr/src/googletest
sudo cmake .
sudo cmake --build . --target install

回答by Arturo Ruiz Ma?as

Some time ago I created a dockerfile and it helps me to keep a kind of recipe for installing later on google test on my systems:

前段时间我创建了一个 dockerfile,它可以帮助我保留一种方法,以便稍后在我的系统上进行 google test 安装:

apt-get install -y git g++ make cmake 
git clone https://github.com/google/googletest.git
cd googletest
mkdir gbuild && cd gbuild && cmake .. && make
cp -r googletest/include/gtest /usr/local/include
cp gbuild/googlemock/gtest/lib*.a /usr/local/lib
cp gbuild/googlemock/lib*.a /usr/local/lib

I hope it helps :)

我希望它有帮助:)