Go Down

Topic: Where's my error? (Read 12120 times) previous topic - next topic

bugmenot

see here for my code: http://ace.pastey.net/9623

With arduino-007, I get this error:

error: 'MorseFlasher' has not been declared In function 'void morse_char(char, void (*)(long unsigned int))':
In function 'void morse_dash(void (*)(long unsigned int))':
In function 'void morse_dot(void (*)(long unsigned int))':
In function 'void morse_string(const char*, void (*)(long unsigned int))':

I'm not sure where I'm going wrong, when I compile this code by hand with avr-gcc, I don't get this error. What is wrong with how I am using function pointers?

I wish arduino gave line numbers for the errors.

mellis

The problem is related to the fact that Arduino automatically generates prototypes for functions in your sketch.  These prototypes are inserted above your code.  Thus, MorseFlasher is not declared before those prototypes and you get an error.  Typedefs are a feature of C/C++ that we thought might be too confusing for our target audience, so we haven't yet worried about supporting them.  For now, you can try rewriting your code without using the typedef, or compiling it outside of Arduino (e.g. with the Arduino command line instructions at: http://www.arduino.cc/en/Hacking/CommandLine).

bugmenot

Does Arduino generate prototypes for all files in the sketch or just in the main sketch file? As in, if I split off my morse code stuff into an .h and .c file, would that work?

mellis

It should, yea, good point.  I believe it only generates prototypes for functions in the "main sketch file" - any of the tabs with no extension (that all get concatenated into a single file).

bugmenot

Okay, I've moved the typedef and morse_* declarations to morse.h, and the morse_* definitions to morse.c.

Now, a different error:

o: In function `loop':
undefined reference to `morse_string(char const*, void (*)(unsigned long))'

Now that's a linker error, isn't it? I read the page on Arduino's build process but if Arduino is doing anything special with linking, I don't see it.

mellis

Did you #include "morse.h" in your main sketch file?  Also, the main sketch is compiled as C++, so you either need to rename morse.c to morse.cpp or wrap the function declarations in morse.h with:


Code: [Select]
#ifdef __cplusplus
extern "C"{
#endif


Code: [Select]
#ifdef __cplusplus
} // extern "C"
#endif

bugmenot

Quote
Did you #include "morse.h" in your main sketch file?  Also, the main sketch is compiled as C++, so you either need to rename morse.c to morse.cpp or wrap the function declarations in morse.h with:


The extern "C" was what I needed, thanks. I found that if I renamed morse.c to morse.cpp, I needed to include WProgram.h

Thank you for your help!

Go Up