Pages: [1]   Go Down
Author Topic: Can't get multi-tab sketches to compile  (Read 866 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/* Multi-tab sketch experiment.
* Teaching myself tabs in Arduino
*
*/

#include "doA.h"


void setup() {
  doB();
}

void loop() {
  doA();
}

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

---- doA.h
Code:
#ifndef DOA_SEEN
#define DOA_SEEN


void doA();

#endif

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

---- doA.c

Code:
#include "doA.h"

void doA() {

}
----------------------------------------

---- doB.pde
Code:
void doB() {

}
----------------------------------------

This is the error I get when compiling

Code:
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



Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49016
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

YES!

Renaming to .cpp did the trick!  smiley-grin

Thank you very much!


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49016
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Sweden
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino System Go!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This was my source: http://www.arduino.cc/en/Hacking/BuildProcess
Logged

Pages: [1]   Go Up
Jump to: