Ubuntu 17.04环境下GTest与GMock安装及CMake配置咨询
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:
You should see links pointing to compiled static libraries (likels -l /usr/lib/libgtest* /usr/lib/libgmock*libgtest.aorlibgtest.so) in/usr/src/gtest/buildand/usr/src/googlemock/buildrespectively. - If links are missing or broken, re-run your compilation steps and re-create the links:
For GTest:
For GMock: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.acd /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 (likegtest/gtest.horgmock/gmock.h).target_link_libraries(...): Links your test executable to the GTest/GMock static libraries, pluspthread—GTest relies on this for thread-related functionality on Linux.- The optional testing block lets you run tests with
make testafter 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

