How to call a function from another "C" file

Hello,
i have a sketch which is using some c files for a lvgl gui.
I would like to call a function which is located in the ino from one of these files.
But i always got the error message undefined reference to that function.
i tried a few things but none of them worked.

any idea ?

Post code?
Post error messages?
Use the "extern" keyword?

Error message
C:\Users...\events_init.c:234: undefined reference to `set_led_brightness'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

I have the test funtion set_led_brightness

in the c file is use: extern void set_led_brightness (uint8_t val);
and the function call. set_led_brightness (VAL);

the function is located in my ino file:
void set_led_brightness (uint8_t val)
{
Serial.print("new brightness val:");
Serial.print("val");
Serial.print("\n\r");
}

in this case, the problem is that a function in a .cpp/.ino file is being called from a .c file. rename it events_init.cpp

ok i understand, the problem is that this file is auto generated and a lot of other files have now a problem to work with it.

say the function in the .ino file that you want to be visible is call foo

int foo(int someParam) {
  // do some stuff here
}

then surround it with this

extern "C" { // <=== ADD THIS
  int foo(int someParam) {
    // do some stuff here
  }
} // <=== AND CLOSE IT AFTER THE FUNCTION

then at the top of your c file do

extern int foo(int);

that should do the trick

an example in wokwi

not working. i did exacly the same like on the simulator
error: conflicting declaration of 'void setup()' with 'C' linkage
void setup()

Did you put setup() as C ?

no its a standard ino file

Why make us play guessing games? Post your complete code. Use Code Tags!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.