General problem with headerfiles

Hello,

I have used arduino in several projects and I am encountered with one certain problem again and again. I'm totally fed up with this an wrote a very short example, to show it to you. Please have a look:

#include "Arduino.h"
#include "lib.h"

void setup () {
Serial.begin(115200);
Serial.print("Returned: ");
Serial.println(f());
}

void loop () {
}

#ifndef lib_h
#define lib_h

int f(void);

#endif

#include "lib.h"

int f(void) {
return 1;
}

If I try to compile this code, I get the following error:

sketch_jan23a.cpp.o: In function setup': C:\Program Files (x86)\arduino-1.0.3/sketch_jan23a.ino:7: undefined reference to f()'

What is the problem? I read several articles and books concerning how to program header files. I can't find any mistake. And it happens everytime I try to split a program in several files. I have found a workaround: including the .c instead of .h

Thank you.

I have found a workaround: including the .c instead of .h

C++ can not use functions defined in a C file without special handling. You are not supplying that special handling.

Why are you not using the cpp extension, instead?

PaulS:
Why are you not using the cpp extension, instead?

Awesome, if I rename the lib.c to lib.cpp, it works. Could you please send me a link, where I can find more information to this problem and the "special handling"?

Thanks a lot.

Just google calling C functions from C++. You need to tell the C++ compiler not to perform name mangling on the C functions. The C compiler doesn't, so the linker can find the name-mangled function in the object module.

Okay, thank you! I learned programming with AVR Studio 4 and C and never had this issue. This problem with arduino always made me crazy!