0

I try to compile my project including google benchmark. This is the project structure:

$proj:
|_ benchmark
|_ include
|_ src
|_ CMakeLists.txt

Into the folder include I add google benchmark as submodule linked to the latest release @73d4d5e. Instead, inside the benchmark folder I put these two files:

benchmark/first.cpp (It is the repository usage example)

#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
  for (auto _ : state)
    std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
  std::string x = "hello";
  for (auto _ : state)
    std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();

CMakeLists.txt

file(GLOB_RECURSE ALL_BENCH_CPP *.cpp)

foreach(ONE_BENCH_CPP ${ALL_BENCH_CPP})

  get_filename_component(ONE_BENCH_EXEC ${ONE_BENCH_CPP} NAME_WE)
  set(TARGET_NAME Benchmark_${ONE_BENCH_EXEC})

  add_executable(${TARGET_NAME} ${ONE_BENCH_CPP})
  set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME ${ONE_BENCH_EXEC}) 
  target_include_directories(${TARGET_NAME} PUBLIC ../include/benchmark/include ${CMAKE_SOURCE_DIR}/src)
  target_link_libraries(
    ${TARGET_NAME}
    PUBLIC
    ${CMAKE_THREAD_LIBS_INIT}
  )

endforeach()

In the CMakeLists.txt file on the root of the project, I add the compile flag:

[...]
SET(CMAKE_CXX_FLAGS  "-pedantic -Wall -Wextra -lbenchmark -lpthread -fopenmp")
[...]

but the flag -lbenchmark is not found. The compilation return this error:

ld: library not found for -lbenchmark

If I omit the flag, during the compilation I have this output:

Undefined symbols for architecture x86_64: [build] "__ZN9benchmark10InitializeEPiPPc", referenced from: [build] _main in first.cpp.o [build] "__ZN9benchmark22RunSpecifiedBenchmarksEv", referenced from: [build] _main in first.cpp.o [build] "__ZN9benchmark27ReportUnrecognizedArgumentsEiPPc", referenced from: [build] _main in first.cpp.o [build] "__ZN9benchmark5State16StartKeepRunningEv", referenced from: [build] __ZL17BM_StringCreationRN9benchmark5StateE in first.cpp.o [build] __ZL13BM_StringCopyRN9benchmark5StateE in first.cpp.o [...]

th3g3ntl3man
  • 1,926
  • 5
  • 29
  • 50

1 Answers1

0

https://github.com/google/benchmark#usage-with-cmake

You'll want another target_link_libraries pointing at the benchmark library rather than trying to set it using -l. CMake will take care of the command line for you then.

dma
  • 1,758
  • 10
  • 25
  • If I add `target_link_libraries(${TARGET_NAME} benchmark::benchmark)` after the other `targer_link_library` I have this error: `CMake Error at benchmark/CMakeLists.txt:8 (add_executable):Target "Benchmark_small_dyna_dbg" links to target "benchmark::benchmark" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?`. And the `find_packege` not the resolvw the problem. – th3g3ntl3man Dec 14 '20 at 20:49
  • this seems like a more general cmake configuration issue than specific to benchmark. Either the `find_package` you've added isn't finding the package (i'd expect an error in your cmake logs) or maybe you have an older benchmark version when the target was just `benchmark` rather than `benchmark::benchmark`? – dma Dec 16 '20 at 12:41
  • I add to my project ony the latest relase of benchmar (with git submodule). I also add the gtest library also as a git submodule. This could be the problem? Can yoi give me an example of cmake for my project structure? – th3g3ntl3man Dec 16 '20 at 22:21