Trouble linking from custom library

Hello

I'm implementing a proprietary communication protocol in Arduino, using libraries I got from my company's repository.

#include "crc.h"

unsigned char sequence[] = {0x15, 0x45, 0x01, 0x21, 0x00, 0xfe, 0x01};
unsigned short *crc;
unsigned short msgSize = 7;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  Serial.println("Alive");
  // reserve 200 bytes for the inputString:
  *crc = 0xFFFF; //initialize CRC
}

void loop() { 
     CRCRangeEx(sequence, crc, msgSize);
     Serial.println(*crc);
}

When I try to compile the code above, I get the error:
CrcTest.ino:16: undefined reference to `CRCRangeEx(unsigned char*, unsigned short*, unsigned short)'

Here's my function signature from the .h file:

void CRCRangeEx(unsigned char*, unsigned short*, unsigned short);

I know Arduino found the files and my function is compiling because if I mess up the code inside CRCRangeEx I get compiling errors. I've used custom libraries before, but this is my first time "creating" one. What am I doing wrong here?

Turn on verbose compiling (see preferences). Did you get an error/warning?

Try:

#include <crc.h>
  *crc = 0xFFFF; //initialize CRC

Where gets this written to ?

I guess you lost some code during copy / paste
The comment above rather looks like a TODO than as a description

Edit: Same for

// reserve 200 bytes for the inputString:

unsigned short *crc;

Why is crc a pointer? It should not be.

     CRCRangeEx(sequence, crc, msgSize);

If this function expects the second argument to be a pointer, use &crc, instead of making crc a pointer that points to nothing.

What kind of file is CRCRangeEx defined in? cpp or c? What does crc.h look like? Does it play well with cpp code?

Thanks for the replies. I got it to compile by copy/pasting my library functions to the code, and then I realized I hadn't allocated anything... (when I ran the code I posted, it messed up serial comm pretty badly).

Even after I fixed up the code (and it's running as intended with the copy/pasted CRCRangeEx code in the same file), if I delete CRCRangeEx and include the library I get the same error :frowning:

Here's my working code right now (minus the function I copied over):

unsigned char sequence[] = {0x15, 0x45, 0x01, 0x21, 0x00, 0xfe, 0x01};
unsigned short mycrc;
unsigned short msgSize = 5;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  Serial.println("Alive");
  // reserve 200 bytes for the inputString:
}

void loop() { 
  mycrc = 0xFFFF; //initialize CRC
  CRCRangeEx(sequence, &mycrc, msgSize);
  Serial.println(mycrc);
  delay(1000);
}

I have verbose on. I have a few warnings, but only on Arduino's own code (they pop up on every project). Also, changing between "crc.h" and <crc.h> doesn't seem to have any effect.

PaulS:
What kind of file is CRCRangeEx defined in? cpp or c? What does crc.h look like? Does it play well with cpp code?

The code is in C. Here's my crc.h:

unsigned short EE_FastCRCUpd(unsigned short, unsigned char);
unsigned short FastCRCUpd(unsigned short, unsigned char);
void CRCRangeEx(unsigned char*, unsigned short*, unsigned short);
unsigned short CRCRange(unsigned char*, unsigned short, unsigned short);
void CRCUpdate(unsigned char c);

Unfortunately I can't post the code in crc.c, but the function signature matches. If I attempt to use any of the other functions I get the same issue.

You need to add

extern "C" {

to the top of your .h file and

}

to the bottom, if you want to use functions implemented in a .c file in a .cpp file.

PaulS:
You need to add

extern "C" {

to the top of your .h file and

}

to the bottom, if you want to use functions implemented in a .c file in a .cpp file.

Adding that gives me a compiling error:

In file included from C:\Users\Mari\Documents\Arduino\libraries\CRC\crc.c:15:
C:\Users\Mari\Documents\Arduino\libraries\CRC/crc.h:26: error: expected identifier or '(' before string constant

crc.h:26 is where the the extern "C" { line is (I have a block of comments before that)

In my header files that define functions called by C or C++ code, I have:

#ifdef __cplusplus
extern "C" {
#endif

and

#ifdef __cplusplus
}
#endif

Check your spelling (we can't; you didn't post your code) and make sure that they are curly braces, not parentheses.

PaulS:
In my header files that define functions called by C or C++ code, I have:

#ifdef __cplusplus

extern "C" {
#endif



and


#ifdef __cplusplus
}
#endif




Check your spelling (we can't; you didn't post your code) and make sure that they are curly braces, not parentheses.

And... it works.
I have the feeling that I had parentheses there when I tried before... The error I was getting also indicates that. :blush:
Thanks for the patience XD