12

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

Ðаn
  • 10,934
  • 11
  • 59
  • 95
mathguy
  • 153
  • 2
  • 8

5 Answers5

11

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

double-beep
  • 5,031
  • 17
  • 33
  • 41
Dang_Ho
  • 323
  • 3
  • 11
  • I can't make an edit currently, but I had to change the `GIT_TAG` for both repos to `main` such that the example is coherent with the philofophy of "living at head" that both repos apply. – Uchendu Jun 30 '22 at 19:53
5

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).

pattakosn
  • 365
  • 4
  • 13
3

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()
Anton Kesy
  • 119
  • 7
rustyx
  • 80,671
  • 25
  • 200
  • 267
2

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

Craig Nicholson
  • 1,241
  • 1
  • 11
  • 21
-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