Undefined reference to UnityAssertEqualString

I’m brand new on Platformio, and was trying to follow the Get started with Arduino and ESP32-DevKitC: debugging and unit testing tutorial everything was good until I tried the unit test section. When I ran the test this of many error messages comes up.

I tried to run tests with pio test command and same output again.

Full command output

test/test_main.cpp

#include "esp32-hal.h"
#include "unity_internals.h"
#include <Arduino.h>
#include <unity.h>

String STR_TO_TEST;

void setUp(void) { STR_TO_TEST = "Hello, world"; }

void tearDown(void) { STR_TO_TEST = ""; }

void test_string_concat(void) {
  String hello = "Hello, ";
  String world = "world!";
  TEST_ASSERT_EQUAL_STRING(STR_TO_TEST.c_str(), (hello + world).c_str());
}

void test_string_substring(void) {
  TEST_ASSERT_EQUAL_STRING("Hello", STR_TO_TEST.substring(0, 5).c_str());
}

void test_string_index_of(void){
  TEST_ASSERT_EQUAL(7, STR_TO_TEST.indexOf('w'));
}

void test_string_equal_ignore_case(void){
  TEST_ASSERT_TRUE(STR_TO_TEST.equalsIgnoreCase("HELLO, WORLD!"));
}

void test_string_to_upper_case(void) {
  STR_TO_TEST.toUpperCase();
  TEST_ASSERT_EQUAL_STRING("HELLO, WORLD!",  STR_TO_TEST.c_str());
}

void test_string_replace(void){
  STR_TO_TEST.replace('!', '?');
  TEST_ASSERT_EQUAL_STRING("Hello, world?", STR_TO_TEST.c_str());
}

void setup(){
  delay(2000);
  UNITY_BEGIN();
  RUN_TEST(test_string_concat);
  RUN_TEST(test_string_substring);
  RUN_TEST(test_string_index_of);
  RUN_TEST(test_string_equal_ignore_case);
  RUN_TEST(test_string_to_upper_case);
  RUN_TEST(test_string_replace);
  UNITY_END();
}

void loop(){
}

platformio.ini:

[env:dev]
############################
board = esp32dev
platform = espressif32 
############################

framework = arduino
test_framework = unity

Why is this line there?

Oh, that was the problem.
It got there because I miss type a function name and the LSP completion auto included.
Thank you for the help.

Same goes for #include "esp32-hal.h".

The technical reason why including unity_internals.h breaks linkage is because that files declares the Unity fundamental functions (like UnityAssertEqualString) but without the extern "C" linkage, so it will look for a C++ function called that way, which does not exist. You must always only use the unity.h include.

1 Like