【发布时间】:2020-12-13 22:15:43
【问题描述】:
我尝试编译我的项目,包括google benchmark。这是项目结构:
$proj:
|_ benchmark
|_ include
|_ src
|_ CMakeLists.txt
在文件夹include 中,我将google benchmark 添加为链接到最新版本@73d4d5e 的submodule。相反,在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”,引用自: [构建] first.cpp.o 中的 _main [build] "__ZN9benchmark22RunSpecifiedBenchmarksEv",引用自: [构建] first.cpp.o 中的 _main [build] "__ZN9benchmark27ReportUnrecognizedArgumentsEiPPc",引用自: [构建] first.cpp.o 中的 _main [build] "__ZN9benchmark5State16StartKeepRunningEv",引用自: [构建] first.cpp.o 中的 __ZL17BM_StringCreationRN9benchmark5StateE [构建] first.cpp.o 中的 __ZL13BM_StringCopyRN9benchmark5StateE [...]
【问题讨论】:
标签: c++ cmake microbenchmark google-benchmark