I encountered a strange behavior of arduino ide.
I use 2.1.0
The following lines inside a comment cause a compilation error if they contain 'extern "C" {' even in a program that does nothing.
// extern "C" { <== error
// even with any preciding char
// y_extern "C" { <== error
/*
extern "C" { <== error
*/
#if false
// extern "C" { <== error #endif
// extern "C" {
// extern "C" { <== error
but any char after the extern is OK
// extern_y "C" { <== OK
Sometimes one line works (rare) but two lines always fail
Here is the 6 lines code from arduino ide:
and here is the full error output:
D:\Projects\Beetle\ESP\Tries\Sketch\extern_C_Comment_err\extern_C_Comment_err.ino:3:18: error: conflicting declaration of 'void setup()' with 'C' linkage
void setup() {
^
In file included from C:\Users\Moish\AppData\Local\Temp\arduino\sketches\6A4C7BE233AF54B43EF5ECD6BE2EC987\sketch\extern_C_Comment_err.ino.cpp:1:
C:\Users\Moish\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.9\cores\esp32/Arduino.h:137:6: note: previous declaration with 'C++' linkage
void setup(void);
^~~~~
D:\Projects\Beetle\ESP\Tries\Sketch\extern_C_Comment_err\extern_C_Comment_err.ino:5:18: error: conflicting declaration of 'void loop()' with 'C' linkage
void loop() {
^
In file included from C:\Users\Moish\AppData\Local\Temp\arduino\sketches\6A4C7BE233AF54B43EF5ECD6BE2EC987\sketch\extern_C_Comment_err.ino.cpp:1:
C:\Users\Moish\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.9\cores\esp32/Arduino.h:138:6: note: previous declaration with 'C++' linkage
void loop(void);
^~~~
exit status 1
Compilation error: conflicting declaration of 'void setup()' with 'C' linkage
Note the spurious extern "C" prefixes on the prototypes for the setup and loop functions.
Even though clearly not as intended, the code still compiles for some boards. This is likely the reason why some here were not able to reproduce the error. For example, I can compile it for the "Arduino UNO" and "Arduino NANO 33 IoT" boards, but compiling for the "ESP32 Dev Module" board fails with the error you shared.
Following on from what you wrote, I get the impression that the problem exists regarding ESP32 boards and depending on the position of the line in the code. I moved the line to the end and there was no error.
// the Board is 'ESP32 Dev Moudule'
// extern "C" { <== Here it gives an error
void setup() {
}
// extern "C" { <== Here it gives an error
void loop() {
}
// extern "C" { <== here after loop() there was no error
What bothers me is that the comment seems to have no effect.
It seems like the compiler looks for the string 'extern "C" {' and ignores the comment even if 'extern' is written like this 'xyzextern'.
I tried several ESP8266 boards and the compilation was error free. Does it seem like a bug of the IDE or of the plug-in of the ESP32 libraries?
Thank you all.
The bug that caused the spurious extern "C" prefix on the generated function prototypes is universal. The spurious prefix is added regardless of which board you are compiling for.
It is only that there is something different (but not wrong in any way) about the ESP32 boards platform compared to the other boards platforms I tested with that causes the spurious prefix to produce a compilation error. I didn't investigate what this difference is in the ESP32 boards platform.
It is a bug in the IDE. More specifically, it is a bug in the Arduino CLI tool that handles the sketch preprocessing for Arduino IDE, or maybe even in the ctags tool Arduino CLI uses to parse the sketch code during preprocessing.
I don't have any information about the scheduling of a fix by the project managers. A pull request from the community fixing the bug would be welcome. The developers certainly acknowledge it is a bug that should be fixed, but there is a lot of work to do and few people to do it so it can sometimes take a while before resources can be allocated to investigation and resolution of a bug.
If you are using Microsoft's "Visual Studio Code extension for Arduino" then it will because that extension uses Arduino IDE or Arduino CLI to compile the sketch.
If you are using the PlatformIO extension, I'm not sure because I don't know if or how they implemented function prototype generation for Arduino sketches.
If you are just compiling C++ code directly without using an Arduino framework, then it will not occur because in that case there is no function prototype generation at all.