I am trying to build google-benchmark and use it with my library using cmake. I have managed to build google-benchmark and run all its tests successfully using cmake. I am unfortunately unable to link it properly with my c++ code in windows using cmake or cl.
the problem I think is that google-benchmark builds the library inside the src folder, i.e it is build in src/Release/benchmark.lib now i cannot point to it in cmake if I use ${benchmark_LIBRARIES} it looks for the library in the Release folder outside src, as this is the usual place all the libraries are build. and it is difficult to find examples which work in windows.
here are two ways which I have tried, both can build the library and all the tests run but I cannot point to the library to target_link_library properly
include(ExternalProject)
ExternalProject_Add(googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-build"
CONFIGURE_COMMAND ${CMAKE_COMMAND} -B ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-build -S ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-src -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON
BUILD_COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-build --config Release
INSTALL_COMMAND ""
TEST_COMMAND ${CMAKE_CTEST_COMMAND} ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-src ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-build --build-config Release
)
and
ExternalProject_Add(googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG master
PREFIX googlebenchmark
CMAKE_ARGS -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON
BUILD_COMMAND ${CMAKE_COMMAND} --build . --config Release
INSTALL_COMMAND ""
TEST_COMMAND ${CMAKE_CTEST_COMMAND} --build-config Release
)
how do i link it to my c++ file try.cpp after this
CMakeLists.txt as below
cmake_minimum_required(VERSION 3.14)
project(g_benchmark)
enable_testing()
include(FetchContent)
## Project-wide setup
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
# Externally provided libraries
FetchContent_Declare(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.10.x)
FetchContent_Declare(googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG master) # need master for benchmark::benchmark
FetchContent_MakeAvailable(
googletest
googlebenchmark)
add_executable(g_benchmark main.cpp)
target_link_libraries(g_benchmark benchmark::benchmark)
Require cmake version above 3.14
$ cmake .
$ cmake --build .
Reference: https://github.com/hohaidang/CPP_Basic2Advance/tree/master/Learning/CMake/g_benchmark
I came here looking for a copy paste solution myself but I do not see any clear solution while I see that there are a lot of people looking at this page, so here is what I did.
I haven't used ExternalProject_Add
but I would be happy to assist you if you pointed me to a complete running test example that I could check out.
This is what I used in my project
include(FetchContent)
FetchContent_Declare(googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark
)
FetchContent_MakeAvailable(googlebenchmark)
target_link_libraries(bench benchmark::benchmark)
I haven't yet tried it on windows but I will do it next time I boot into win at home. I tried it on several linux machines though.
I hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With