Dakkar
Dakkar

Reputation: 5952

native unit tests in platformIO: ld: symbol(s) not found for architecture x86_64

I can't fix the following problem and need some help. I'm trying to write unit tests for an arduino (c++) project. When running the tests I receive the following error:

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, 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

The minimal source for reproducing: src/internal/internalTimefunctions.h

#include <cstdint>

class InternalTimefunctionsClass
{
    public:
        InternalTimefunctionsClass();
        ~InternalTimefunctionsClass();
        bool isInSleeptime(uint8_t, uint8_t, uint8_t, 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

the command to run: pio test -e native

Whats suspicious in the error message:

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

why usigned char? it should be unsigned int? maybe that's the reason, why isInSleeptime can't be found (==unresolved symbol?)

EDIT: building under 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

Upvotes: 2

Views: 512

Answers (1)

Matt Williams
Matt Williams

Reputation: 1714

why usigned char? it should be unsigned int?

An 8-bit unsigned int is equivalent to unsigned char. This isn't what is causing your problem though.

Try organising your library as per the PlatformIO lib_dir documentation.

For your example, you could do:

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

Things of particular interest to note:

  • each unit test file needs its own folder
  • place the shared code inside the /lib/{library_name}/src directory. I tried putting code into the /lib/{library_name} directory and that didn't work, so perhaps you are having a similar issue. This worked for me.

Upvotes: 1

Related Questions