Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: August 12, 2011, 08:38:41 am » |
Hi, Although I'm pretty experienced with programming AVR's, I have a very basic question the arduino IDE. I'm trying to include a header file, but no matter what I do, I keep getting the following error: "undefined reference to `test()'" Here's my stripped code: #include <WProgram.h> #include "test.h"
void setup(){ test(); }
void loop(){ while(1); }
#ifndef TEST_H #define TEST_H void test(void); #endif #include "test.h" #include <stdint.h>
void test(void){ static uint8_t test; test=2; } Has anyone an idea what I'm doing wrong??
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #1 on: August 12, 2011, 08:54:07 am » |
Well I already found a (sort of) solution. When I rename test.c to test.cpp, it works.
However, I'm not using C++ and since I want my libraries to be usable in avrstudio too, I just want to use plain c files. Does anyone know how I can arrange that in the arduino IDE?
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #3 on: August 12, 2011, 09:26:35 am » |
C++ allows for function overloading, where one function, like Serial.print(), can print characters, arrays of characters, ints, array of ints, etc.
To support this, the C++ compiler performs a process known as name mangling, where the function name is actually constructed from the name of the function and the types of all the arguments.
C does not support function overloading, so it does not support name mangling.
The C++ compiler is invoked when the main program is C++, which is what the sketch is converted to.
You can tell the C++ compiler not to perform name mangling for a function, by using extern "C" in front of each function name in the header file:
extern "C" void test();
If there are multiple functions you want to define, you can do this:
extern "C" { int foo(); double bar(int n); void test(); }
If you do this, the C compiler will have a tizzy fit, so:
#ifdef __cpluscplus extern "C" { #endif int foo(); double bar(int n); void test(); #ifdef __cpluscplus } #endif
The __cplusplus term is defined when the C++ compiler is invoked, but not when the C compiler is.
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2476
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #4 on: August 12, 2011, 09:41:19 am » |
One thing I haven't been able to figure out yet, what is the difference between
#include <foo.h>
and
#include "foo.h"
?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: August 12, 2011, 09:53:27 am » |
The difference between <> and "" is where the compiler looks for header files. The "" means local (in the sketch folder). The <> is for library files (not in the sketch directory). Some compilers are not rigorous in enforcing this distinction.
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2476
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #6 on: August 12, 2011, 10:38:58 am » |
@PaulS, thanks, had been through the avr-libc manual, etc., and couldn't find an explanation. Maybe I need to re-read K&R.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #7 on: August 12, 2011, 10:47:27 am » |
@Pauls, thanks for your clear explanation. That makes it work.
Just one remark to make this topic useful for other readers: you made a typo in your example. It should be #ifdef __cplusplus instead of __cpluscplus.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #8 on: August 12, 2011, 10:56:59 am » |
It should be #ifdef __cplusplus instead of __cpluscplus. I knew that. My fingers were operating on their own this morning. Yeah, that's my story, and I'm sticking to it.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #9 on: August 12, 2011, 04:10:12 pm » |
Now I've got another problem: When I try to include SD.h in my library, the compiler says "No such file or directory". But when I include it in the main-file, i get no complaints. Does anybody have an idea how that comes?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #10 on: August 13, 2011, 06:42:17 pm » |
*kick*
Does no one know why I can't include the arduino libraries in other files than the main sketch file?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10130
|
 |
« Reply #11 on: August 13, 2011, 06:48:20 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
|