c++cmakegoogle-benchmark

Google Benchmark as a cmake dependency


I'm trying to compile a google benchmark with a dependency on the Libtorch (Pytorch) library. I have installed Google Benchmark with make install so to my understanding i should be able to use find_package() to add both dependencies. Finally I add some compiler flags.

Here is my 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)

It should be noted that with and without the GCC_LINK_FLAGS, I always get a fatal error: 'benchmark/benchmark.h' file not found. My code was compiling and running when it only depended on Pytorch. Is it possible to use find_package with google benchmark? If not how can I go about this properly?

EDIT 1:

here are the commands I have run.

$ 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

Solution

  • You use find_package correctly, but you misuse what it defines.

    The find_package command don't export XY_LIBRARIES and XY_INCLUDE_DIRECTORIES when using exported packages. You'll notice it if you print the values of those variable.

    Instead, modern CMake packages export targets. This is true for all projects that uses package exportation instead of find modules. To link to an imported target, you should use target_link_libraries:

    target_link_libraries(PROJECT_NAME PRIVATE benchmark::benchmark)
    

    This will add all necessary flags for all CPP insinde the PROJECT_NAME target to be able to use google benchmark.

    You can drop flags variable (don't toutch CMAKE_CXX_FLAGS and link flags!) and use more robust constructs instead, like the target based target_link_libraries.