我正在尝试构建 google-benchmark 并使用 cmake 将其与我的库一起使用。我已经成功构建了 google-benchmark 并使用 cmake 成功运行了所有测试。不幸的是,我无法使用 cmake 或 cl 将其与 Windows 中的 C++ 代码正确链接。
我认为的问题是 google-benchmark 在 src 文件夹内构建库,即它是在 src/Release/benchmark.lib 中构建的,现在如果我使用 ${benchmark_LIBRARIES} ,我无法在 cmake 中指向它,它会在中查找库src 外部的 Release 文件夹,因为这是构建所有库的通常位置。并且很难找到在 Windows 中运行的示例。
这是我尝试过的两种方法,都可以构建库并运行所有测试,但我无法正确地将库指向 target_link_library
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
)
之后我如何将它链接到我的 C++ 文件 try.cpp
CMakeLists.txt
如下:
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)
需要cmake版本3.14以上
cmake .
cmake --build .
参考:https://github.com/hohaidang/CPP_Basic2Advance/tree/master/Learning/CMake/g_benchmark https://github.com/hohaidang/CPP_Basic2Advance/tree/master/Learning/CMake/g_benchmark
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)