platformIO中的本机单元测试:ld:找不到架构x86_64的符号

问题描述

我无法解决以下问题,需要一些帮助。我正在尝试为arduino(c ++)项目编写单元测试。运行测试时,出现以下错误

g++ -o .pio/build/native/test/test_internalTimefunctions.o -c -dplATFORMIO=50002 -DUNIT_TEST -DUNITY_INCLUDE_CONfig_H -Iinclude -Isrc -I.pio/build/native/UnityTestLib -I/Users/dakkar/.platformio/packages/tool-unity test/test_internalTimefunctions.cpp
g++ -o .pio/build/native/program .pio/build/native/test/test_internalTimefunctions.o .pio/build/native/test/tmp_pio_test_transport.o -L.pio/build/native .pio/build/native/libUnityTestLib.a
Undefined symbols for architecture x86_64:
  "_InternalTimefunctions",referenced from:
      test_isSleeptime() in test_internalTimefunctions.o
  "InternalTimefunctionsClass::isInSleeptime(unsigned char,unsigned char,unsigned char)",referenced from:
      test_isSleeptime() in test_internalTimefunctions.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)
*** [.pio/build/native/program] Error 1

复制的最小来源: src / internal / internalTimefunctions.h

#include <cstdint>

class InternalTimefunctionsClass
{
    public:
        InternalTimefunctionsClass();
        ~InternalTimefunctionsClass();
        bool isInSleeptime(uint8_t,uint8_t,uint8_t);
};

extern InternalTimefunctionsClass InternalTimefunctions;

src / internal / internalTimefunctions.cpp

#include "internalTimefunctions.h"

InternalTimefunctionsClass InternalTimefunctions = InternalTimefunctionsClass();

bool InternalTimefunctionsClass::isInSleeptime(uint8_t hours,uint8_t minutes,uint8_t startHour,uint8_t startMinute,uint8_t endHour,uint8_t endMinute)
{
    return false;
}

test / test_internalTimefunctions.cpp

#ifdef UNIT_TEST

#include <unity.h>
#include <cstdint>
#include "internal/internalTimefunctions.h"

void test_isSleeptime()
{
    uint8_t hours = 14;
    uint8_t minutes = 0;
    uint8_t startHour = 22;
    uint8_t startMinute = 0;
    uint8_t endHour = 6;
    uint8_t endMinute = 0;

    TEST_ASSERT_FALSE(InternalTimefunctions.isInSleeptime(hours,minutes,startHour,startMinute,endHour,endMinute ));
}


int main( int argc,char **argv) {
    UNITY_BEGIN();

    RUN_TEST(test_isSleeptime);

    UNITY_END();
}

#endif

platformio.ini

platformio]

[env:native]
platform = native

要运行的命令:pio test -e native

错误消息中有什么可疑的内容

  "InternalTimefunctionsClass::isInSleeptime(unsigned char,referenced from:

为什么要放弃char?它应该是unsigned int吗?也许就是这个原因,为什么找不到isInSleeptime(==未解析的符号?)

编辑:在macOS下构建

❯ g++ --version
Configured with: --prefix=/Library/Developer/CommandLinetools/usr --with-gxx-include-dir=/Library/Developer/CommandLinetools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.29)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLinetools/usr/bin

解决方法

为什么要使用字符?它应该是 unsigned int 吗?

一个 8 位无符号 int 等价于 unsigned char。 不过,这不是导致您出现问题的原因。

尝试按照 PlatformIO lib_dir 文档组织您的库。

例如,您可以这样做:

|--lib
|  |--internal
|  |  |--docs
|  |  |--examples
|  |  |--src
|  |     |- internalTimefunctions.cpp
|  |     |- internalTimefunctions.h
|- platformio.ini
|--src
|  |- main.c
|--test
   |--test_internal_time_functions
      |- test_internalTimefunctions.cpp

需要特别注意的事项:

  • 每个单元测试文件都需要自己的文件夹
  • 将共享代码放在 /lib/{library_name}/src 目录中。我尝试将代码放入 /lib/{library_name} 目录,但没有成功,所以您可能遇到了类似的问题。这对我有用。