Skip to content

ESP32-S2: Linker error undefined reference to `__sync_sub_and_fetch_4' (ESP32 is working) #5948

Closed
@klausj1

Description

@klausj1

Hardware:

Board: ESP32-S2 (WROVER / ESP32-S2-Saola-1)
Core Installation version: 2.0.1
IDE name: Arduino IDE 1.8.16
Flash Frequency: 80Mhz
PSRAM enabled: No
Upload Speed: 921600
Computer OS: Windows 10

Description:

I am trying to connect my ESP32-S2 to the Azure IoT Hub using ESP32_AzureIoT - Azure IoT Hub library for esp32 devices in Arduino.

BTW: Is there a better way to connect the ESP32-S2 to Azure IoT than using this library?

When using the device ESP32SDev Module, compile and linking works.

When using the device ESP32S2 Dev Module, I get a linker error:

c:/users/klaus/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s2-elf-gcc/gcc8_4_0-esp-2021r1/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: c:\Users\Klaus\Documents\IoTWorkbenchProjects\examples\esp32_get_started\Device\.build\libraries\ESP32_AzureIoT_Arduino\az_iot\c-utility\src\constbuffer.c.o:(.literal.CONSTBUFFER_Destroy+0x0): undefined reference to `__sync_sub_and_fetch_4'
c:/users/klaus/appdata/local/arduino15/packages/esp32/tools/xtensa-esp32s2-elf-gcc/gcc8_4_0-esp-2021r1/bin/../lib/gcc/xtensa-esp32s2-elf/8.4.0/../../../../xtensa-esp32s2-elf/bin/ld.exe: c:\Users\Klaus\Documents\IoTWorkbenchProjects\examples\esp32_get_started\Device\.build\libraries\ESP32_AzureIoT_Arduino\az_iot\c-utility\src\constbuffer.c.o: in function `CONSTBUFFER_Destroy':
C:\Users\Klaus\Documents\Arduino\libraries\ESP32_AzureIoT_Arduino\src\az_iot\c-utility\src/constbuffer.c:143: undefined reference to `__sync_sub_and_fetch_4'
collect2.exe: error: ld returned 1 exit status

I tracked this down to this file, which defines this:

#define DEC_RETURN_ZERO (0)
#define INC_REF(type, var) __sync_add_and_fetch((&((REFCOUNT_TYPE(type)*)var)->count), 1)
#define DEC_REF(type, var) __sync_sub_and_fetch((&((REFCOUNT_TYPE(type)*)var)->count), 1)

If I change the contents of this file in a way, that DEC_REF is defined differently:

#define DEC_RETURN_ZERO (0)
#define INC_REF(type, var) ++((((REFCOUNT_TYPE(type)*)var)->count))
#define DEC_REF(type, var) --((((REFCOUNT_TYPE(type)*)var)->count))

Then compiling and linking works.

This is what I assume happens with ESP32 (not ESP32S2), but to be honest I do not really know what I am doing here.

Which definition is chosen depends on some defines like __STDC_VERSION__, __STDC_VERSION__, __STDC_NO_ATOMICS__, ARDUINO_ARCH_SAMD, FREERTOS_ARCH_ESP8266, but I could not figure out where these definitions are set and whats the difference between ESP32 and ESP32-S2.

(Still, the sketch is not working and delivers this runtime error:

Error: Time:Tue Nov 30 05:22:39 2021 File:C:\Users\Klaus\Documents\Arduino\libraries\ESP32_AzureIoT_Arduino\src\az_iot\c-utility\pal\socket_async.c Func:socket_async_create Line:134 Socket connect failed, not EINPROGRESS: 0
Info: Could not open the socket
Info: >>>Connection status: disconnected
Info: >>>Connection status: disconnected
Info: tlsio_openssl_close has been called when in neither TLSIO_STATE_OPEN nor TLSIO_STATE_ERROR.

But I would like to follow this up when I the linker error is clarifed.)

Sketch:

/**
 * A simple Azure IoT example for sending telemetry to Iot Hub.
 */

#include <WiFi.h>
#include "Esp32MQTTClient.h"

#define INTERVAL 10000
#define MESSAGE_MAX_LEN 256
// Please input the SSID and password of WiFi
const char* ssid     = "***";
const char* password = "***";

/*String containing Hostname, Device Id & Device Key in the format:                         */
/*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"                */
/*  "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>"    */
static const char* connectionString = "***";
static bool hasIoTHub = false;
static bool hasWifi = false;

void setup() {
  Serial.begin(115200);
  Serial.println("ESP32 Device");
  Serial.println("Initializing...");
  Serial.println(" > WiFi");
  Serial.println("Starting connecting WiFi.");

  delay(10);
  WiFi.mode(WIFI_AP);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    hasWifi = false;
  }
  hasWifi = true;
  
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(" > IoT Hub");
  if (!Esp32MQTTClient_Init((const uint8_t*)connectionString, true))
  {
    hasIoTHub = false;
    Serial.println("Initializing IoT hub failed.");
    return;
  }
}

void loop() {
  delay(10);
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions