mj

Using Google Benchmark and GTest in a CMake Project

20251019

This is a a neat way to use Google Benchmark and GTest in a CMake project.

Rember to update the version numbers.

    
include(FetchContent)

set(BENCHMARK_ENABLE_TESTING off)

FetchContent_Declare(
  googletest
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG v1.17.0
)

FetchContent_Declare(
  googlebenchmark
    GIT_REPOSITORY https://github.com/google/benchmark.git
    GIT_TAG v1.9.4
)

FetchContent_MakeAvailable(
  googletest
  googlebenchmark
)

enable_testing()

add_executable(source_code_test)
target_sources(
  source_code_test
  PRIVATE
    source_code.test.cpp
)
target_link_libraries(
  source_code_test
  PRIVATE
    gtest_main
    source_code
)

include(GoogleTest)
gtest_discover_tests(source_code_test)

add_executable(source_code_benchmark)
target_sources(
  source_code_benchmark
  PRIVATE
    source_code.benchmark.cpp
)

target_link_libraries(
  source_code_benchmark
  PRIVATE
    source_code
    pthread
    benchmark::benchmark
    benchmark::benchmark_main
)