CMake cannot find GoogleTest required library in Ubuntu

后端 未结 4 1287
慢半拍i
慢半拍i 2020-12-14 07:52

Similar issue here.

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
include_directories($         


        
相关标签:
4条回答
  • 2020-12-14 08:16

    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
    

    This worked for me.

    0 讨论(0)
  • 2020-12-14 08:16

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

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

    sudo apt install libgtest-dev build-essential cmake
    cd /usr/src/googletest
    sudo cmake .
    sudo cmake --build . --target install
    
    0 讨论(0)
  • 2020-12-14 08:25

    ntox86-c++ looks like a cross-compiler, libgtest-dev package does not provide compiled library for the target platform (QNX).

    Since year 2014 compiled libraries was dropped from libgtest-dev and has been added again in Ubuntu-20.04 focal, so find_package(GTest REQUIRED) does not work on Ubuntu-16.04 xenial and Ubuntu-18.04 bionic. The reason is given in /usr/share/doc/googletest/README.Debian (/usr/share/doc/libgtest-dev/README.Debian) and in e.g. in /usr/src/googletest/googletest/docs/V1_7_FAQ.md "Why is it not recommended to install a pre-compiled copy of Google Test (for example, into /usr/local)" section. Difference in compiler flags for the library and for a test could generate incompatible executable code. The problem with 18.04 and 16.04 is the reason why I have decided to add another answer to the old question.

    add_subdirectory could be used to compile gtest provided by system package

    set(GTest_ROOT /usr/src/googletest/googletest)
    add_subdirectory(${GTest_ROOT}
            "${CMAKE_CURRENT_BINARY_DIR}/googletest" EXCLUDE_FROM_ALL)
    
    add_executable(test test.cpp)
    target_link_libraries(test gtest_main)
    # or just gtest if main function is defined
    

    Instead of using system package for googletest sources there are at least 2 variants how to obtain particular version from git (besides obvious submodule), see

    • https://cmake.org/cmake/help/latest/module/FetchContent.html
    • https://github.com/google/googletest/blob/master/googletest/README.md
    0 讨论(0)
  • 2020-12-14 08:33

    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:

    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 :)

    0 讨论(0)
提交回复
热议问题