You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 17.04环境下GTest与GMock安装及CMake配置咨询

Integrating GTest/GMock with Your Ubuntu 17.04 Project

Hey there! It looks like you’ve got the foundational installation steps for GTest and GMock wrapped up on Ubuntu 17.04, but your current CMakeLists.txt is missing the critical configuration to actually link those libraries to your project. Let’s fix that and make sure your setup is solid.

First: Verify Your Installation

Before tweaking the CMake config, let’s confirm your libraries are properly accessible:

  • Run these commands to check if your soft links are set up correctly:
    ls -l /usr/lib/libgtest* /usr/lib/libgmock*
    
    You should see links pointing to compiled static libraries (like libgtest.a or libgtest.so) in /usr/src/gtest/build and /usr/src/googlemock/build respectively.
  • If links are missing or broken, re-run your compilation steps and re-create the links:
    For GTest:
    cd /usr/src/gtest
    sudo cmake CMakeLists.txt
    sudo make
    sudo ln -s /usr/src/gtest/libgtest.a /usr/lib/libgtest.a
    sudo ln -s /usr/src/gtest/libgtest_main.a /usr/lib/libgtest_main.a
    
    For GMock:
    cd /usr/src/googlemock
    sudo cmake CMakeLists.txt
    sudo make
    sudo ln -s /usr/src/googlemock/libgmock.a /usr/lib/libgmock.a
    sudo ln -s /usr/src/googlemock/libgmock_main.a /usr/lib/libgmock_main.a
    

Fix Your CMakeLists.txt

Your current config only sets up the project name and minimum CMake version—we need to add logic to find the GTest/GMock libraries, include their headers, and link them to your test executable. Here’s a complete, updated version:

project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6)

# Locate GTest and GMock libraries
find_package(GTest REQUIRED)
find_package(GMock REQUIRED)

# Include header directories for GTest/GMock
include_directories(${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})

# Replace "your_test_file.cpp" with your actual test source file name
add_executable(runTests your_test_file.cpp)

# Link the test executable to GTest, GMock, and pthread (required for Linux thread safety)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES} pthread)

# Optional: Enable CTest integration to run tests with `make test`
enable_testing()
add_test(NAME ChapterFirstTests COMMAND runTests)

Key Explanations:

  • find_package(GTest REQUIRED): Tells CMake to locate your GTest installation; it will fail if GTest isn’t found, confirming your setup is correct.
  • include_directories(...): Ensures your compiler can find GTest/GMock headers (like gtest/gtest.h or gmock/gmock.h).
  • target_link_libraries(...): Links your test executable to the GTest/GMock static libraries, plus pthread—GTest relies on this for thread-related functionality on Linux.
  • The optional testing block lets you run tests with make test after building, automating test execution.

Building Your Project

Once you’ve updated the CMakeLists.txt, run these commands to build and run your tests:

mkdir build && cd build
cmake ..
make
./runTests  # Runs your test executable directly
# Or, if you added the test block:
make test

If you hit issues like "undefined reference" errors, double-check that your soft links point to the correct compiled libraries, and that your test files include the necessary GTest/GMock headers (e.g., #include <gtest/gtest.h> and #include <gmock/gmock.h>).

内容的提问来源于stack exchange,提问作者Steve Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:26:02