Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call weak function from Arduino C-Library to ESP32 .ino sketch #7133

Closed
1 task done
Erriez opened this issue Aug 15, 2022 · 2 comments
Closed
1 task done

Cannot call weak function from Arduino C-Library to ESP32 .ino sketch #7133

Erriez opened this issue Aug 15, 2022 · 2 comments
Labels
Status: Awaiting triage Issue is waiting for triage Status: Solved Type: For reference Common questions & problems Type: Question Only question

Comments

@Erriez
Copy link

Erriez commented Aug 15, 2022

Board

Any ESP32

Device Description

Any ESP32

Hardware Configuration

None.

Version

v2.0.4

IDE Name

Arduino 1.8.19

Operating System

Ubuntu 22.04

Flash frequency

Not relevant

PSRAM enabled

no

Upload speed

Not relevant

Description

An Arduino C-Library calls a weak function which should be defined in the Arduino .ino sketch. Building works for AVR and ESP8266 targets, but generates build errors when building for ESP32.

Sketch

examples/ErriezTest/ErriezTest.ino

#include <Arduino.h>
#include <ErriezTest.h>


#ifdef __cplusplus
extern "C" {
#endif

// Function is called from C-library
void myCallback()
{
    digitalWrite(LED_BUILTIN, HIGH);
}

#ifdef __cplusplus
}
#endif


void setup()
{
    // Initialize built-in LED
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
}

src/ErriezTest.h

#ifndef ERRIEZ_TEST_H_
#define ERRIEZ_TEST_H_

#ifdef __cplusplus
extern "C" {
#endif

void foo(void);

#ifdef __cplusplus
}
#endif

#endif // ERRIEZ_TEST_H_

src/ErriezTest.c

#include <Arduino.h>
#include "ErriezTest.h"
 
/* Function prototypes */
void myCallback(void) __attribute__((weak));
extern void myCallback(void);


void foo(void)
{
    if (myCallback != NULL) {
        myCallback();
    }
}

Debug Message

ErriezTest:44:6: error: conflicting declaration of 'void setup()' with 'C' linkage
 void setup()
      ^~~~~
In file included from /home/user/Arduino/libraries/ErriezTest/examples/ErriezTest/ErriezTest.ino:25:
/home/user/.arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/Arduino.h:136:6: note: previous declaration with 'C++' linkage
 void setup(void);
      ^~~~~
ErriezTest:51:6: error: conflicting declaration of 'void loop()' with 'C' linkage
 void loop()
      ^~~~
In file included from /home/user/Arduino/libraries/ErriezTest/examples/ErriezTest/ErriezTest.ino:25:
/home/user/.arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/Arduino.h:137:6: note: previous declaration with 'C++' linkage
 void loop(void);
      ^~~~
Using library ErriezTest at version 1.0.0 in folder: /home/user/Arduino/libraries/ErriezTest 
exit status 1
conflicting declaration of 'void setup()' with 'C' linkage

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@SuGlider
Copy link
Collaborator

@Erriez - That's interesting.

I tested your example and it causes the error you have posted.
But if I change the order of declaration in the INO file, it works fine:

#include <Arduino.h>
#include "ErriezTest.h"

void setup()
{
    // Initialize built-in LED
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
}


#ifdef __cplusplus
extern "C" {
#endif

// Function is called from C-library
void myCallback()
{
    digitalWrite(LED_BUILTIN, HIGH);
}

#ifdef __cplusplus
}
#endif

@SuGlider SuGlider added Type: For reference Common questions & problems Type: Question Only question labels Aug 22, 2022
@Erriez
Copy link
Author

Erriez commented Aug 22, 2022

Thanks for your feedback. I did not expect this behavior as it works for AVR and ESP8266 (and maybe more). In the mean time I've found a workaround by adding a callback function, so I'm not blocked on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting triage Issue is waiting for triage Status: Solved Type: For reference Common questions & problems Type: Question Only question
Projects
None yet
Development

No branches or pull requests

3 participants