Integrate Google Benchmark with CMake
mainYou can consume Google Benchmark in a CMake project in three ways:
- Installed version: Use
find_package(benchmark REQUIRED)and link againstbenchmark::benchmarkorbenchmark::benchmark_main. - Source tree (add_subdirectory): Add the repository to your project and use
add_subdirectory(path/to/benchmark). It is recommended to disable tests and install rules when doing this. - Object Libraries: If sharing benchmarks through an intermediate CMake target, use an
OBJECTlibrary to ensureBENCHMARKregistrations are correctly linked into the final executable.
Link against benchmark::benchmark if you define your own main. Link against benchmark::benchmark_main to use the default entry point.
# Using an installed version
find_package(benchmark REQUIRED)
target_link_libraries(MyTarget benchmark::benchmark)
# Using add_subdirectory (recommended configuration)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(third_party/benchmark)
target_link_libraries(MyTarget benchmark::benchmark)