- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何使用 CMake 在 Android 上为我的 native C++ 代码设置 GoogleTest? Android NDK 与 googletest 捆绑在一起,但说明仅适用于 Android.mk (here)。如何将 Android.mk gtest 设置移植到我的 CMakeLists.txt?一旦设置完成,是否可以通过 Android Studio 的测试配置运行测试运行器?
最佳答案
我只能回答您的第一个主要问题,但这是一个可行的解决方案。它不是特定于 Android 的;只要您可以运行 CMake 和 CTest,它就可以工作。
我对 GoogleTest 了解不多,但我记得几个月前有一个非常相似的问题。就我而言,我想将 CMake 与 Boost UnitTestFramework 一起使用。
我搜索了一下,然后发现 a certain tutorial .他们提供的解决方案只是使用正则表达式解析您的测试文件的内容。好处是,它非常可定制,并且允许每个文件进行多个单元测试。我这样做了,而且效果很好。当然,当您添加新的单元测试时,您必须重新运行 CMake。
就像我说的,我将在下面发布针对 Boost UnitTestFramework 的内容,但最大的区别在于正则表达式和我使用的变量名。如果您不习惯在 CMake 中使用正则表达式(我不习惯),here是关于字符串操作的官方文档页面。
这是我最终得到的 CMakeLists.txt
文件。我将其用作 test/
目录下的独立 CMakeLists.txt
文件。
# =============================
# = ADDING TESTS =
# =============================
include(CTest)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
file(GLOB_RECURSE TEST_SRCS RELATIVE ${TEST_SOURCE_DIR} *.cpp)
set(TEST_EXTRA_LIBS ${Boost_LIBRARIES} ${TEST_MAIN_LIB})
# Function which, given a file name and a test name, yields the
# name of the GoogleTest test case.
# That way, several different files can have the same test name.
# Adapt this to GoogleTest.
function(getTestCase testFileName testName outTestCase)
string(REGEX MATCH "Test([_a-zA-Z][_a-zA-Z0-9]*)" match ${testFileName})
string(REGEX REPLACE ".*Test([_a-zA-Z][_a-zA-Z0-9]*).*" "\\1" testCase ${match})
set(testCase Test${testCase}${testName})
set(${outTestCase} ${testCase} PARENT_SCOPE)
endfunction()
# Function which adds all tests within a certain test file.
function(add_all_tests_in testSrc)
get_filename_component(testFileName ${testSrc} NAME_WE)
add_executable(${testFileName} ${testSrc})
target_link_libraries(${testFileName} ${TEST_EXTRA_LIBS})
set_target_properties(${testFileName} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
file(READ "${testSrc}" testFileContents)
# You should change this Regex for what you need in your case.
# Maybe something like:
# "TEST\\( *([_a-zA-Z][_a-zA-Z0-9]* *, *[_a-zA-Z][_a-zA-Z0-9]*) *\\)"
string(REGEX MATCHALL "DEF_TEST_CASE\\( *([_a-zA-Z][_a-zA-Z0-9]*) *\\)" unitTests ${testFileContents})
foreach(match ${unitTests})
# This replace will also probably need some change.
string(REGEX REPLACE ".*\\( *([_a-zA-Z][_a-zA-Z0-9]*) *\\).*" "\\1" testName ${match})
getTestCase(${testFileName} ${testName} testCase)
# Actually add the test.
# I wanted my CTest test names to be in the form
# <fileName>.<testName>, but you can use any
# format you want. Suit yourself.
#
# Also, in order for CMake to run the tests one by one,
# you have to find how to invoke the test executable.
# In the case of Boost, the option --run_test=<Boost_Test_Name>
# runs only the test called <Boost_Test_Name>. There should be
# an equivalent option for GoogleTest, I'm sure.
add_test(NAME "${testFileName}.${testName}" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${testFileName} --run_test=${testCase} --catch_system_error=yes)
endforeach()
endfunction()
# Actually add all tests in all test files.
foreach(testSrc ${TEST_SRCS})
add_all_tests_in(${testSrc})
endforeach()
为我工作。在 build/
目录下运行 CMake 后,运行所有测试会产生:
$ cd test && ctest
Test project /home/anthonyd973/Git/Git_Projects/MySweeper/build/test
Start 1: TestFieldMaker.makeFromFile
1/12 Test #1: TestFieldMaker.makeFromFile ........ Passed 0.03 sec
Start 2: TestFieldMaker.make
2/12 Test #2: TestFieldMaker.make ................ Passed 0.01 sec
Start 3: TestFieldMaker._computeFieldDims
3/12 Test #3: TestFieldMaker._computeFieldDims ... Passed 0.01 sec
Start 4: TestFieldMaker._populateField
4/12 Test #4: TestFieldMaker._populateField ...... Passed 0.00 sec
Start 5: TestInputField.InputField
5/12 Test #5: TestInputField.InputField .......... Passed 0.00 sec
Start 6: TestCell.Cell
6/12 Test #6: TestCell.Cell ...................... Passed 0.00 sec
Start 7: TestCell.initNeighbours
7/12 Test #7: TestCell.initNeighbours ............ Passed 0.00 sec
Start 8: TestCell.updateNeighbours
8/12 Test #8: TestCell.updateNeighbours .......... Passed 0.00 sec
Start 9: TestCell._mark
9/12 Test #9: TestCell._mark ..................... Passed 0.00 sec
Start 10: TestMySweeper.MySweeper
10/12 Test #10: TestMySweeper.MySweeper ............ Passed 0.00 sec
Start 11: TestField.Field
11/12 Test #11: TestField.Field .................... Passed 0.01 sec
Start 12: TestField._initNeighbours
12/12 Test #12: TestField._initNeighbours .......... Passed 0.00 sec
100% tests passed, 0 tests failed out of 12
Total Test time (real) = 0.10 sec
希望使用正则表达式不会导致您two problems :) .
关于c++ - GoogleTest for Android NDK C++ with CMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43503192/
运行 pip install cmake 时出现上述错误在 alpine linux (WSL) 上。 完整错误: /home/user# pip install cmake Collecting c
我的项目依赖 mariadb-connector-c我正在尝试使用 cmake 自动化下载、构建和链接过程。 我目前将项目下载到一个目录中,然后尝试执行生成忍者文件并运行它们,但我根本无法运行 cma
我正在尝试像这样在 cmake 中执行 execute_process execute_process(COMMAND ${CMAKE_COMMAND} -P myScript.cmake 这仅在文件
我想知道如果我没有给它任何变量告诉它在哪里找到它,cmake 如何找到我的 llvm cmake 配置。 我是 LLVM 新手。我正在构建一个 Hello World LLVM pass。我在 Ubu
我正在开发一个使用 CMake 的项目。顶CMakeLists.txt文件包含以下行: cmake_minimum_required(VERSION 3.7.2) # Kittens will die
我正在使用 Meego 1.2 工具链使用 cmake 运行构建(长话短说)。这个特定的工具链需要使用 --sysroot 选项来查找它需要的任何库。 set (CMAKE_CXX_FLAGS "-f
我们有一些源代码处理工具,可以从多个“片段”生成原始汇编文件等内容。 当使用 make 中的这些工具时,我们可以通过让源处理工具发出“依赖文件”来确保最新的构建,就像 gcc 一样-MD 标志。 例如
如何在 MSYS2 中安装和配置“cmake”?我尝试安装以下 MSYS 包 我在运行任何 cmake 命令时都收到以下错误 '''CMakeLists.txt:5 (cmake_minimum_re
有没有办法在 CMake 中得到一个变量列表?具体来说,我想要做的是调用一个现有函数,该函数接受多个变量并检查它们的计算结果是否为 true。 在某些情况下,其中一些变量将是空列表(计算结果为 fal
我有两组测试(功能测试和单元测试),我希望能够指定通过 cmake 运行哪一组。 一组测试是我想通过“make test”运行的单元测试。 另一组测试是我想通过“制作功能测试”来运行的功能测试。 目前
我的目标是查看有关调用 g++ 的详细信息由 cmake 直接调用从命令行。我不关心 make 的输出就这个问题而言。 根据official FAQ和 the accepted answer on a
我有一个生成库的CMake项目。我已经编写了一个CMake脚本FindXXX.cmake来帮助我的图书馆的用户。这样,他们可以使用find_package(XXX)并获取所需的变量。到现在为止还挺好。
当我看到带有命名空间的 CMake 库时,它们总是在表单中Parent::Component . 如果我有一个足够大的库,那么该库的子部分可能包含组件。我想知道是否可以/适合做 ParentProje
CMake 变量和属性似乎完成了非常相似的事情,我一直无法理解它们之间的区别。 它们每个都有自己的文档部分,但两者都会影响构建系统,两者都是“预先存在的”,并且两者都可以基于其他 CMake 命令动态
我有一个包含多个子目录的 CMake 项目,例如: dir1 subdir11 subdir12 dir2 subdir21 subdir22 根 CMakeLists
如果您想将静态库链接到共享库或可执行文件,同时保持所有符号可见(例如,您可以稍后打开它以找到它们),在 Linux/BSD 上执行此操作的一种非可移植方式是使用标志-Wl,--whole-archiv
CMake 自动创建一个 FooTarget.cmake文件例如添加 install(EXPORT FooTargets FILE FooTargets.cmake NAMESPACE Foo
在CMake中,ELSE和ENDIF控制流函数将表达式作为参数。根据文档,这些是optional。那么这些目的是什么?仅仅是为了维护目的而使原始IF表达式更清晰,还是提供某些功能? 最佳答案 正如您所
我是 cmake 的新手,我发现它非常令人沮丧。我试图在构建运行时评估的文件路径中使用通配符,而不是在生成构建时。 我创建了一个构建,它使用 SWIG 为某些 C++ 代码生成 Java 包装器。我可
我可能在谷歌上搜索错误,但我无法找到 .cmake 文件的用途。 我刚刚偶然发现了一个我必须使用的项目的 CMake 工具,我很难理解它是如何工作的。我明白在包含 CMakeLists.txt 的目录
我是一名优秀的程序员,十分优秀!