mathguy
mathguy

Reputation: 153

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

Upvotes: 12

Views: 10979

Answers (5)

Dang_Ho
Dang_Ho

Reputation: 351

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 main) # need main 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

Upvotes: 13

rustyx
rustyx

Reputation: 85442

The following works with VC++ 2022, also in C++20 mode.

cmake_minimum_required(VERSION 3.20)

project(myproject C CXX)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type")   # Default build type

set(CMAKE_CXX_STANDARD 20)           # choose 14, 17, 20
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS NO)

include(FetchContent)
set(BENCHMARK_ENABLE_TESTING off)    # to suppress benchmark internal tests
FetchContent_Declare(googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG release-1.11.0)      # or "main" for latest
FetchContent_Declare(googlebenchmark
        GIT_REPOSITORY https://github.com/google/benchmark.git
        GIT_TAG v1.6.0)              # or "main" for latest
FetchContent_MakeAvailable(googletest googlebenchmark)

add_executable(mybench mybench.cpp)
target_link_libraries(mybench PRIVATE benchmark::benchmark)

If you get link errors LNK2038, then add this as well:

if (MSVC)
  set(gtest_force_shared_crt on)     # needed to avoid error LNK2038
endif()

Upvotes: 4

pattakosn
pattakosn

Reputation: 395

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.

edit 2023: since CMake 3.11, one can use FetchContent_Declare and FetchContent_MakeAvailable, as shown in the excellent An Introduction to Modern CMake . One can alternatively use git submodules (or directly download the project) and simply use include(path).

Upvotes: 5

MikhailFediakov
MikhailFediakov

Reputation: 1

You can simply do that:

cmake_minimum_required(VERSION 3.16)

project(googletest-download NONE)

include(ExternalProject)

ExternalProject_Add(googletest
        GIT_REPOSITORY    https://github.com/google/googletest.git
        GIT_TAG           master
        SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
        BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
        CONFIGURE_COMMAND ""
        BUILD_COMMAND     ""
        INSTALL_COMMAND   ""
        TEST_COMMAND      ""
        )

reload cmake project and run build, if you use Clion

Upvotes: -1

Craig Nicholson
Craig Nicholson

Reputation: 1241

After hunting for a working solution for Visual Studio 2019 I eventually got it working. I created a example to help others.

Upvotes: 2

Related Questions