【C++】GTEST && google/benchmark工具安装

李勇
2023-12-01

最近在做一些C++相关事情,用的是C++编译成so库,最后引入到android播放器当中的,为了对C++代码进行快速迭代以及做性能测试,准备引入GTEST一个google出品的C++测试库,以及google/benchmark库用来做性能测试

注:其实按照google benchmark在github页面的安装教程来就可以了,这篇文章只是做一个简单的更新,算是一个安装笔记

google benchmark依赖gtest,所以正常安装google benchmark之后,会自动安装gtest

安装

  1. 在项目的根目录下面执行下面的命令,会在根目录下面生成一个benchmark目录
# Check out the library.
# Benchmark requires Google Test as a dependency. Add the source tree as a subdirectory.
git clone https://github.com/google/benchmark.git
git clone https://github.com/google/googletest.git benchmark/googletest
# Go to the library root directory
cd benchmark
cmake -E make_directory "build"
# cmake -E chdir "build" cmake -DCMAKE_BUILD_TYPE=Release ../
# or, starting with CMake 3.13, use a simpler form:
cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build"
cmake --build "build" --config Release

# check install
# cmake -E chdir "build" ctest --build-config Release
# install global
# sudo cmake --build "build" --config Release --target install
  1. CMakeList.txt中的引用
cmake_minimum_required(VERSION 3.19)
project(leetcode)
set(CMAKE_CXX_STANDARD 14)

add_subdirectory(benchmark)

# benchmark
set(BENCHMARK_SRC
        bench_test.cpp)
add_executable(bench ${BENCHMARK_SRC})
target_link_libraries(bench benchmark benchmark_main pthread gtest_main)
# 用来引入头文件
# target_include_directories()

# gtest
add_executable(lgtest main_gtest.cpp)
target_link_libraries(lgtest gtest_main)

==== 下面的方法太过麻烦,而且重复安装(忽略) ====

安装

  • 找到一种方便的安装方式,不会引入额外的文件
  • 总共需要3个CMakeLists.txt文件(包含项目主CMakeLists.txt),与主项目的CMakeLists.txt同级目录
    CMakeLists.gtest
    CMakeLists.benchmark
    CMakeLists.txt: 主项目的CMakeLists.txt文件
# CMakeLists.gtest文件
cmake_minimum_required(VERSION 2.8.12)

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        ""
)
# CMakeLists.benchmark文件
cmake_minimum_required(VERSION 2.8.12)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(benchmark
        GIT_REPOSITORY      https://github.com/google/benchmark.git
        GIT_TAG             master
        SOURCE_DIR          "${CMAKE_CURRENT_BINARY_DIR}/benchmark-src"
        BINARY_DIR          "${CMAKE_CURRENT_BINARY_DIR}/benchmark-build"
        CONFIGURE_COMMAND   ""
        BUILD_COMMAND       ""
        INSTALL_COMMAND     ""
        TEST_COMMAND        ""
)
# CMakeLists.txt当中引入这一部分
message("================= gtest =================")
if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
    configure_file(CMakeLists.txt.gtest googletest-download/CMakeLists.txt)
    message("xxxx" ${CMAKE_COMMAND} ${CMAKE_GENERATOR} ${CMAKE_CURRENT_BINARY_DIR})
    execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
    if (result)
        message(FATAL_ERROR "CMake step for googletest failed: ${result}")
    endif ()

    execute_process(COMMAND ${CMAKE_COMMAND} --build .
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
    if (result)
        message(FATAL_ERROR "Build step for googletest failed: ${result}")
    endif ()
endif()

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
        ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
        EXCLUDE_FROM_ALL)

message("================= benchmark =================")
if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/benchmark-download)
    configure_file(CMakeLists.txt.benchmark benchmark-download/CMakeLists.txt)
    execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/benchmark-download)

    # cmake -DCMAKE_BUILD_TYPE=Release -DGOOGLETEST_PATH=../googletest-src ../benchmark-src
    execute_process(COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release -DGOOGLETEST_PATH=${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/benchmark-src
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/benchmark-build)
    if (result)
        message(FATAL_ERROR "CMake step for google benchmark failed: ${result}")
    endif ()

    # cmake --build . --config Release
    execute_process(COMMAND ${CMAKE_COMMAND} --build . --config Release
            RESULT_VARIABLE result
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/benchmark-download)
    if (result)
        message(FATAL_ERROR "Build step for google benchmark failed: ${result}")
    endif ()
endif()

add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/benchmark-src
        ${CMAKE_CURRENT_BINARY_DIR}/benchmark-build
        EXCLUDE_FROM_ALL)

include_directories("${gtest_SOURCE_DIR}/include" "${gmock_SOURCE_DIR}/include")
include_directories("${benchmark_SOURCE_DIR}/include")

......
# 在需要编译的目标文件里链接gtest_main, benchmark benchmark_main, pthread
target_link_libraries(allTest gtest_main benchmark benchmark_main pthread)

  • 没有什么其他要做的了,就是这么简单。。。
  • 感觉里面有些内容重复,当编译google/benchmark的时候,好像自动会编译依赖的gtest库,找个时间优化一下吧。。。

一些简单的CMakeList.txt知识

  • add_subdirectory:添加一个子目录,并构建该目录,所以该目录下必须有个CMakeLists.txt文件
  • include_directories: 将指定目录添加到编译器的头文件搜索路径之下,所以如果有找不到符号的报错,那么可以查看一下是否引人了相应的头文件
  • target_link_libraries: 目标文件与库文件进行链接
  • ExternalProject_Add:
  • configure_file: 将一个文件(由input参数指定)拷贝到指定位置(由output参数指定),并根据options修改其内容
 类似资料: