4

我正在尝试编译一个依赖于 Libtorch (Pytorch) 库的谷歌基准测试。我已经安装了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 与谷歌基准一起使用?如果不是,我该如何正确处理?

编辑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
4

1 回答 1

0

你使用find_package正确,但你误用了它的定义。

find_package命令不导出XY_LIBRARIES,并且XY_INCLUDE_DIRECTORIES在使用导出的包时。如果您打印这些变量的值,您会注意到它。

相反,现代 CMake 包导出目标。这适用于所有使用包导出而不是查找模块的项目。要链接到导入的目标,您应该使用target_link_libraries

target_link_libraries(PROJECT_NAME PRIVATE benchmark::benchmark)

这将为目标内的所有 CPP 添加所有必要的标志,PROJECT_NAME以便能够使用谷歌基准。

您可以删除 flags 变量(不要触摸CMAKE_CXX_FLAGS和链接标志!)并改用更强大的构造,例如基于目标的target_link_libraries.

于 2019-11-11T19:17:49.780 回答