Header file wquestion

I am getting the following error after I attempt to break out a function definition from a header file I wrote:

o: In function `readBytes()':
undefined reference to `add_byte(byte_array*, unsigned char)'

The header file (byte_array_s.h) looks like this:

#ifndef _byte_array_h
#define _byte_array_h

#include <WProgram.h>

typedef struct byte_array {
  byte *data;    // array of bytes this struct is meant to represent
  int size;      // number of bytes the structure possesses
  int bitsRead;  // number of bits contained in the structure (this is different than 8 * size)
} byte_array_s;

void add_byte(byte_array_s *byteArray, const byte _byte);

#endif

And this is the code that is throwing the error (currentByte is of type 'byte' and byteArray is a pointer to a byte_array_s BTW):

#include "byte_array_s.h"
...
byte_array_s * readBytes() {
  ...
  if(bitCounter != 0) add_byte(byteArray, currentByte);
  ...
}

Can anyone tell me what I am doing wrong? Is the definition not defined properly?

this is a "link" error; your definition is fine, but there doesn't appear to actually be an "add_byte" function created anywhere in your sketch.
(the "void add_byte(byte_array_s *byteArray, const byte _byte);" statement you have is a "prototype" that describes what the function looks like without actually creating any code. The code is what is missing.)

try replacing this

void add_byte(byte_array_s *byteArray, const byte _byte);

with this

void add_byte(byte_array_s *byteArray, const byte _byte)
{
    //do something;
}
  • Tim

I should have been more clear. I DO have the code defined in a separate file (byte_array_s.c). This file is saved in the same directory as the others. I presumed that the IDE would link them all together?

Am I mistaken about that?

mlambert@gnuget:~/arduino/arduino-0012/sketchbook/HID$ ls
applet  byte_array_s.c byte_array_s.h  HID.pde

You have to either re-open the sketch or explicitly add the file in order for it to be "noticed" as part of the sketch. If it dosn't show up on a tab in your sketch, it won't get compiled...

The ".c" files do show up as sperate tabs in the IDE but the only way I can get them to compile is to include them in the original sketch. :-/ Is this a known issue or is anyone else experiencing these problems?

Building the modules on the command line works but the Make file I downloaded seems to have trouble compiling the sketch itself (it is looking for a file that doesnt exist: 'WRandom.cpp') so that was a dead-end. Is there an updating Makefile (the md5sum for the file I have is cf4933cf13ee4d194060c76c69fa9471)?

I had a similar problem so I posted a question yesterday and I got the answer: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1228866246

As indicated, I renamed the .c files to .cpp and everything works now. I hope this helps.

Thank you so much I think this is the answer I was looking for! :slight_smile:

Update: added "extern C" to the header files as described in the linked thread and also here http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html and it now compiles

Thanks all!