允许与来自 Throw The Switch 的 Unity 单元测试框架进行双浮点比较

Jam*_*mes 3 c linker gcc compiler-errors clang

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

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

 *     - define UNITY_INCLUDE_DOUBLE to allow double floating point comparisons
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

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

#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();
}
Run Code Online (Sandbox Code Playgroud)

示例的 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)
Run Code Online (Sandbox Code Playgroud)

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

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

Tob*_*oby 5

我通过在 unity_config.h 文件中使用相同的定义并添加UNITY_INCLUDE_CONFIG_H为编译器-D标志 (gcc)来处理我的最小测试。但是#define在源代码中运行不会做同样的事情

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

如果您按照该文件中的注释(您在问题中引用)中指定的那样将定义直接添加到 unity.h 文件中,它也可以使用

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

即在 unity.h

#define UNITY_INCLUDE_DOUBLE
#include "unity_internals.h"
...
Run Code Online (Sandbox Code Playgroud)

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