Pages: [1]   Go Down
Author Topic: Header file wquestion  (Read 783 times)
0 Members and 1 Guest are viewing this topic.
Ann Arbor, MI
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Code:

#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):

Code:
#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?
« Last Edit: December 07, 2008, 07:27:58 pm by MacFlecknoe » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6765
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

Cheltenham, England
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino Fiend
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

try replacing this
Code:
void add_byte(byte_array_s *byteArray, const byte _byte);
with this
Code:
void add_byte(byte_array_s *byteArray, const byte _byte)
{
    //do something;
}
- Tim
Logged

Ann Arbor, MI
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:

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

« Last Edit: December 08, 2008, 08:29:26 am by MacFlecknoe » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 135
Posts: 6765
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Ann Arbor, MI
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Ann Arbor, MI
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


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!
« Last Edit: December 10, 2008, 11:24:22 am by MacFlecknoe » Logged

Pages: [1]   Go Up
Jump to: