less than 1 minute read

Usecase: You want to make use of some system-wide installed library.

Install the library using your linux package manager. Most installed libraries can be found by CMake via the find_package(<NAME> REQUIRED) command.


Example to get GTest up and running:

# file: CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(my_project)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(GTest REQUIRED)    # <----------- !!!
add_executable(my_test myTest.cpp)
enable_testing()
target_link_libraries(my_test PRIVATE GTest::gtest_main)
gtest_discover_tests(my_test)

// file: myTest.cpp
#include <gtest/gtest.h>

TEST(HelloTest, BasicAssertions) {
  EXPECT_STRNE("hello", "world"); // Expect two strings not to be equal.
  EXPECT_EQ(7 * 6, 42); // Expect equality.
}


Source: https://google.github.io/googletest/quickstart-cmake.html