Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build and link google benchmark using cmake in windows

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

like image 447
mathguy Avatar asked Mar 27 '19 11:03

mathguy


2 Answers

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

like image 61
Dang_Ho Avatar answered Sep 23 '22 14:09

Dang_Ho


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.

like image 42
pattakosn Avatar answered Sep 20 '22 14:09

pattakosn