【问题标题】:Allow double floating point comparisons with Unity unit testing framework from Throw The Switch允许与 Throw The Switch 中的 Unity 单元测试框架进行双浮点比较
【发布时间】:2016-06-13 12:11:27
【问题描述】:

在尝试编译使用 Throw The Switch 的 Unity 测试框架编写的测试时,我在链接器的某个地方遇到了一些问题。我还有其他测试可以编译和运行得非常好,所以我肯定只是在启用双浮点比较的断言助手时遗漏了一些东西。

头文件中有文档告诉我们如何启用双浮点比较。

 *     - define UNITY_INCLUDE_DOUBLE to allow double floating point comparisons

但是,我最终得到了这个错误:

Undefined symbols for architecture x86_64:
  "_UnityAssertDoublesWithin", referenced from:
      _test_example in main-6fae82.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我写了一个最简单的例子来复制这个:

#define UNITY_INCLUDE_DOUBLE

#include "unity.h"

void test_example(void)
{
    TEST_ASSERT_EQUAL_DOUBLE(1.234, 1.234);
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_example);
    return UNITY_END();
}

示例的 CWD 内容和调用 clang 的确切命令(再次显示相同的错误):

$ ls
main.c          unity.c         unity.h         unity_internals.h
$ clang unity.c main.c
Undefined symbols for architecture x86_64:
  "_UnityAssertDoublesWithin", referenced from:
      _test_example in main-ee77c2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

gcc 也发生了同样的事情(但实际上它只是在后台调用 clang)。

我很确定我只是错过了一小步,但对于我的一生来说,我现在看不到它是什么。提前感谢您的帮助。

【问题讨论】:

  • 作为数据点,我通过在 unity_config.h 文件中使用相同的定义并添加 UNITY_INCLUDE_CONFIG_H 作为编译器 -D 标志 (gcc) 来处理我的最小测试。但是使用#define 运行并不能达到同样的效果。

标签: c gcc compiler-errors linker clang


【解决方案1】:

我通过在 unity_config.h 文件中使用相同的定义并添加 UNITY_INCLUDE_CONFIG_H 作为编译器 -D 标志 (gcc) 来完成我的最小测试。但是在源代码中使用#define 运行并没有相同的技巧

您可以在ThrowTheSwitch GitHub repo 上找到unity_config.h 文件的完整副本。我通常只是把它放在 Unity 所在的同一个文件夹中。

如果您将定义直接添加到该文件中的 cmets 中指定的 unity.h 文件中(您在问题中引用),它也可以工作,其中

下面描述的所有选项都应作为编译器标志传递给使用 Unity 的所有文件。如果您必须添加#defines,请将它们放在上面的#include 之前。

即在unity.h

#define UNITY_INCLUDE_DOUBLE
#include "unity_internals.h"
...

我不知道为什么您最初的策略(本质上应该是相同的)不起作用。

【讨论】:

  • 太酷了。我也完全错过了示例目录中的 unity_config.h 文件,我喜欢它比我最终做的更好(在我的 Makefile 中指定)。感谢您的帮助!
【解决方案2】:

当使用 -D 标志将 UNITY_INCLUDE_DOUBLE 定义为 clang/gcc 时,看起来这很有效。例如:

$ clang -DUNITY_INCLUDE_DOUBLE -DUNITY_DOUBLE_PRECISION=1e-12f unity.c main.c
$ ./a.out 
main.c:10:test_example:PASS

-----------------------
1 Tests 0 Failures 0 Ignored 
OK

【讨论】:

    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多