Go Down

Topic: Can't get multi-tab sketches to compile (Read 970 times) previous topic - next topic

Anders 2009

Hi,
I have read the guides on how to structure a sketch over multiple tabs to keep it readable. The problem I have now is that only tabs without extension work (doB below). Having own .c and .h files doesn't work (doA below). I keep getting errors that whatever functions I have in an other .c and .h file in the sketch cannot be found in the scope. See screenshot and code below.




This is the code I use.

---- MultiTabSketchExperiment.pde
Code: [Select]

/* Multi-tab sketch experiment.
* Teaching myself tabs in Arduino
*
*/

#include "doA.h"


void setup() {
  doB();
}

void loop() {
  doA();
}


----------------------------------------

---- doA.h
Code: [Select]
#ifndef DOA_SEEN
#define DOA_SEEN


void doA();

#endif


----------------------------------------

---- doA.c

Code: [Select]
#include "doA.h"

void doA() {

}

----------------------------------------

---- doB.pde
Code: [Select]
void doB() {

}

----------------------------------------

This is the error I get when compiling

Code: [Select]
MultiTabSketchExperiment.cpp.o: In function `loop':
MultiTabSketchExperiment.cpp:19: undefined reference to `doA()'



In conclusion:


  • doB() works

  • doA() can't be found

  • Why?



I'm sure I'm doing something really stupid, but would really appreciate any pointers in the right direction. I am used to programming Java and my memories of C from school are very dim.

Best regards/Anders




PaulS

What happens if you rename doA.c to doA.cpp?

Anders 2009

YES!

Renaming to .cpp did the trick!  :D

Thank you very much!



PaulS

The reason that re-naming the file worked is that C++ uses name mangling to construct the actual function name called. This is necessary since there can be multiple functions of the same name that take different arguments (Serial.print() comes to mind - there are several versions of print, for int, byte, char, etc.). To uniquely define each function, as a separate entry in the function table, name mangling is performed. So, doA() actually gets a different name, based on the input argument types, in the function table.

C doesn't perform name mangling, since multiple functions with the same name are not allowed. So, the C compiler added doA() to the function table. Since the C function doA() in the function table does not match the C++ doA() function table entry, the linker couldn't resolve the name.

There are ways to modify the header file to tell the C++ compiler not to perform name mangling on functions that will be defined in C files, but renaming the .c file to .cpp is easier.

Anders 2009

Wow!, That's quite a snag.

The tutorial should probably be changed to just recommend you to use .cpp as extension instead of .c. This way beginners will have an easier ride.

Thanks again!/Anders


Go Up