Pages: [1]   Go Down
Author Topic: Function defined in header not available in main file  (Read 285 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a file structure as follows:
Code:
interface.h --> interface.c
      |
      |
effects.h --> effects.c
      |
      |
    main

However, functions declared in effects.h are not accessible in main.

Code snippets :

main :

Code:
#include "interface.h"
#include "effects.h"
void setup()  //Initialize all variables here
{

....
turnoff();
};

effects.h :

Code:
#ifndef EFFECTS
#define EFFECTS
void turnoff();
#endif

effects.c :

Code:
#include "interface.h"
#include "effects.h"
void turnoff()
{
....
};

interface.h :

Code:
#ifndef INTERFACE
#define INTERFACE
....
#endif

Error message : In function ``loop':undefined reference to ``turnoff()'

Anybody knows whats wrong ?
Logged

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

Quote
Anybody knows whats wrong ?
I c what the problem is. Changing to cpp would help.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why ? Doesnt avr-gcc handle c ??

http://arduino.cc/en/Hacking/BuildProcess
« Last Edit: February 10, 2013, 10:28:52 am by ashrj » Logged

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

Yes, it does. However, C++ supports function overloading. That is the same function can be called with different arguments. How do you suppose the compiler knows which code to actually execute when you type Serial.print(val);?

The answer is that every function really gets a different name. That name is made up of the function name and the types of the arguments that the function accepts.

So, Serial.print() for an int is a different name what Serial.print() for a float.

The C++ compiler is looking for a function based on the name and the arguments that the function accepts.

The C compiler, because it does not support function overloading, knows that the function name is unique, so it doesn't mangle the name.

The linker, then, can't find the mangled name it is looking for in the object file produced by the C compiler.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed the files to .cpp and it worked. What I still dont understand is why ? If my project is completely in C or C++ then the linker should be able to handle it. What you describe should cause problems when i mix C and C++ together, if I understood you correctly.
Logged

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

Quote
What I still dont understand is why ?
I described why. You could google "C++ name mangling" for more details.

Quote
If my project is completely in C or C++ then the linker should be able to handle it.
And it can. The IDE creates a cpp file from the sketch, though.

Quote
What you describe should cause problems when i mix C and C++ together, if I understood you correctly.
Well? Didn't it?
Logged

Pages: [1]   Go Up
Jump to: