如何在Windows中使用cmake构建和链接Google基准 [英] How to build and link google benchmark using cmake in windows

查看:140
本文介绍了如何在Windows中使用cmake构建和链接Google基准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建Google基准,并使用cmake将其与我的库一起使用.我已经成功建立了google-benchmark并使用cmake成功运行了所有测试.不幸的是,我无法使用cmake或cl将其与Windows中的C ++代码正确链接.

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.

我认为的问题是google-benchmark会在src文件夹中构建库,即它是在src/Release/benchmark.lib中构建的,如果我使用$ {benchmark_LIBRARIES}来查找,我现在无法在cmake中指向它库位于src外部的Release文件夹中,因为这是构建所有库的常用位置.而且很难找到可以在Windows中使用的示例.

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.

这是我尝试过的两种方法,都可以构建库并且可以运行所有测试,但是我无法正确指向target_link_library库

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 
)

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

how do i link it to my c++ file try.cpp after this

推荐答案

我来这里是为了寻找复制粘贴解决方案,但是我看不到任何明确的解决方案,尽管看到很多人在看此页面,所以这就是我所做的.

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.

我没有使用过ExternalProject_Add,但是如果您向我指出了一个可以运行的完整运行测试示例,我将很乐意为您提供帮助.

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)

我还没有在Windows上尝试过它,但是下次我下次在家中赢得胜利时会做.我在几台Linux机器上都尝试过.

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.

希望对您有帮助.

这篇关于如何在Windows中使用cmake构建和链接Google基准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆