Skip to content

Generated prototype incorrectly prefixed with extern "C" when using extern "C" { ... } to mix C functions in an .ino file. #1618

Open
@dmalenic

Description

@dmalenic

Describe the problem

Compiling the following blink sketch for ESP32 board:

#ifdef __cplusplus
extern "C"
{
#endif

void dummy(void) {}

#ifdef __cplusplus
}
#endif

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

results in errors:

sketch_esp32_blink:12:12: error: conflicting declaration of 'void setup()' with 'C' linkage
 void setup()
            ^
In file included from /tmp/arduino_build_620310/sketch/sketch_esp32_blink.ino.cpp:1:0:
.arduino15/packages/esp32/hardware/esp32/1.0.6/cores/esp32/Arduino.h:118:6: note: previous declaration with 'C++' linkage
 void setup(void);
      ^
sketch_esp32_blink:17:11: error: conflicting declaration of 'void loop()' with 'C' linkage
 void loop()
           ^
In file included from /tmp/arduino_build_620310/sketch/sketch_esp32_blink.ino.cpp:1:0:
.arduino15/packages/esp32/hardware/esp32/1.0.6/cores/esp32/Arduino.h:119:6: note: previous declaration with 'C++' linkage
 void loop(void);
      ^
exit status 1
conflicting declaration of 'void setup()' with 'C' linkage

Arduino CLI version

Original report

Arduino IDE 1.8.19

Last verified with

60c1c98

Operating system

  • Linux
  • Windows

Operating system version

  • Ubuntu 21.10
  • Windows 11

Additional context

If the extern "C" { ... } block does not define any function but only variables, or if the whole extern "C" { ... } block is moved after the definition of setup and loop functions, the code compiles successfully.

If the extern "C" { ... } block only declares functions, and functions are defined in a separate module (file), everything compiles correctly.

The files with .cpp extensions do not have this problem, and allow mixing C linkage functions defined within extern "C" { ... } blocks.

Looks like, when the compiler encounters a C linkage function definition in a sketch file (.ino extension), it assumes that the rest of file defines only C linkage functions.

The error can not be reproduced when compiling for boards using AVR or ARM processors that I could test with.

Related

Issue checklist

  • I searched for previous reports in the issue tracker
  • I verified the problem still occurs when using the nightly build
  • My report contains all necessary details

Metadata

Metadata

Assignees

Labels

topic: build-processRelated to the sketch build processtopic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions