0

我尝试编译我的项目,包括google benchmark. 这是项目结构:

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

进入include我添加google benchmarksubmodule链接到最新版本@73d4d5e的文件夹中。相反,在benchmark文件夹中,我放置了这两个文件:

benchmark/first.cpp(它是存储库使用示例)

#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()

CMakeLists.txt项目根目录的文件中,我添加了编译标志:

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

-lbenchmark找不到标志。编译返回此错误:

ld: 找不到 -lbenchmark 的库

如果我省略标志,在编译期间我有这个输出:

架构 x86_64 的未定义符号:[build] "__ZN9benchmark10InitializeEPiPPc",引用自:[build] _main in first.cpp.o [build] "__ZN9benchmark22RunSpecifiedBenchmarksEv",引用自:[build] _main in first.cpp.o [build]" __ZN9benchmark27ReportUnrecognizedArgumentsEiPPc”,引用自:[build] _main in first.cpp.o [build]“__ZN9benchmark5State16StartKeepRunningEv”,引用自:[build] __ZL17BM_StringCreationRN9benchmark5StateE in first.cpp.o [build] __ZL13BM_StringCopyRN9benchmark5StateE in first.cpp.o [... ]

4

1 回答 1

0

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

您需要另一个target_link_libraries指向基准库,而不是尝试使用-l. CMake 将为您处理命令行。

于 2020-12-14T11:33:37.283 回答