我尝试编译我的项目,包括google benchmark
. 这是项目结构:
$proj:
|_ benchmark
|_ include
|_ src
|_ CMakeLists.txt
进入include
我添加google benchmark
为submodule
链接到最新版本@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 [... ]