0

I have problem and hope, that you could help me. I try to make CAN-communication between two dsPIC30F4011. It also works. Now I have to make the Identifier. I have to use the SID and the EID. They are divided into 4 parts. I want to make a bit-declaration and get an error. I made a new header-file

#ifndef IDENTIFIER_H
#define IDENTIFIER_H

#ifdef  __cplusplus
extern "C" {
#endif




#ifdef  __cplusplus
}
#endif

#endif  /* IDENTIFIER_H */


#include <p30F4011.h>
#include "system.h"
#include <p30fxxxx.h>


typedef struct tagCxTXxSIDBITS{
    unsigned          : 11;
    unsigned PRIO4_0  : 5;
}CxTXxPRIOBITS;

extern volatile unsigned int C1TX0PRIO __attribute__((__sfr__));
extern volatile CxTXxPRIOBITS C1TX0PRIObits __attribute__((__sfr__));
extern volatile unsigned int C1TX1PRIO __attribute__((__sfr__));
extern volatile CxTXxPRIOBITS C1TX1PRIObits __attribute__((__sfr__));
extern volatile unsigned int C1TX2PRIO __attribute__((__sfr__));
extern volatile CxTXxPRIOBITS C1TX2PRIObits __attribute__((__sfr__));

In the Code I want to write

...
...
C1TX0PRIO = 0x0000;
...

If I want to build the project I get the error

build/default/production/CAN_function.o(.text+0x66): In  function    `.LSM19':
: undefined reference to `_C1TX0PRIO'
make[2]: *** [dist/default/production/blink.X.production.hex] Error 255
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

What did I do wrong? I wrote it like in the p30F4011.h

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Johannes
  • 31
  • 1
  • You've written a definition for C1TX0PRIO, but you've not written an implementation. i.e. in your C code you need `unsigned int C1TX0PRIO;` so that the variable exists – Brian Sidebotham Apr 29 '15 at 13:01

2 Answers2

1

TL;DR- declaration does not allocate memory, definition does.

As per C11 standard document, chapter §6.7, declaration,

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

— for an object, causes storage to be reserved for that object;

— .....

When you put extern storage class specifier, you're declaring a variable, not defining it.

So, you need to define the variable before you use it.

Add

volatile unsigned int C1TX0PRIO;

in your source file.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

You have the variables declared as extern in the header.

Usually you need to put the variable as extern in the header, if you want to use that variable from multiple source files. In this way, the variable will be accessible in all source files which include this header. However, in one of the C files you will need to have the following:

volatile unsigned int C1TX0PRIO __attribute__((__sfr__));
user252592
  • 141
  • 3