Google Benchmark作为cmake依赖项

时间:2019-11-11 19:04:16

标签: c++ cmake google-benchmark

我正在尝试编译依赖于Libtorch(Pytorch)库的google基准。我已将Google Benchmarkmake install一起安装,因此据我所知,我应该能够使用find_package()添加两个依赖项。最后,我添加了一些编译器标志。

这是我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(learned_b VERSION 1.0)
add_executable(PROJECT_NAME learned_benchmark.cpp)

find_package(Torch REQUIRED)
find_package(benchmark REQUIRED)
target_link_libraries(PROJECT_NAME "${TORCH_LIBRARIES}")
target_include_directories(PROJECT_NAME PUBLIC "${benchmark_INCLUDE_DIRS}")
target_link_libraries(PROJECT_NAME "${benchmark_LIBRARIES}")

SET(GCC_LINK_FLAGS    "-isystem /Users/yhr/Programs/benchmark/include -lbenchmark -pthread")
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_LINK_FLAGS}")
set_property(TARGET PROJECT_NAME PROPERTY CXX_STANDARD 11)

请注意,无论是否使用GCC_LINK_FLAGS,我总是 严重错误:找不到'benchmark / benchmark.h'文件。仅依靠Pytorch时,我的代码正在编译并运行。是否可以将find_package与Google基准一起使用?如果没有,我该如何正确处理?

编辑1:

这是我运行的命令。

$ cd build
$ cmake -DCMAKE_PREFIX_PATH='/Users/yhr/Programs/libtorch;/Users/yhr/Programs/benchmark' ..
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter
 $ cd ..
 $ make VERBOSE=1
/usr/local/Cellar/cmake/3.15.4/bin/cmake -S/Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter -B/Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.15.4/bin/cmake -E cmake_progress_start /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter/CMakeFiles /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/PROJECT_NAME.dir/build.make CMakeFiles/PROJECT_NAME.dir/depend
cd /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter && /usr/local/Cellar/cmake/3.15.4/bin/cmake -E cmake_depends "Unix Makefiles" /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter/CMakeFiles/PROJECT_NAME.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/PROJECT_NAME.dir/build.make CMakeFiles/PROJECT_NAME.dir/build
[ 50%] Building CXX object CMakeFiles/PROJECT_NAME.dir/learned_benchmark.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -DAT_PARALLEL_OPENMP=1 -D_THP_CORE -I/Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter -isystem /Users/yhr/programs/libtorch/include -isystem /Users/yhr/programs/libtorch/include/torch/csrc/api/include  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -mmacosx-version-min=10.14   -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wno-write-strings -Wno-unknown-pragmas -Wno-missing-braces -std=gnu++11 -o CMakeFiles/PROJECT_NAME.dir/learned_benchmark.cpp.o -c /Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter/learned_benchmark.cpp
/Users/yhr/Programs/learnedbloomfilters/OpenBloomFilter/learned_benchmark.cpp:4:10: fatal error: 'benchmark/benchmark.h' file not found
#include <benchmark/benchmark.h>
         ^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/PROJECT_NAME.dir/learned_benchmark.cpp.o] Error 1
make[1]: *** [CMakeFiles/PROJECT_NAME.dir/all] Error 2
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:0)

您正确使用了relative,但误用了它的定义。

在使用导出的软件包时,find_package命令不会导出find_packageXY_LIBRARIES。如果您打印这些变量的值,则会注意到它。

相反,现代CMake软件包导出目标。对于所有使用包导出而不是查找模块的项目,都是如此。要链接到导入的目标,应使用XY_INCLUDE_DIRECTORIES

target_link_libraries

这将为所有target_link_libraries(PROJECT_NAME PRIVATE benchmark::benchmark) 目标中的CPP添加所有必要的标记,以便能够使用Google基准。

您可以删除flags变量(不要拖拉PROJECT_NAME和链接标志!),而改用更健壮的结构,例如基于目标的CMAKE_CXX_FLAGS